Skip to content

Commit 1c8951f

Browse files
committed
feat: add options for local page loading and Python code generation in PagePackageController
1 parent f2b0d1a commit 1c8951f

File tree

2 files changed

+91
-23
lines changed

2 files changed

+91
-23
lines changed

src/osw/controller/page_package.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,17 @@ class CreationConfig(OswBaseModel):
372372
offline_pages: Optional[Dict[str, WtPage]] = None
373373
"""A dictionary of pages that are already loaded. Pages in this dictionary
374374
will not be fetched again."""
375+
prefer_local_pages: bool = False
376+
"""Load the pages from the local working directory
377+
instead of the server if set to True."""
378+
generate_python_code: bool = False
379+
"""Whether to generate python code for the data models."""
380+
python_code_working_dir: Optional[Union[str, Path]] = None
381+
"""Working directory for python code generation. If set, pydantic v2 data models
382+
will be generated in this directory, v1 models in a /v1 subdirectory.
383+
"""
384+
python_code_filename: Optional[str] = "_model_generated.py"
385+
"""Filename for the generated python code."""
375386

376387
class Config:
377388
arbitrary_types_allowed = True
@@ -418,6 +429,57 @@ def create(
418429
)
419430
},
420431
)
432+
433+
offline_pages = creation_config.offline_pages
434+
local_pages = {}
435+
if creation_config.prefer_local_pages:
436+
# Read the local pages from the package
437+
result = wtsite.read_page_package(
438+
WtSite.ReadPagePackageParam(
439+
package_name=self.name,
440+
storage_path=Path(creation_config.working_dir),
441+
)
442+
)
443+
local_pages = {p.title: p for p in result.pages}
444+
if offline_pages is None:
445+
offline_pages = local_pages
446+
else:
447+
# Merge the local pages with the offline pages
448+
offline_pages.update(local_pages)
449+
450+
if (
451+
creation_config.generate_python_code
452+
and creation_config.python_code_working_dir is not None
453+
):
454+
python_code_path = Path(creation_config.python_code_working_dir)
455+
python_code_path /= creation_config.python_code_filename
456+
schema_titles = self.page_titles
457+
# remove duplicates and entries in ignore_titles
458+
schema_titles = list(
459+
set(schema_titles)
460+
- (
461+
set(creation_config.ignore_titles)
462+
if creation_config.ignore_titles
463+
else set()
464+
)
465+
)
466+
# remove all schemas that do not start with "Category:"
467+
schema_titles = [
468+
title for title in schema_titles if title.startswith("Category:")
469+
]
470+
from osw.core import OSW
471+
472+
osw_obj = OSW(site=wtsite)
473+
474+
osw_obj.fetch_schema(
475+
fetchSchemaParam=OSW.FetchSchemaParam(
476+
schema_title=schema_titles,
477+
offline_pages=offline_pages,
478+
result_model_path=python_code_path,
479+
mode="replace",
480+
)
481+
)
482+
421483
# Create a PagePackageConfig instance
422484
config = package.PagePackageConfig(
423485
name=self.name,
@@ -432,7 +494,7 @@ def create(
432494
wtsite.create_page_package(
433495
WtSite.CreatePagePackageParam(
434496
config=config,
435-
offline_pages=creation_config.offline_pages,
497+
offline_pages=offline_pages,
436498
)
437499
)
438500

src/osw/wtsite.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,9 @@ class ReadPagePackageParam(OswBaseModel):
848848
"""A list of slots that should be read. If None, all slots are read."""
849849
debug: Optional[bool] = False
850850
"""If True, debug information is printed to the console."""
851+
offline: Optional[bool] = True
852+
"""Skip reading the page content from the webserver
853+
before reading the local content, if True."""
851854

852855
class ReadPagePackageResult(OswBaseModel):
853856
"""Return type of read_page_package."""
@@ -957,19 +960,9 @@ def get_slot_content(
957960
namespace = page["namespace"].split("_")[-1].capitalize()
958961
name = page["name"]
959962
# Create the WtPage object
960-
page_obj = WtPage(wtSite=self, title=f"{namespace}:{name}")
961-
if "main" in selected_slots:
962-
# Main slot is special
963-
slot_content = get_slot_content(
964-
parent_dir=sub_dirs,
965-
url_path=page["urlPath"],
966-
files_in_storage_path=storage_path_content["files"],
967-
)
968-
if slot_content is not None:
969-
page_obj.set_slot_content(
970-
slot_key="main",
971-
content=slot_content,
972-
)
963+
page_obj = WtPage(
964+
wtSite=self, title=f"{namespace}:{name}", do_init=not param.offline
965+
)
973966
if selected_slots is None:
974967
_selected_slots = page["slots"]
975968
else:
@@ -979,16 +972,29 @@ def get_slot_content(
979972
if slot_name in selected_slots
980973
}
981974
for slot_name, slot_dict in _selected_slots.items():
982-
slot_content = get_slot_content(
983-
parent_dir=sub_dirs,
984-
url_path=slot_dict["urlPath"],
985-
files_in_storage_path=storage_path_content["files"],
986-
)
987-
if slot_content is not None:
988-
page_obj.set_slot_content(
989-
slot_key=slot_name,
990-
content=slot_content,
975+
if slot_name == "main":
976+
# Main slot is special
977+
slot_content = get_slot_content(
978+
parent_dir=sub_dirs,
979+
url_path=page["urlPath"],
980+
files_in_storage_path=storage_path_content["files"],
991981
)
982+
if slot_content is not None:
983+
page_obj.set_slot_content(
984+
slot_key="main",
985+
content=slot_content,
986+
)
987+
else:
988+
slot_content = get_slot_content(
989+
parent_dir=sub_dirs,
990+
url_path=slot_dict["urlPath"],
991+
files_in_storage_path=storage_path_content["files"],
992+
)
993+
if slot_content is not None:
994+
page_obj.set_slot_content(
995+
slot_key=slot_name,
996+
content=slot_content,
997+
)
992998
pages.append(page_obj)
993999
return WtSite.ReadPagePackageResult(pages=pages)
9941000

0 commit comments

Comments
 (0)