mirror of
https://github.com/RGBCube/GitHubWrapper
synced 2025-05-18 15:05:08 +00:00
Cleanup unschema
This commit is contained in:
parent
7d61048af6
commit
4723f0d23d
2 changed files with 55 additions and 32 deletions
|
@ -5,9 +5,21 @@ from argparse import ArgumentParser
|
||||||
|
|
||||||
from .parser import generate
|
from .parser import generate
|
||||||
|
|
||||||
parser = ArgumentParser(description="Generate TypedDicts from a json schema.")
|
# fmt: off
|
||||||
parser.add_argument("-f", "--from", required=True, help="The json schema to generate from.")
|
parser = ArgumentParser(
|
||||||
parser.add_argument("-t", "--to", help="The file to write the TypedDicts to.")
|
description="Generate TypedDicts from a json schema."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-f",
|
||||||
|
"--from",
|
||||||
|
required=True,
|
||||||
|
help="The json schema to generate from."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-t",
|
||||||
|
"--to",
|
||||||
|
help="The file to write the TypedDicts to."
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-on",
|
"-on",
|
||||||
"--object-name",
|
"--object-name",
|
||||||
|
@ -15,7 +27,10 @@ parser.add_argument(
|
||||||
help="The name of the object to generate.",
|
help="The name of the object to generate.",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-nc", "--no-comments", action="store_true", help="If given, comments wont be added."
|
"-nc",
|
||||||
|
"--no-comments",
|
||||||
|
action="store_true",
|
||||||
|
help="If given, comments wont be added."
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-p",
|
"-p",
|
||||||
|
@ -23,42 +38,28 @@ parser.add_argument(
|
||||||
action="store_true",
|
action="store_true",
|
||||||
help="If given, the result will be printed.",
|
help="If given, the result will be printed.",
|
||||||
)
|
)
|
||||||
|
# fmt: on
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
with open(args.__getattribute__("from")) as f:
|
with open(args.__getattribute__("from")) as f:
|
||||||
generated = generate(json.load(f), no_comments=args.no_comments)
|
schema = json.load(f)
|
||||||
|
|
||||||
start = time.perf_counter()
|
start = time.perf_counter()
|
||||||
|
generated = generate(schema, no_comments=args.no_comments)
|
||||||
text = f"""from __future__ import annotations
|
|
||||||
|
|
||||||
from typing import Any, List, Optional, TypedDict, Union, TYPE_CHECKING
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from typing_extensions import NotRequired
|
|
||||||
|
|
||||||
{generated[0]}
|
|
||||||
|
|
||||||
{args.object_name} = {generated[1]}
|
|
||||||
"""
|
|
||||||
|
|
||||||
if args.print:
|
|
||||||
print(text)
|
|
||||||
|
|
||||||
end = time.perf_counter() - start
|
end = time.perf_counter() - start
|
||||||
|
|
||||||
|
if args.print:
|
||||||
|
print(generated)
|
||||||
else:
|
else:
|
||||||
if not (to := args.to):
|
if not (to := args.to):
|
||||||
raise ValueError("-t/--to is required when writing to a file.")
|
raise ValueError("-t/--to is required when writing to a file.")
|
||||||
|
|
||||||
with open(to, "w") as f:
|
with open(to, "w") as f:
|
||||||
f.write(text)
|
f.write(generated)
|
||||||
|
|
||||||
end = time.perf_counter() - start
|
|
||||||
|
|
||||||
os.system(
|
os.system(
|
||||||
f"unimport {to} --gitignore -r --ignore-init; isort {to}; black {to}; flynt {to} -tc",
|
f"unimport {to} --gitignore -r --ignore-init; black {to}",
|
||||||
)
|
)
|
||||||
|
|
||||||
print(f"\n\nSuccess! Finished in {end*1000:.3} milliseconds")
|
print(f"\n\nSuccess! Finished in {end*1000:.3} milliseconds (formatting excluded)")
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
__all__ = ("generate",)
|
||||||
|
|
||||||
from typing import Tuple # , Any
|
from typing import Tuple # , Any
|
||||||
|
|
||||||
types = {
|
types = {
|
||||||
|
@ -21,7 +23,7 @@ types = {
|
||||||
# super().append(obj)
|
# super().append(obj)
|
||||||
|
|
||||||
|
|
||||||
def generate(
|
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,7 +55,7 @@ def generate(
|
||||||
if obj_schema["type"] == "null":
|
if obj_schema["type"] == "null":
|
||||||
optional = True
|
optional = True
|
||||||
else:
|
else:
|
||||||
extras, target = generate(obj_schema, title=title, no_comments=no_comments)
|
extras, target = _generate(obj_schema, title=title, no_comments=no_comments)
|
||||||
result.append(extras)
|
result.append(extras)
|
||||||
union_items.append(target)
|
union_items.append(target)
|
||||||
|
|
||||||
|
@ -120,7 +122,7 @@ def generate(
|
||||||
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(
|
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)
|
||||||
|
@ -156,7 +158,7 @@ def generate(
|
||||||
# maxContains, minLength, maxLength, uniqueItems
|
# maxContains, minLength, maxLength, uniqueItems
|
||||||
|
|
||||||
if obj_items := obj.get("items"):
|
if obj_items := obj.get("items"):
|
||||||
extras, target = generate(obj_items, no_comments=no_comments)
|
extras, target = _generate(obj_items, no_comments=no_comments)
|
||||||
result.append(extras)
|
result.append(extras)
|
||||||
annotation = f"List[{target}]"
|
annotation = f"List[{target}]"
|
||||||
|
|
||||||
|
@ -166,13 +168,13 @@ def generate(
|
||||||
(
|
(
|
||||||
extras,
|
extras,
|
||||||
target,
|
target,
|
||||||
) = generate(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(extra_item_type, no_comments=no_comments)
|
extras, extra_type = _generate(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(
|
||||||
|
@ -191,3 +193,23 @@ def generate(
|
||||||
result = [i for i in result if i]
|
result = [i for i in result if i]
|
||||||
|
|
||||||
return "\n".join(result), annotation
|
return "\n".join(result), annotation
|
||||||
|
|
||||||
|
|
||||||
|
text = """from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import TYPE_CHECKING, Any, List, Optional, TypedDict, Union
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from typing_extensions import NotRequired
|
||||||
|
|
||||||
|
{0[0]}
|
||||||
|
|
||||||
|
{1} = {0[1]}
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def generate(
|
||||||
|
schema: dict, /, *, object_name: str = "GeneratedObject", no_comments: bool = False
|
||||||
|
) -> str:
|
||||||
|
generated = _generate(schema, title=object_name, no_comments=no_comments)
|
||||||
|
return text.format(generated, object_name)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue