Skip to content

Commit 35dd834

Browse files
committed
feat: offline pages and custom target path for code gen
1 parent 3d4c845 commit 35dd834

1 file changed

Lines changed: 36 additions & 5 deletions

File tree

‎src/osw/core.py‎

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,14 @@ class FetchSchemaParam(BaseModel):
380380
)
381381
legacy_generator: Optional[bool] = False
382382
"""uses legacy command line for code generation if true"""
383+
offline_pages: Optional[Dict[str, WtPage]] = None
384+
"""pages to be used offline instead of fetching them from the OSW instance"""
385+
result_model_path: Optional[Union[str, pathlib.Path]] = None
386+
"""path to the generated model file, if None,
387+
the default path ./model/entity.py is used"""
388+
389+
class Config:
390+
arbitrary_types_allowed = True
383391

384392
def fetch_schema(self, fetchSchemaParam: FetchSchemaParam = None) -> None:
385393
"""Loads the given schemas from the OSW instance and auto-generates python
@@ -402,6 +410,8 @@ def fetch_schema(self, fetchSchemaParam: FetchSchemaParam = None) -> None:
402410
schema_title=schema_title,
403411
mode=mode,
404412
legacy_generator=fetchSchemaParam.legacy_generator,
413+
offline_pages=fetchSchemaParam.offline_pages,
414+
result_model_path=fetchSchemaParam.result_model_path,
405415
)
406416
)
407417
first = False
@@ -428,9 +438,17 @@ class _FetchSchemaParam(BaseModel):
428438
)
429439
legacy_generator: Optional[bool] = False
430440
"""uses legacy command line for code generation if true"""
441+
offline_pages: Optional[Dict[str, WtPage]] = None
442+
"""pages to be used offline instead of fetching them from the OSW instance"""
443+
result_model_path: Optional[Union[str, pathlib.Path]] = None
444+
"""path to the generated model file, if None,
445+
the default path ./model/entity.py is used"""
431446
fetched_schema_titles: Optional[List[str]] = []
432447
"""keep track of fetched schema titles to prevent recursion"""
433448

449+
class Config:
450+
arbitrary_types_allowed = True
451+
434452
def _fetch_schema(self, fetchSchemaParam: _FetchSchemaParam = None) -> None:
435453
"""Loads the given schema from the OSW instance and autogenerates python
436454
datasclasses within osw.model.entity from it
@@ -448,10 +466,20 @@ def _fetch_schema(self, fetchSchemaParam: _FetchSchemaParam = None) -> None:
448466
fetchSchemaParam.fetched_schema_titles.append(schema_title)
449467
root = fetchSchemaParam.root
450468
schema_name = schema_title.split(":")[-1]
451-
page = self.site.get_page(WtSite.GetPageParam(titles=[schema_title])).pages[0]
452-
if not page.exists:
453-
print(f"Error: Page {schema_title} does not exist")
454-
return
469+
if (
470+
fetchSchemaParam.offline_pages is not None
471+
and schema_title in fetchSchemaParam.offline_pages
472+
):
473+
print(f"Fetch {schema_title} from offline pages")
474+
page = fetchSchemaParam.offline_pages[schema_title]
475+
else:
476+
print(f"Fetch {schema_title} from online pages")
477+
page = self.site.get_page(WtSite.GetPageParam(titles=[schema_title])).pages[
478+
0
479+
]
480+
if not page.exists:
481+
print(f"Error: Page {schema_title} does not exist")
482+
return
455483
# not only in the JsonSchema namespace the schema is located in the main sot
456484
# in all other namespaces, the json_schema slot is used
457485
if schema_title.startswith("JsonSchema:"):
@@ -482,7 +510,6 @@ def _fetch_schema(self, fetchSchemaParam: _FetchSchemaParam = None) -> None:
482510
)
483511
# fix https://github.com/koxudaxi/datamodel-code-generator/issues/1910
484512
)
485-
print(f"Fetch {schema_title}")
486513

487514
jsonpath_expr = parse("$..dollarref")
488515
for match in jsonpath_expr.find(schema):
@@ -523,6 +550,10 @@ def _fetch_schema(self, fetchSchemaParam: _FetchSchemaParam = None) -> None:
523550

524551
# result_model_path = schema_path.replace(".json", ".py")
525552
result_model_path = os.path.join(model_dir_path, "entity.py")
553+
if fetchSchemaParam.result_model_path:
554+
result_model_path = fetchSchemaParam.result_model_path
555+
if not isinstance(result_model_path, str):
556+
result_model_path = str(result_model_path)
526557
temp_model_path = os.path.join(model_dir_path, "temp.py")
527558
if root:
528559
if fetchSchemaParam.legacy_generator:

0 commit comments

Comments
 (0)