Skip to content

Commit

Permalink
trying to allow non ascii-characters to be saved to file (json_data a…
Browse files Browse the repository at this point in the history
…nd json_schema slots)

Line 743 in wtsite.py makes the the page download fail
  • Loading branch information
LukasGold committed Nov 16, 2023
1 parent a01f6d4 commit 1818726
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/osw/wtsite.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,6 @@ def create_page_package(self, param: CreatePagePackageParam):
added_titles.append(title)
page = self.get_page(WtSite.GetPageParam(titles=[title])).pages[0]

page = self.get_WtPage(title)
bundle.packages[config.name].pages.append(page.dump(dump_config))
if config.include_files:
for file in page._page.images():
Expand All @@ -459,11 +458,11 @@ def create_page_package(self, param: CreatePagePackageParam):
file_page.dump(dump_config)
)

content = bundle.json(exclude_none=True, indent=4)
content = bundle.json(exclude_none=True, indent=4, ensure_ascii=False)
# This will create the JSON (e.g., package.json) with the PagePackageConfig,
# which contains the PagePackageBundle
file_name = f"{config.config_path}"
with open(file_name, "w") as f:
with open(file_name, "w", encoding="utf-8") as f:
f.write(content)

class ReadPagePackageParam(model.OswBaseModel):
Expand Down Expand Up @@ -548,7 +547,7 @@ def read_page_package(self, param: ReadPagePackageParam) -> ReadPagePackageResul
f"Error: No JSON files found in '{storage_path}'."
)
# Read packages info file
with open(pi_fp, "r") as f:
with open(pi_fp, "r", encoding="utf-8") as f:
packages_json = json.load(f)
# Assume that the pages files are located in the subdir
storage_path_content = ut.list_files_and_directories(
Expand All @@ -568,12 +567,12 @@ def get_slot_content(
for pdir in parent_dir:
slot_path = storage_path / pdir / url_path
if slot_path in files_in_storage_path:
with open(slot_path, "r") as f:
with open(slot_path, "r", encoding="utf-8") as f:
file_content = f.read()
# Makes sure not to open an empty file with json
if len(file_content) > 0:
if url_path.endswith(".json"):
with open(slot_path, "r") as f:
with open(slot_path, "r", encoding="utf-8") as f:
slot_data = json.load(f)
return slot_data
elif url_path.endswith(".wikitext"):
Expand Down Expand Up @@ -742,7 +741,10 @@ def __init__(self, wtSite: WtSite = None, title: str = None):
# revision["slots"][slot_key]["*"]
if self._content_model[slot_key] == "json":
self._slots[slot_key] = json.loads(
self._slots[slot_key]
self._slots[slot_key],
ensure_ascii=False
# todo: fix - adding ensure_ascii=False causes
# the page load to fail
)
# todo: set content for slots not in revision["slots"] (use
# SLOTS) --> create empty slots
Expand Down Expand Up @@ -885,7 +887,7 @@ def _edit(self, comment: str = None, mode="action-multislot"):
content = self._slots[slot_key]
if self._content_model[slot_key] == "json":
if not isinstance(content, str):
content = json.dumps(content)
content = json.dumps(content, ensure_ascii=False)
params["slot_" + slot_key] = content
if changed:
self.wtSite._site.api(
Expand All @@ -902,7 +904,7 @@ def _edit(self, comment: str = None, mode="action-multislot"):
if self._slots_changed[slot_key]:
content = self._slots[slot_key]
if self._content_model[slot_key] == "json":
content = json.dumps(content)
content = json.dumps(content, ensure_ascii=False)
self.wtSite._site.api(
"editslot",
token=self.wtSite._site.get_token("csrf"),
Expand Down Expand Up @@ -1004,7 +1006,7 @@ def save_to_file(file_path__, content__):

def dump_slot_content(slot_key_, content_type_, content_):
if isinstance(content_, dict):
content_ = json.dumps(content_, indent=4)
content_ = json.dumps(content_, indent=4, ensure_ascii=False)
if content_type_ == "Scribunto":
content_type_ = "lua"
if slot_key_ == "main" and config.skip_slot_suffix_for_main:
Expand Down

0 comments on commit 1818726

Please sign in to comment.