Skip to content

Commit 5ec42f3

Browse files
authored
Merge pull request #82 from OpenSemanticLab/feat-add-schema-install-policy-param
feat: add schema install policy param
2 parents 1e192c8 + 8fb4139 commit 5ec42f3

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

src/osw/express.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,30 @@ def shut_down(self):
193193
# connection can't be reopened except when initializing a new instance)
194194

195195
def install_dependencies(
196-
self, dependencies: Dict[str, str] = None, mode: str = "append"
196+
self,
197+
dependencies: Dict[str, str] = None,
198+
mode: str = "append",
199+
policy: str = "force",
197200
):
198201
"""Expects a dictionary with the keys being the names of the dependencies and
199202
the values being the full page name of the dependencies.
200203
To keep existing dependencies, use mode='append'.
204+
Default policy is 'force', which will always load dependencies.
205+
If policy is 'if-missing', dependencies will only be loaded if they are not already installed.
206+
This may lead to outdated dependencies, if the dependencies have been updated on the server.
207+
If policy is 'if-outdated', dependencies will only be loaded if they were updated on the server.
208+
(not implemented yet)
201209
"""
202210
if dependencies is None:
203211
dependencies = DEPENDENCIES
204-
schema_fpts = list(dependencies.values())
212+
schema_fpts = []
213+
for k, v in dependencies.items():
214+
if policy != "if-missing" or not hasattr(model, k):
215+
schema_fpts.append(v)
216+
if policy == "if-outdated":
217+
raise NotImplementedError(
218+
"The policy 'if-outdated' is not implemented yet."
219+
)
205220
schema_fpts = list(set(schema_fpts))
206221
for schema_fpt in schema_fpts:
207222
if not schema_fpt.count(":") == 1:
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import time
2+
3+
import osw.model.entity as model
4+
from osw.auth import CredentialManager
5+
6+
7+
def test_fetch_and_load(wiki_domain, wiki_username, wiki_password, mocker):
8+
# create a credential file on the default path for osw express
9+
cm = CredentialManager() # cred_filepath = Path.cwd() / "accounts.pwd.yaml")
10+
cm.add_credential(
11+
CredentialManager.UserPwdCredential(
12+
iri=wiki_domain, username=wiki_username, password=wiki_password
13+
)
14+
)
15+
# cm.save_credentials_to_file()
16+
# wtsite = WtSite(WtSite.WtSiteConfig(iri=wiki_domain, cred_mngr=cm))
17+
# osw_obj = OSW(site=wtsite)
18+
19+
# Here the initial connection to the wiki is mocked (passing domain, username and
20+
# password)
21+
mocked_input = mocker.patch("builtins.input")
22+
mocked_getpass = mocker.patch("getpass.getpass")
23+
mocked_input.side_effect = [wiki_domain, wiki_username]
24+
mocked_getpass.return_value = wiki_password
25+
# This import will trigger the install_dependencies method call on the first run
26+
import osw.express
27+
28+
osw_express = osw.express.OswExpress(domain=wiki_domain, cred_mngr=cm)
29+
30+
# Measure the time taken to store and load an entity
31+
start_time = time.time()
32+
33+
# Load Tutorial Schema on demand
34+
# if not hasattr(model, "Tutorial"):
35+
DEPENDENCIES = {"Tutorial": "Category:OSW494f660e6a714a1a9681c517bbb975da"}
36+
osw_express.install_dependencies(DEPENDENCIES, policy="force")
37+
38+
end_time = time.time()
39+
print(f"Time taken to load Tutorial Schema: {end_time - start_time}")
40+
assert hasattr(model, "Tutorial")
41+
assert end_time - start_time < 15 # typically takes 8 seconds
42+
43+
start_time = time.time()
44+
osw_express.install_dependencies(DEPENDENCIES, policy="force")
45+
end_time = time.time()
46+
print(
47+
f"Time taken to reload Tutorial Schema with policy 'force': {end_time - start_time}"
48+
)
49+
assert end_time - start_time < 2 # typically takes 1 seconds using memory cache
50+
51+
start_time = time.time()
52+
osw_express.install_dependencies(DEPENDENCIES, policy="if-missing")
53+
end_time = time.time()
54+
print(
55+
"Time taken to reload Tutorial Schema with policy ",
56+
"'if-missing': {end_time - start_time}",
57+
)
58+
assert end_time - start_time < 0.1 # typically takes 0 seconds

0 commit comments

Comments
 (0)