Skip to content

Commit cb1cfb3

Browse files
committed
example on copying pages between to OSW instances added
1 parent cb77ceb commit cb1cfb3

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

examples/inter_osw_copy_page.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
"""THis script provides the ability to copy a page from one OSW instance to another
2+
OSW instance."""
3+
from pathlib import Path
4+
5+
from typing_extensions import List, Optional, Union
6+
7+
from osw.auth import CredentialManager
8+
from osw.core import OSW
9+
from osw.model.static import OswBaseModel
10+
from osw.utils import util
11+
from osw.wtsite import SLOTS, WtPage, WtSite
12+
13+
14+
class OswInstance(OswBaseModel):
15+
domain: str
16+
cred_fp: Union[str, Path]
17+
credentials_manager: Optional[CredentialManager]
18+
osw: Optional[OSW]
19+
wtsite: Optional[WtSite]
20+
21+
class Config:
22+
arbitrary_types_allowed = True
23+
24+
def __init__(self, domain: str, cred_fp: Union[str, Path]):
25+
super().__init__(**{"domain": domain, "cred_fp": cred_fp})
26+
self.credentials_manager = CredentialManager(cred_filepath=cred_fp)
27+
self.osw = OSW(
28+
site=WtSite(
29+
WtSite.WtSiteConfig(iri=domain, cred_mngr=self.credentials_manager)
30+
)
31+
)
32+
self.wtsite = self.osw.site
33+
34+
def get_page_content(self, full_page_titles: List[str]) -> dict:
35+
get_page_res: WtSite.GetPageResult = self.wtsite.get_page(
36+
WtSite.GetPageParam(titles=full_page_titles)
37+
)
38+
39+
return_dict = {}
40+
for page in get_page_res.pages:
41+
title = page.title
42+
slot_contents = {}
43+
for slot in SLOTS:
44+
slot_content = page.get_slot_content(slot)
45+
if slot_content is not None:
46+
slot_contents[slot] = slot_content
47+
return_dict[title] = slot_contents
48+
49+
return return_dict
50+
51+
def set_single_page_content(
52+
self,
53+
handover_dict: dict,
54+
comment: str,
55+
overwrite: bool = False,
56+
):
57+
full_page_title: str = handover_dict["full_page_title"]
58+
content_dict: dict = handover_dict["content_dict"]
59+
wtpage = WtPage(
60+
wtSite=self.wtsite,
61+
title=full_page_title,
62+
)
63+
if wtpage.exists:
64+
if overwrite is False:
65+
print(
66+
f"Page '{full_page_title}' already exists. It will not be updated."
67+
)
68+
return {full_page_title: False}
69+
print(f"Page '{full_page_title}' already exists. It will be updated.")
70+
for slot in content_dict.keys():
71+
wtpage.create_slot(
72+
slot_key=slot,
73+
content_model=SLOTS[slot]["content_model"],
74+
)
75+
wtpage.set_slot_content(
76+
slot_key=slot,
77+
content=content_dict[slot],
78+
)
79+
wtpage.edit(
80+
comment=comment,
81+
)
82+
print(f"Page updated for 'https://{self.domain}/wiki/{full_page_title}'")
83+
return {full_page_title: True}
84+
85+
def set_page_contents(
86+
self, content_list: List[dict], comment: str, overwrite: bool = False
87+
) -> list:
88+
result_list = util.parallelize(
89+
self.set_single_page_content,
90+
content_list,
91+
comment=comment,
92+
overwrite=overwrite,
93+
flush_at_end=True,
94+
)
95+
return result_list
96+
97+
98+
def copy_pages_from(
99+
source_domain: str,
100+
to_target_domains: List[str],
101+
page_titles: List[str],
102+
cred_fp: Union[str, Path],
103+
comment: str = None,
104+
overwrite: bool = False,
105+
):
106+
if comment is None:
107+
f"[bot edit] Copied from {source_domain}"
108+
osw_source = OswInstance(
109+
domain=source_domain,
110+
cred_fp=cred_fp,
111+
)
112+
osw_targets = [
113+
OswInstance(
114+
domain=domain,
115+
cred_fp=cred_fp,
116+
)
117+
for domain in to_target_domains
118+
]
119+
page_contents = osw_source.get_page_content(full_page_titles=page_titles)
120+
result = {}
121+
for osw_target in osw_targets: # could also be parallelized!
122+
result[osw_target.domain] = osw_target.set_page_contents(
123+
content_list=[
124+
{
125+
"full_page_title": full_page_title,
126+
"content_dict": page_content,
127+
}
128+
for full_page_title, page_content in page_contents.items()
129+
],
130+
comment=comment,
131+
overwrite=overwrite,
132+
)
133+
return result
134+
135+
136+
if __name__ == "__main__":
137+
credentials_fp = Path(r"accounts.pwd.yaml")
138+
source = "onto-wiki.eu"
139+
targets = ["wiki-dev.open-semantic-lab.org"]
140+
titles = [
141+
"Item:OSW8dca6aaebe005c5faca05bac33264e4d",
142+
"Item:OSWaeffcee25ccb5dd8b42a434dc644d62c",
143+
]
144+
copied_pages = copy_pages_from(
145+
source_domain=source,
146+
to_target_domains=targets,
147+
page_titles=titles,
148+
cred_fp=credentials_fp,
149+
overwrite=False,
150+
)

0 commit comments

Comments
 (0)