Skip to content

Commit

Permalink
fix: make black reformatting of generated files selectable
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelm committed Feb 12, 2022
1 parent a47fa13 commit 2b17390
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions utilities/process_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from collections import namedtuple
from datetime import datetime
from textwrap import TextWrapper
from typing import TextIO

import xmlschema
from markdownTable import markdownTable
Expand Down Expand Up @@ -334,7 +335,7 @@ def sort_schema(schema):
return toposort_flatten(dependancy_map, sort=True)


def open_output_files():
def open_output_files(prevent_reformat: bool):
results = []
generation_time = datetime.now()
for thing in ("type", "request", "response"):
Expand All @@ -344,7 +345,8 @@ def open_output_files():
out.write("# Autogenerated from the Broadworks XML Schemas.\n")
out.write("# Do not edit as changes will be overwritten.\n")
out.write(f"# Generated on {generation_time.isoformat()}\n")
out.write("# fmt: off\n")
if prevent_reformat:
out.write("# fmt: off\n")
out.write("from typing import List\n")
out.write("from typing import Tuple\n\n")
if thing in ("request", "response"):
Expand All @@ -358,18 +360,30 @@ def open_output_files():

def open_doc_files():
results = []
generation_time = datetime.now()
for thing in ("type", "request", "response"):
filename = os.path.join("docs/api", thing + "s.md")
out = open(filename, "w")
out.write("<!--\n")
out.write("# Autogenerated from Broadworks XML Schemas\n")
out.write("# Do not edit as changes will be overwritten.\n")
out.write(f"# Generated on {generation_time.isoformat()}\n")
out.write("-->\n")
out.write(f"# {thing.title()}s\n")
out.write("\n")
results.append(out)
return results[0], results[1], results[2]


def close_output_files(types_file, requests_file, responses_file):
def close_output_files(
prevent_reformat: bool,
types_file: TextIO,
requests_file: TextIO,
responses_file: TextIO,
):
for out in (types_file, requests_file, responses_file):
out.write("# fmt: on\n")
if prevent_reformat:
out.write("# fmt: on\n")
out.write("# end\n")


Expand All @@ -389,6 +403,10 @@ def main():
"--pickle",
action="store_true",
)
parser.add_argument(
"--block-black",
action="store_true",
)
args = parser.parse_args()
if args.schema:
schema = xmlschema.XMLSchema(args.schema)
Expand All @@ -405,7 +423,7 @@ def main():
sys.exit("No schema")
class_list = sort_schema(schema)
print("Sorted schema")
types_file, requests_file, responses_file = open_output_files()
types_file, requests_file, responses_file = open_output_files(args.block_black)
types_docfile, requests_docfile, responses_docfile = open_doc_files()
process_schema(
schema,
Expand All @@ -417,7 +435,7 @@ def main():
requests_docfile,
responses_docfile,
)
close_output_files(types_file, requests_file, responses_file)
close_output_files(args.block_black, types_file, requests_file, responses_file)


if __name__ == "__main__":
Expand Down

0 comments on commit 2b17390

Please sign in to comment.