Skip to content

Commit 1c69fa2

Browse files
committed
refactor: use dataclasses as params
BREAKING CHANGE: Changes the function signature of WtPage.create_page_package, WtPage.read_page_package and WtPage.upload_page_package
1 parent 6d50789 commit 1c69fa2

File tree

2 files changed

+92
-45
lines changed

2 files changed

+92
-45
lines changed

examples/gui_local_slot_editing.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,11 @@ def save_as_page_package(
124124
titles=[full_page_name_str],
125125
)
126126
wtsite_inst.create_page_package(
127-
config=config,
128-
dump_config=dump_config_inst,
129-
debug=False,
127+
WtSite.CreatePagePackageParam(
128+
config=config,
129+
dump_config=dump_config_inst,
130+
debug=False,
131+
)
130132
)
131133
return {"Page package bundle": bundle, "Page package config": config}
132134

@@ -458,11 +460,13 @@ def save_as_page_package(
458460
else:
459461
storage_path = Path(dump_config.target_dir).parent
460462
pages = wtsite_obj.read_page_package(
461-
storage_path=storage_path,
462-
packages_info_file_name=PACKAGE_INFO_FILE_NAME,
463-
selected_slots=slots_to_upload,
464-
debug=False,
465-
)
463+
WtSite.ReadPagePackageParam(
464+
storage_path=storage_path,
465+
packages_info_file_name=PACKAGE_INFO_FILE_NAME,
466+
selected_slots=slots_to_upload,
467+
debug=False,
468+
)
469+
).pages
466470
param = wtsite_obj.UploadPageParam(pages=pages, parallel=False)
467471
wtsite_obj.upload_page(param)
468472
# Success:

src/osw/wtsite.py

Lines changed: 80 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,32 @@ def upload_page(index_, p_):
309309
dask.compute(*tasks)
310310
message_buffer.flush()
311311

312-
def create_page_package(
313-
self,
314-
config: package.PagePackageConfig,
315-
dump_config: "WtPage.PageDumpConfig" = None,
316-
debug: bool = True,
317-
):
312+
class CreatePagePackageParam(model.OswBaseModel):
313+
"""Parameter object for create_page_package method."""
314+
315+
config: package.PagePackageConfig
316+
"""Configuration object for the page package."""
317+
dump_config: Optional["WtPage.PageDumpConfig"] = None
318+
"""Configuration object for the page dump"""
319+
debug: Optional[bool] = True
320+
"""If True, debug messages will be printed."""
321+
322+
class Config:
323+
arbitrary_types_allowed = True
324+
325+
def create_page_package(self, param: CreatePagePackageParam):
318326
"""Create a page package, which is a locally stored collection of wiki pages
319-
and their slots, based on a configuration object."""
327+
and their slots, based on a configuration object.
328+
329+
Parameters:
330+
-----------
331+
param: CreatePagePackageParam
332+
"""
333+
334+
debug = param.debug
335+
dump_config = param.dump_config
336+
config = param.config
337+
320338
# Clear the content directory
321339
try:
322340
if debug:
@@ -369,33 +387,42 @@ def create_page_package(
369387
with open(file_name, "w") as f:
370388
f.write(content)
371389

372-
def read_page_package(
373-
self,
374-
storage_path: Union[str, Path],
375-
packages_info_file_name: Union[str, Path] = None,
376-
selected_slots: List[str] = None,
377-
debug: bool = False,
378-
) -> List["WtPage"]:
390+
class ReadPagePackageParam(model.OswBaseModel):
391+
"""Parameter type of read_page_package."""
392+
393+
storage_path: Union[str, Path]
394+
"""The path to the directory where the page package is stored."""
395+
packages_info_file_name: Optional[Union[str, Path]] = None
396+
"""The name of the file that contains the page package information. If not
397+
specified, the default value 'packages.json' is used."""
398+
selected_slots: Optional[List[str]] = None
399+
"""A list of slots that should be read. If None, all slots are read."""
400+
debug: Optional[bool] = False
401+
"""If True, debug information is printed to the console."""
402+
403+
class ReadPagePackageResult(model.OswBaseModel):
404+
"""Return type of read_page_package."""
405+
406+
pages: List["WtPage"]
407+
"""A list of WtPage objects."""
408+
409+
def read_page_package(self, param: ReadPagePackageParam) -> ReadPagePackageResult:
379410
"""Read a page package, which is a locally stored collection of wiki pages and
380411
their slots' content.
381412
382413
Parameters
383414
----------
384-
storage_path:
385-
The path to the directory where the page package is stored.
386-
packages_info_file_name:
387-
The name of the file that contains the page package information. If not
388-
specified, the default value 'packages.json' is used.
389-
selected_slots:
390-
A list of slots that should be read. If None, all slots are read.
391-
debug:
392-
If True, debug information is printed to the console.
415+
param: ReadPagePackageParam
393416
394417
Returns
395418
-------
396-
result:
397-
A list of WtPage objects.
419+
result: ReadPagePackageResult
398420
"""
421+
# map params
422+
storage_path = param.storage_path
423+
packages_info_file_name = param.packages_info_file_name
424+
selected_slots = param.selected_slots
425+
debug = param.debug
399426
# Test arguments / set default value
400427
if not os.path.exists(storage_path):
401428
raise FileNotFoundError(f"Storage path '{storage_path}' does not exist.")
@@ -511,20 +538,40 @@ def get_slot_content(
511538
content=slot_content,
512539
)
513540
pages.append(page_obj)
514-
return pages
541+
return "ReadPagePackageResult"(page_list=pages)
542+
543+
class UploadPagePackageParam(model.OswBaseModel):
544+
"""Parameter class for upload_page_package method."""
545+
546+
storage_path: Optional[Union[str, Path]] = None
547+
"""The path to the storage directory.
548+
If 'storage_path' is not given, 'pages' must be given."""
549+
pages: Optional[List["WtPage"]] = None
550+
"""A list of WtPage objects.
551+
If 'pages' is not given, 'storage_path' must be given."""
552+
debug: Optional[bool] = False
553+
"""If True, prints debug information."""
554+
555+
def upload_page_package(self, param: UploadPagePackageParam):
556+
"""Uploads a page package to the wiki defined by a list of WtPage objects or a storage path.
557+
558+
Parameters
559+
----------
560+
param : UploadPagePackageParam
561+
562+
"""
563+
storage_path = param.storage_path
564+
pages = param.pages
565+
debug = param.debug
515566

516-
def upload_page_package(
517-
self,
518-
storage_path: Union[str, Path] = None,
519-
pages: List["WtPage"] = None,
520-
debug: bool = False,
521-
):
522567
if storage_path and pages is None:
523568
raise ValueError(
524569
"Error: If 'storage_path' is not given, 'pages' must be given."
525570
)
526571
if pages is None:
527-
pages = self.read_page_package(storage_path=storage_path, debug=debug)
572+
pages = self.read_page_package(
573+
WtSite.ReadPagePackageParam(storage_path=storage_path, debug=debug)
574+
).pages
528575
for page in pages:
529576
page.edit()
530577

@@ -876,7 +923,3 @@ def dump_slot_content(slot_key_, content_type_, content_):
876923
package_page.fileURLPath = path_prefix + file_name
877924

878925
return package_page
879-
880-
881-
# Updating forwards refs in pydantic models
882-
WtSite.UploadPageParam.update_forward_refs()

0 commit comments

Comments
 (0)