mirror of
https://github.com/RGBCube/GitHubWrapper
synced 2025-05-20 16:05:08 +00:00
rewrite parser
This commit is contained in:
parent
7e25087df4
commit
f2988d3405
3 changed files with 201 additions and 150 deletions
|
@ -1,50 +1,67 @@
|
|||
import json
|
||||
import os
|
||||
import time
|
||||
from argparse import ArgumentParser
|
||||
|
||||
from .parser import generate
|
||||
from .parser import generate_typed_dicts_from_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("-t", "--to", help="The file to write the TypedDicts to.")
|
||||
parser.add_argument(
|
||||
"-ne", "--no-examples", action="store_true", help="If given, examples wont be added."
|
||||
"-on",
|
||||
"--object-name",
|
||||
default="GeneratedObjectResult",
|
||||
help="The name of the object to generate.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-nf", "--no-formats", action="store_true", help="If given, formats wont be added."
|
||||
"-nc", "--no-comments", action="store_true", help="If given, comments wont be added."
|
||||
)
|
||||
parser.add_argument(
|
||||
"-p",
|
||||
"--print",
|
||||
action="store_true",
|
||||
help="If given, the result will be printed instead of being writing to a file",
|
||||
help="If given, the result will be printed.",
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
with open(args.__getattribute__("from")) as g:
|
||||
schema = json.load(g)
|
||||
with open(args.__getattribute__("from")) as f:
|
||||
generated = generate_typed_dicts_from_json_schema(json.load(f), no_comments=args.no_comments)
|
||||
|
||||
start = time.monotonic()
|
||||
text = f"""
|
||||
from __future__ import annotations
|
||||
start = time.perf_counter()
|
||||
|
||||
from typing import List, Optional, TypedDict, Union, TYPE_CHECKING
|
||||
text = f"""from __future__ import annotations
|
||||
|
||||
from typing import Any, List, Optional, TypedDict, Union, TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing_extensions import NotRequired{generate(schema, no_examples=args.no_examples, no_formats=args.no_formats)} # Rename this to your liking.
|
||||
"""[
|
||||
1:
|
||||
]
|
||||
from typing_extensions import NotRequired
|
||||
|
||||
{generated[0]}
|
||||
|
||||
{args.object_name} = {generated[1]}
|
||||
"""
|
||||
|
||||
if args.print:
|
||||
print(text)
|
||||
|
||||
end = time.perf_counter() - start
|
||||
|
||||
else:
|
||||
if not args.to:
|
||||
if not (to := args.to):
|
||||
raise ValueError("-t/--to is required when writing to a file.")
|
||||
|
||||
with open(args.to, "w") as f:
|
||||
with open(to, "w") as f:
|
||||
f.write(text)
|
||||
|
||||
end = time.monotonic() - start
|
||||
end = time.perf_counter() - start
|
||||
|
||||
exit_code = os.system(f"unimport {to} --gitignore -r --ignore-init; isort {to}; black {to}; flynt {to} -tc", )
|
||||
|
||||
if exit_code != 0:
|
||||
print(f"Formatting failed with exit code {exit_code}")
|
||||
|
||||
print(f"\n\nSuccess! Finished in {end*1000:.3} milliseconds")
|
||||
|
||||
|
||||
print(f"Success! Finished in {end*1000:.3} milliseconds.")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue