1
Fork 0
mirror of https://github.com/RGBCube/GitHubWrapper synced 2025-05-18 23:15:09 +00:00

Rename it to generate

This commit is contained in:
RGBCube 2022-06-26 17:28:01 +03:00
parent 132f42cf28
commit 193b8ad93d
3 changed files with 9 additions and 17 deletions

View file

@ -1 +1 @@
from .parser import generate_typed_dicts_from_json_schema from .parser import generate

View file

@ -3,7 +3,7 @@ import os
import time import time
from argparse import ArgumentParser from argparse import ArgumentParser
from .parser import generate_typed_dicts_from_json_schema from .parser import generate
parser = ArgumentParser(description="Generate TypedDicts from a json schema.") parser = ArgumentParser(description="Generate TypedDicts from a json schema.")
parser.add_argument("-f", "--from", required=True, help="The json schema to generate from.") parser.add_argument("-f", "--from", required=True, help="The json schema to generate from.")
@ -27,7 +27,7 @@ parser.add_argument(
args = parser.parse_args() args = parser.parse_args()
with open(args.__getattribute__("from")) as f: with open(args.__getattribute__("from")) as f:
generated = generate_typed_dicts_from_json_schema(json.load(f), no_comments=args.no_comments) generated = generate(json.load(f), no_comments=args.no_comments)
start = time.perf_counter() start = time.perf_counter()

View file

@ -21,7 +21,7 @@ types = {
# super().append(obj) # super().append(obj)
def generate_typed_dicts_from_json_schema( def generate(
obj: dict, /, *, title: str = "GeneratedObject", no_comments: bool = False obj: dict, /, *, title: str = "GeneratedObject", no_comments: bool = False
) -> Tuple[str, str]: # sourcery skip: low-code-quality ) -> Tuple[str, str]: # sourcery skip: low-code-quality
"""Makes TypedDict from a JSON Schema object. """Makes TypedDict from a JSON Schema object.
@ -53,9 +53,7 @@ def generate_typed_dicts_from_json_schema(
if obj_schema["type"] == "null": if obj_schema["type"] == "null":
optional = True optional = True
else: else:
extras, target = generate_typed_dicts_from_json_schema( extras, target = generate(obj_schema, title=title, no_comments=no_comments)
obj_schema, title=title, no_comments=no_comments
)
result.append(extras) result.append(extras)
union_items.append(target) union_items.append(target)
@ -122,9 +120,7 @@ def generate_typed_dicts_from_json_schema(
typed_dict = [f"class {obj_title}(TypedDict{total}):"] typed_dict = [f"class {obj_title}(TypedDict{total}):"]
for key, value in obj_properties.items(): for key, value in obj_properties.items():
extras, param_annotation = generate_typed_dicts_from_json_schema( extras, param_annotation = generate(value, title=key.capitalize(), no_comments=no_comments)
value, title=key.capitalize(), no_comments=no_comments
)
result.append(extras) result.append(extras)
if key not in obj.get("required", []): if key not in obj.get("required", []):
@ -151,9 +147,7 @@ def generate_typed_dicts_from_json_schema(
# maxContains, minLength, maxLength, uniqueItems # maxContains, minLength, maxLength, uniqueItems
if obj_items := obj.get("items"): if obj_items := obj.get("items"):
extras, target = generate_typed_dicts_from_json_schema( extras, target = generate(obj_items, no_comments=no_comments)
obj_items, no_comments=no_comments
)
result.append(extras) result.append(extras)
annotation = f"List[{target}]" annotation = f"List[{target}]"
@ -163,15 +157,13 @@ def generate_typed_dicts_from_json_schema(
( (
extras, extras,
target, target,
) = generate_typed_dicts_from_json_schema(item, no_comments=no_comments) ) = generate(item, no_comments=no_comments)
result.append(extras) result.append(extras)
tuple_annotation.append(target) tuple_annotation.append(target)
if extra_item_type := obj.get("items"): if extra_item_type := obj.get("items"):
if extra_item_type is not True: if extra_item_type is not True:
extras, extra_type = generate_typed_dicts_from_json_schema( extras, extra_type = generate(extra_item_type, no_comments=no_comments)
extra_item_type, no_comments=no_comments
)
result.append(extras) result.append(extras)
if not no_comments: if not no_comments:
result.append( result.append(