Skip to content

Commit 1b03808

Browse files
authored
Update gui local slot editing (#48)
* Using credentials manager * trying to allow non ascii-characters to be saved to file (json_data and json_schema slots) Line 743 in wtsite.py makes the page download fail * Fixing the page load bug
1 parent 6727ec6 commit 1b03808

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

examples/gui_local_slot_editing.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from numpy import array as np_array
1212

1313
import osw.model.page_package as package
14+
from osw.auth import CredentialManager
1415
from osw.core import OSW
1516
from osw.wiki_tools import read_domains_from_credentials_file
1617
from osw.wtsite import SLOTS, WtPage, WtSite
@@ -164,10 +165,9 @@ def save_as_page_package(
164165
if settings_read_from_file:
165166
settings["domain"] = domain
166167

167-
wtsite_obj = WtSite.from_domain(
168-
domain=domain, password_file="", credentials=accounts[domain]
169-
)
170-
osw_obj = OSW(site=wtsite_obj)
168+
cm = CredentialManager(cred_filepath=settings["credentials_file_path"])
169+
osw_obj = OSW(site=WtSite(WtSite.WtSiteConfig(iri=domain, cred_mngr=cm)))
170+
wtsite_obj = osw_obj.site
171171

172172
full_page_name = settings["target_page"].split("/")[-1].replace("_", " ")
173173
page = wtsite_obj.get_page(WtSite.GetPageParam(titles=[full_page_name])).pages[0]
@@ -378,12 +378,10 @@ def save_as_page_package(
378378
elif event == "-DOMAIN-":
379379
settings["domain"] = values["-DOMAIN-"]
380380
domain = settings["domain"].split("//")[-1]
381-
wtsite_obj = WtSite.from_domain(
382-
domain=settings["domain"],
383-
password_file="",
384-
credentials=accounts[settings["domain"]],
381+
osw_obj = OSW(
382+
site=WtSite(WtSite.WtSiteConfig(iri=settings["domain"], cred_mngr=cm))
385383
)
386-
osw_obj = OSW(site=wtsite_obj)
384+
wtsite_obj = osw_obj.site
387385
elif event == "Load page":
388386
window["-LABEL-"].update("Loading page...")
389387
window["-DL_RES-"].update("")

src/osw/wtsite.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,6 @@ def create_page_package(self, param: CreatePagePackageParam):
448448
added_titles.append(title)
449449
page = self.get_page(WtSite.GetPageParam(titles=[title])).pages[0]
450450

451-
page = self.get_WtPage(title)
452451
bundle.packages[config.name].pages.append(page.dump(dump_config))
453452
if config.include_files:
454453
for file in page._page.images():
@@ -463,11 +462,11 @@ def create_page_package(self, param: CreatePagePackageParam):
463462
file_page.dump(dump_config)
464463
)
465464

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

473472
class ReadPagePackageParam(model.OswBaseModel):
@@ -552,7 +551,7 @@ def read_page_package(self, param: ReadPagePackageParam) -> ReadPagePackageResul
552551
f"Error: No JSON files found in '{storage_path}'."
553552
)
554553
# Read packages info file
555-
with open(pi_fp, "r") as f:
554+
with open(pi_fp, "r", encoding="utf-8") as f:
556555
packages_json = json.load(f)
557556
# Assume that the pages files are located in the subdir
558557
storage_path_content = ut.list_files_and_directories(
@@ -572,12 +571,12 @@ def get_slot_content(
572571
for pdir in parent_dir:
573572
slot_path = storage_path / pdir / url_path
574573
if slot_path in files_in_storage_path:
575-
with open(slot_path, "r") as f:
574+
with open(slot_path, "r", encoding="utf-8") as f:
576575
file_content = f.read()
577576
# Makes sure not to open an empty file with json
578577
if len(file_content) > 0:
579578
if url_path.endswith(".json"):
580-
with open(slot_path, "r") as f:
579+
with open(slot_path, "r", encoding="utf-8") as f:
581580
slot_data = json.load(f)
582581
return slot_data
583582
elif url_path.endswith(".wikitext"):
@@ -746,7 +745,7 @@ def __init__(self, wtSite: WtSite = None, title: str = None):
746745
# revision["slots"][slot_key]["*"]
747746
if self._content_model[slot_key] == "json":
748747
self._slots[slot_key] = json.loads(
749-
self._slots[slot_key]
748+
self._slots[slot_key],
750749
)
751750
# todo: set content for slots not in revision["slots"] (use
752751
# SLOTS) --> create empty slots
@@ -889,7 +888,7 @@ def _edit(self, comment: str = None, mode="action-multislot"):
889888
content = self._slots[slot_key]
890889
if self._content_model[slot_key] == "json":
891890
if not isinstance(content, str):
892-
content = json.dumps(content)
891+
content = json.dumps(content, ensure_ascii=False)
893892
params["slot_" + slot_key] = content
894893
if changed:
895894
self.wtSite._site.api(
@@ -906,7 +905,7 @@ def _edit(self, comment: str = None, mode="action-multislot"):
906905
if self._slots_changed[slot_key]:
907906
content = self._slots[slot_key]
908907
if self._content_model[slot_key] == "json":
909-
content = json.dumps(content)
908+
content = json.dumps(content, ensure_ascii=False)
910909
self.wtSite._site.api(
911910
"editslot",
912911
token=self.wtSite._site.get_token("csrf"),
@@ -1008,7 +1007,7 @@ def save_to_file(file_path__, content__):
10081007

10091008
def dump_slot_content(slot_key_, content_type_, content_):
10101009
if isinstance(content_, dict):
1011-
content_ = json.dumps(content_, indent=4)
1010+
content_ = json.dumps(content_, indent=4, ensure_ascii=False)
10121011
if content_type_ == "Scribunto":
10131012
content_type_ = "lua"
10141013
if slot_key_ == "main" and config.skip_slot_suffix_for_main:

0 commit comments

Comments
 (0)