Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jirikuncar committed Feb 3, 2022
1 parent c27f423 commit 83fe08b
Showing 1 changed file with 0 additions and 103 deletions.
103 changes: 0 additions & 103 deletions .generator/src/generator/openapi.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import hashlib
import json
import pathlib
import uuid
import random
import yaml
import warnings
from datetime import datetime
from jsonref import JsonRef
from urllib.parse import urlparse
from yaml import CSafeLoader
Expand Down Expand Up @@ -332,101 +327,3 @@ def response(operation, status_code=None):
"schema"
]
return None


def lookup(data, dotted_path):
result = data
for dot_path in dotted_path.split("."):
for part in dot_path.split("["):
if "]" in part:
index = int(part[: part.index("]")])
result = result[index]
else:
result = result[part]
return result


def generate_value(schema, use_random=False, prefix=None):
spec = schema.spec
if not use_random:
if "example" in spec:
return spec["example"]
if "default" in spec:
return spec["default"]

if spec["type"] == "string":
if use_random:
return str(
uuid.UUID(
bytes=hashlib.pbkdf2_hmac(
"sha256",
str(prefix or schema.keys).encode("utf-8"),
b"",
10 ** 6,
dklen=16,
)
)
)
return "string"
elif spec["type"] == "integer":
return random.randint(0, 32000) if use_random else 1
elif spec["type"] == "number":
return random.random() if use_random else 1.0
elif spec["type"] == "boolean":
return True
elif spec["type"] == "array":
return [generate_value(schema[0], use_random=use_random)]
elif spec["type"] == "object":
return {
key: generate_value(schema[key], use_random=use_random)
for key in spec["properties"]
}
else:
raise TypeError(f"Unknown type: {spec['type']}")


class Schema:
def __init__(self, spec, value=None, keys=None):
self.spec = spec
self.value = value if value is not None else generate_value
self.keys = keys or tuple()

def __getattr__(self, key):
return self[key]

def __getitem__(self, key):
type_ = self.spec.get("type", "object")
if type_ == "object":
try:
return self.__class__(
self.spec["properties"][key],
value=self.value,
keys=self.keys + (key,),
)
except KeyError:
if "oneOf" in self.spec:
for schema in self.spec["oneOf"]:
if schema.get("type", "object") == "object":
try:
return self.__class__(
schema["properties"][key],
value=self.value,
keys=self.keys + (key,),
)
except KeyError:
pass
raise KeyError(
f"{key} not found in {self.spec.get('properties', {}).keys()}: {self.spec}"
)
if type_ == "array":
return self.__class__(
self.spec["items"], value=self.value, keys=self.keys + (key,)
)

raise KeyError(f"{key} not found in {self.spec}")

def __repr__(self):
value = self.value(self)
if isinstance(value, (dict, list)):
return json.dumps(value, indent=2)
return str(value)

0 comments on commit 83fe08b

Please sign in to comment.