Skip to content

Commit e0805b1

Browse files
committed
introducing a getter for private attribute osw.wtsite.WtSite._site
1 parent 31bb784 commit e0805b1

7 files changed

Lines changed: 54 additions & 39 deletions

File tree

‎examples/inter_osw_copy_page.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def copy_pages_from(
185185
target_sites = [osw_target.wtsite for osw_target in osw_targets]
186186
result = {}
187187
for target_site in target_sites:
188-
target = target_site._site.host
188+
target = target_site.mw_site.host
189189
copied_pages = target_site.copy_pages(
190190
WtSite.CopyPagesParam(
191191
source_site=source_site,

‎scripts/migration/file_page_migration.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def get_file_info_and_usage_single(
7272
wtsite_obj: WtSite, page_title: str, debug: bool = False
7373
) -> dict:
7474
result = wt.get_file_info_and_usage(
75-
site=wtsite_obj._site,
75+
site=wtsite_obj.mw_site,
7676
title=wt.SearchParam(
7777
query=page_title,
7878
debug=debug,
@@ -92,7 +92,7 @@ def get_file_info_and_usage_extended(
9292
parallel: bool = True,
9393
) -> List[dict]:
9494
result = wt.get_file_info_and_usage(
95-
site=wtsite_obj._site,
95+
site=wtsite_obj.mw_site,
9696
title=wt.SearchParam(
9797
query=page_titles,
9898
debug=debug,

‎src/osw/controller/file/wiki.py‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def from_other(
3434

3535
def get(self) -> IO:
3636
self._init()
37-
file = self.osw.site._site.images[self.title]
37+
file = self.osw.mw_site.images[self.title]
3838
# return file.download() # in-memory - limited by available RAM
3939

4040
# use web api
@@ -43,13 +43,13 @@ def get(self) -> IO:
4343
response = None
4444
try:
4545
url = (
46-
f"{self.osw.site._site.scheme}://"
47-
f"{self.osw.site._site.host}"
48-
f"{self.osw.site._site.path}"
46+
f"{self.osw.mw_site.scheme}://"
47+
f"{self.osw.mw_site.host}"
48+
f"{self.osw.mw_site.path}"
4949
f"api.php?action=download&format=json&title={full_title}"
5050
)
5151
# print("Use web api: ", url)
52-
response = self.osw.site._site.connection.get(url, stream=True)
52+
response = self.osw.mw_site.connection.get(url, stream=True)
5353
api_error = response.headers.get("Mediawiki-Api-Error")
5454
if api_error is not None:
5555
if api_error == "download-notfound":
@@ -72,7 +72,7 @@ def get(self) -> IO:
7272
"download from ",
7373
url,
7474
)
75-
response = self.osw.site._site.connection.get(url, stream=True)
75+
response = self.osw.mw_site.connection.get(url, stream=True)
7676

7777
if response.status_code != 200:
7878
raise Exception(
@@ -89,10 +89,10 @@ def get(self) -> IO:
8989

9090
def get_to(self, other: "FileController"):
9191
self._init()
92-
file = self.osw.site._site.images[self.title]
92+
file = self.osw.mw_site.images[self.title]
9393
# return file.download() # in-memory - limited by available RAM
9494

95-
with self.osw.site._site.connection.get(
95+
with self.osw.mw_site.connection.get(
9696
file.imageinfo["url"], stream=True
9797
) as response:
9898
# for chunk in response.iter_content(1024):
@@ -136,7 +136,7 @@ def put(self, file: IO, **kwargs: Dict[str, Any]):
136136
**se_params,
137137
)
138138
)
139-
self.osw.site._site.upload(
139+
self.osw.mw_site.upload(
140140
file=file,
141141
filename=self.title,
142142
# comment="",
@@ -165,7 +165,7 @@ def delete(self):
165165
@property
166166
def url(self):
167167
return (
168-
f"{self.osw.site._site.scheme}://{self.osw.site._site.host}"
168+
f"{self.osw.mw_site.scheme}://{self.osw.mw_site.host}"
169169
f"/wiki/File:{self.title}"
170170
)
171171

‎src/osw/core.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import rdflib
1616
from jsonpath_ng.ext import parse
17+
from mwclient.client import Site
1718
from pydantic.v1 import BaseModel, PrivateAttr, create_model, validator
1819
from pyld import jsonld
1920

@@ -86,6 +87,11 @@ class Config:
8687

8788
site: WtSite
8889

90+
@property
91+
def mw_site(self) -> Site:
92+
"""Returns the mwclient Site object of the OSW instance."""
93+
return self.site.mw_site
94+
8995
@staticmethod
9096
def get_osw_id(uuid: uuid) -> str:
9197
"""Generates a OSW-ID based on the given uuid by prefixing "OSW" and removing
@@ -427,7 +433,7 @@ def _fetch_schema(self, fetchSchemaParam: _FetchSchemaParam = None) -> None:
427433

428434
jsonpath_expr = parse("$..dollarref")
429435
for match in jsonpath_expr.find(schema):
430-
# value = "https://" + self.site._site.host + match.value
436+
# value = "https://" + self.mw_site.host + match.value
431437
if match.value.startswith("#"):
432438
continue # skip self references
433439
ref_schema_title = match.value.replace("/wiki/", "").split("?")[0]

‎src/osw/express.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def __exit__(self):
117117

118118
def close_connection(self):
119119
"""Close the connection to the OSL instance."""
120-
self.site._site.connection.close()
120+
self.mw_site.connection.close()
121121

122122
def shut_down(self):
123123
"""Makes sure this OSL instance can't be reused after it was shut down,

‎src/osw/wtsite.py‎

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def __init__(self, config: Union[WtSiteConfig, WtSiteLegacyConfig]):
8989
scheme = "https"
9090

9191
if isinstance(config, WtSite.WtSiteLegacyConfig):
92-
self._site = config.site
92+
self._site: mwclient.Site = config.site
9393
else:
9494
cred = config.cred_mngr.get_credential(
9595
CredentialManager.CredentialConfig(
@@ -144,6 +144,11 @@ def __init__(self, config: Union[WtSiteConfig, WtSiteLegacyConfig]):
144144
self._page_cache = {}
145145
self._cache_enabled = False
146146

147+
@property
148+
def mw_site(self) -> mwclient.client.Site:
149+
"""Returns the mwclient.Site object of the WtSite instance"""
150+
return self._site
151+
147152
@classmethod
148153
@deprecated("Use contructor instead")
149154
def from_domain(
@@ -1175,15 +1180,15 @@ def __init__(self, wtSite: WtSite = None, title: str = None, do_init: bool = Tru
11751180
def init(self):
11761181
"""Initializes the page by loading the content and meta data from the site"""
11771182
# inits the mwclient object - triggers an API call
1178-
self._page = self.wtSite._site.pages[self.title]
1183+
self._page = self.wtSite.mw_site.pages[self.title]
11791184
self.exists = self._page.exists
11801185

11811186
if self.exists:
11821187
self._original_content = self._page.text()
11831188
self._content = self._original_content
11841189
# multi content revisions
11851190
# second API call - ToDo: combine / replace with first call
1186-
rev = self.wtSite._site.api(
1191+
rev = self.wtSite.mw_site.api(
11871192
"query",
11881193
prop="revisions",
11891194
titles=self.title,
@@ -1381,7 +1386,7 @@ def get_url(self) -> str:
13811386
-------
13821387
the URL of the page
13831388
"""
1384-
return "https://" + self.wtSite._site.host + "/wiki/" + self.title
1389+
return "https://" + self.wtSite.mw_site.host + "/wiki/" + self.title
13851390

13861391
def is_file_page(self) -> bool:
13871392
"""Checks if this page is a file page (containing an image, pdf, etc.)
@@ -1557,9 +1562,9 @@ def _edit(self, comment: str = None, mode="action-multislot"):
15571562
params["slot_" + slot_key] = content
15581563
if changed:
15591564
self.changed = True
1560-
self.wtSite._site.api(
1565+
self.wtSite.mw_site.api(
15611566
"editslots",
1562-
token=self.wtSite._site.get_token("csrf"),
1567+
token=self.wtSite.mw_site.get_token("csrf"),
15631568
title=self.title,
15641569
summary=comment,
15651570
**params,
@@ -1574,9 +1579,9 @@ def _edit(self, comment: str = None, mode="action-multislot"):
15741579
content = self._slots[slot_key]
15751580
if self._content_model[slot_key] == "json":
15761581
content = json.dumps(content, ensure_ascii=False)
1577-
self.wtSite._site.api(
1582+
self.wtSite.mw_site.api(
15781583
"editslot",
1579-
token=self.wtSite._site.get_token("csrf"),
1584+
token=self.wtSite.mw_site.get_token("csrf"),
15801585
title=self.title,
15811586
slot=slot_key,
15821587
text=content,
@@ -1655,7 +1660,7 @@ class Config:
16551660

16561661
def copy(self, config: CopyPageConfig) -> PageCopyResult:
16571662
if config.comment is None:
1658-
config.comment = f"[bot edit] Copied from {config.source_site._site.host}"
1663+
config.comment = f"[bot edit] Copied from {config.source_site.mw_site.host}"
16591664
result = config.source_site.get_page_content([config.existing_page])
16601665
for title, slot_contents in result.contents.items():
16611666
self.title = title
@@ -1685,10 +1690,14 @@ def copy(self, config: CopyPageConfig) -> PageCopyResult:
16851690
)
16861691
self.set_slot_content(slot_key=slot, content=slot_contents[slot])
16871692
self.edit(comment=config.comment)
1688-
s2p = f"Page {verb}: 'https://{self.wtSite._site.host}/wiki/{self.title}'."
1693+
s2p = (
1694+
f"Page {verb}: "
1695+
f"'https://{self.wtSite.mw_site.host}/wiki/{self.title}'."
1696+
)
16891697
if verb == "updated":
16901698
s2p = (
1691-
f"Page {verb}: 'https://{self.wtSite._site.host}/w/index.php?title"
1699+
f"Page {verb}: "
1700+
f"'https://{self.wtSite.mw_site.host}/w/index.php?title"
16921701
f"={self.title}&action=history'."
16931702
)
16941703
print(s2p)
@@ -1800,7 +1809,7 @@ def dump_slot_content(slot_key_, content_type_, content_):
18001809
dump_slot_content(slot_key, content_type, content)
18011810

18021811
if self.is_file_page():
1803-
file = self.wtSite._site.images[self.title.split(":")[-1]]
1812+
file = self.wtSite.mw_site.images[self.title.split(":")[-1]]
18041813
file_name = f"{page_name}"
18051814
file_path = os.path.join(tar_dir, *file_name.split("/")) # handle subpages
18061815
# The following will return KeyError "url" if the file is not found
@@ -1826,7 +1835,7 @@ def get_file_info_and_usage(
18261835
and 'usage'.
18271836
"""
18281837
return wt.get_file_info_and_usage(
1829-
site=self.wtSite._site,
1838+
site=self.wtSite.mw_site,
18301839
title=wt.SearchParam(query=self.title, debug=debug),
18311840
)[0]
18321841

@@ -1913,25 +1922,25 @@ def export_xml(self, config: Optional[ExportConfig] = None) -> ExportResult:
19131922
if config is None:
19141923
config = WtPage.ExportConfig()
19151924
url = (
1916-
self.wtSite._site.scheme
1925+
self.wtSite.mw_site.scheme
19171926
+ "://"
1918-
+ self.wtSite._site.host
1919-
+ self.wtSite._site.path
1927+
+ self.wtSite.mw_site.host
1928+
+ self.wtSite.mw_site.path
19201929
+ "index.php?title=Special:Export/"
19211930
+ self.title
19221931
)
19231932
data = {
19241933
"title": "Special:Export",
19251934
"catname": "",
19261935
"pages": self.title,
1927-
"wpEditToken": self.wtSite._site.get_token("csrf"),
1936+
"wpEditToken": self.wtSite.mw_site.get_token("csrf"),
19281937
"wpDownload": "1",
19291938
}
19301939
if not config.full_history:
19311940
data["curonly"] = "1"
19321941
if config.include_templates:
19331942
data["templates"] = "1"
1934-
response = self.wtSite._site.connection.post(url, data=data)
1943+
response = self.wtSite.mw_site.connection.post(url, data=data)
19351944
if response.status_code != 200:
19361945
return WtPage.ExportResult(success=False, xml="")
19371946
else:
@@ -2008,17 +2017,17 @@ def import_xml(self, config: ImportConfig) -> ImportResult:
20082017
)
20092018

20102019
api_url = (
2011-
self.wtSite._site.scheme
2020+
self.wtSite.mw_site.scheme
20122021
+ "://"
2013-
+ self.wtSite._site.host
2014-
+ self.wtSite._site.path
2022+
+ self.wtSite.mw_site.host
2023+
+ self.wtSite.mw_site.path
20152024
+ "api.php"
20162025
)
2017-
response = self.wtSite._site.connection.post(
2026+
response = self.wtSite.mw_site.connection.post(
20182027
url=api_url,
20192028
data={
20202029
"action": "import",
2021-
"token": self.wtSite._site.get_token("csrf"),
2030+
"token": self.wtSite.mw_site.get_token("csrf"),
20222031
"fullhistory": "1" if config.full_history else "0",
20232032
"templates": "1" if config.include_templates else "0",
20242033
"assignknownusers": "1",

‎tests/integration/test_file_page_migration.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def test_file_page_migration(wiki_domain: str, wiki_username: str, wiki_password
6565
if file_page.exists:
6666
file_page.delete()
6767
with open(path_to_test_file, "r") as file:
68-
_ = wtsite._site.upload(
68+
_ = wtsite.mw_site.upload(
6969
file=file,
7070
filename=file_page_name,
7171
comment="File for testing the file_page_migration.py script. "

0 commit comments

Comments
 (0)