Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions examples/gui_local_slot_editing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from numpy import array as np_array

import osw.model.page_package as package
from osw.auth import CredentialManager
from osw.core import OSW
from osw.wiki_tools import read_domains_from_credentials_file
from osw.wtsite import SLOTS, WtPage, WtSite
Expand Down Expand Up @@ -164,10 +165,9 @@ def save_as_page_package(
if settings_read_from_file:
settings["domain"] = domain

wtsite_obj = WtSite.from_domain(
domain=domain, password_file="", credentials=accounts[domain]
)
osw_obj = OSW(site=wtsite_obj)
cm = CredentialManager(cred_filepath=settings["credentials_file_path"])
osw_obj = OSW(site=WtSite(WtSite.WtSiteConfig(iri=domain, cred_mngr=cm)))
wtsite_obj = osw_obj.site

full_page_name = settings["target_page"].split("/")[-1].replace("_", " ")
page = wtsite_obj.get_page(WtSite.GetPageParam(titles=[full_page_name])).pages[0]
Expand Down Expand Up @@ -378,12 +378,10 @@ def save_as_page_package(
elif event == "-DOMAIN-":
settings["domain"] = values["-DOMAIN-"]
domain = settings["domain"].split("//")[-1]
wtsite_obj = WtSite.from_domain(
domain=settings["domain"],
password_file="",
credentials=accounts[settings["domain"]],
osw_obj = OSW(
site=WtSite(WtSite.WtSiteConfig(iri=settings["domain"], cred_mngr=cm))
)
osw_obj = OSW(site=wtsite_obj)
wtsite_obj = osw_obj.site
elif event == "Load page":
window["-LABEL-"].update("Loading page...")
window["-DL_RES-"].update("")
Expand Down
19 changes: 9 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,7 @@ 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],
)
# todo: set content for slots not in revision["slots"] (use
# SLOTS) --> create empty slots
Expand Down Expand Up @@ -885,7 +884,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 +901,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 +1003,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