|
1 | 1 | import ast |
2 | | -import astor |
3 | 2 | import json |
4 | | -from collections.abc import Iterable |
| 3 | +import subprocess |
5 | 4 | from pathlib import Path |
| 5 | +from collections.abc import Iterable |
| 6 | +import astor |
6 | 7 | from typing import Dict, List, Tuple, Iterator, Set |
| 8 | +import black |
7 | 9 | from . import snake_case |
8 | 10 |
|
9 | 11 | directory = Path(__file__).parent |
@@ -112,7 +114,7 @@ def get_domains(_property: dict) -> Iterator[dict]: |
112 | 114 | yield domain |
113 | 115 |
|
114 | 116 |
|
115 | | -def generate() -> str: |
| 117 | +def generate(module_path: Path) -> str: |
116 | 118 | module = astor.code_to_ast.parse_file(header_path) |
117 | 119 | # print(astor.dump_tree(tree)) |
118 | 120 | with schema_path.open() as file: |
@@ -153,9 +155,15 @@ def generate() -> str: |
153 | 155 | (ast.Constant("@type"), ast.Constant(step_class["@id"])) |
154 | 156 | ] |
155 | 157 | properties = properties_by_domain.get(step_class["@id"], []) |
| 158 | + sorted_properties = sorted( |
| 159 | + properties, |
| 160 | + key=lambda _property: "" |
| 161 | + if _property["@id"] == "linkedql:from" |
| 162 | + else _property["@id"], |
| 163 | + ) |
156 | 164 | positional_args = [] |
157 | 165 | kwonlyargs = [] |
158 | | - for _property in sorted(properties, key=lambda _property: _property["@id"]): |
| 166 | + for _property in sorted_properties: |
159 | 167 | argument_name = remove_linked_ql(_property["@id"]) |
160 | 168 | if argument_name == "from": |
161 | 169 | is_method = True |
@@ -211,4 +219,8 @@ def generate() -> str: |
211 | 219 | class_def.body.append(function_def) |
212 | 220 | else: |
213 | 221 | module.body.append(function_def) |
214 | | - return astor.to_source(module) |
| 222 | + generated_code = astor.to_source(module) |
| 223 | + with module_path.open("w+") as file: |
| 224 | + file.write(generated_code) |
| 225 | + subprocess.run(["black", str(module_path)]) |
| 226 | + |
0 commit comments