From a01f6d49ce2711059d2fdb5a2f9d7432943cf5c5 Mon Sep 17 00:00:00 2001 From: Lukas Gold Date: Thu, 16 Nov 2023 17:23:21 +0100 Subject: [PATCH 1/3] Using credentials manager --- examples/gui_local_slot_editing.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/examples/gui_local_slot_editing.py b/examples/gui_local_slot_editing.py index 56959d50..d35e42ad 100644 --- a/examples/gui_local_slot_editing.py +++ b/examples/gui_local_slot_editing.py @@ -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 @@ -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] @@ -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("") From 1818726363bdc3d0c5266f7276ebecd3460f5054 Mon Sep 17 00:00:00 2001 From: Lukas Gold Date: Thu, 16 Nov 2023 17:25:31 +0100 Subject: [PATCH 2/3] trying to allow non ascii-characters to be saved to file (json_data and json_schema slots) Line 743 in wtsite.py makes the the page download fail --- src/osw/wtsite.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/osw/wtsite.py b/src/osw/wtsite.py index f428cacb..75fbcefc 100644 --- a/src/osw/wtsite.py +++ b/src/osw/wtsite.py @@ -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(): @@ -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): @@ -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( @@ -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"): @@ -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 @@ -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( @@ -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"), @@ -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: From 24518086404fa7ebd06709f6257b4e48e8e7c0b0 Mon Sep 17 00:00:00 2001 From: Lukas Gold Date: Thu, 16 Nov 2023 17:47:08 +0100 Subject: [PATCH 3/3] Fixing the page load bug --- src/osw/wtsite.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/osw/wtsite.py b/src/osw/wtsite.py index 75fbcefc..55f1f26e 100644 --- a/src/osw/wtsite.py +++ b/src/osw/wtsite.py @@ -742,9 +742,6 @@ def __init__(self, wtSite: WtSite = None, title: str = None): if self._content_model[slot_key] == "json": self._slots[slot_key] = json.loads( 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