Skip to content

Commit 0f30549

Browse files
committed
Moving some functions from OswExpress to OSW
1 parent 0ec5036 commit 0f30549

3 files changed

Lines changed: 84 additions & 77 deletions

File tree

‎src/osw/core.py‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from pyld import jsonld
2222

2323
import osw.model.entity as model
24+
from osw.defaults import params as default_params
2425
from osw.model.static import OswBaseModel
2526
from osw.utils.templates import (
2627
compile_handlebars_template,
@@ -94,6 +95,10 @@ def mw_site(self) -> Site:
9495
"""Returns the mwclient Site object of the OSW instance."""
9596
return self.site.mw_site
9697

98+
def close_connection(self):
99+
"""Close the connection to the OSL instance."""
100+
self.mw_site.connection.close()
101+
97102
@staticmethod
98103
def get_osw_id(uuid: Union[str, UUID]) -> str:
99104
"""Generates a OSW-ID based on the given uuid by prefixing "OSW" and removing
@@ -617,6 +622,81 @@ def _fetch_schema(self, fetchSchemaParam: _FetchSchemaParam = None) -> None:
617622
if not site_cache_state:
618623
self.site.disable_cache() # restore original state
619624

625+
def install_dependencies(
626+
self,
627+
dependencies: Dict[str, str] = None,
628+
mode: str = "append",
629+
policy: str = "force",
630+
):
631+
"""Installs data models, listed in the dependencies, in the osw.model.entity
632+
module.
633+
634+
Parameters
635+
----------
636+
dependencies
637+
A dictionary with the keys being the names of the dependencies and the
638+
values being the full page name (IRI) of the dependencies.
639+
mode
640+
The mode to use when loading the dependencies. Default is 'append',
641+
which will keep existing data models and only load the missing ones. The
642+
mode 'replace' will replace all existing data models with the new ones.
643+
policy
644+
The policy to use when loading the dependencies. Default is 'force',
645+
which will always load the dependencies. If policy is 'if-missing',
646+
dependencies will only be loaded if they are not already installed.
647+
This may lead to outdated dependencies, if the dependencies have been
648+
updated on the server. If policy is 'if-outdated', dependencies will only
649+
be loaded if they were updated on the server. (not implemented yet)
650+
"""
651+
if dependencies is None:
652+
if default_params.dependencies is None:
653+
raise ValueError(
654+
"No 'dependencies' parameter was passed to "
655+
"install_dependencies() and "
656+
"osw.defaults.params.dependencies was not set!"
657+
)
658+
dependencies = default_params.dependencies
659+
schema_fpts = []
660+
for k, v in dependencies.items():
661+
if policy != "if-missing" or not hasattr(model, k):
662+
schema_fpts.append(v)
663+
if policy == "if-outdated":
664+
raise NotImplementedError(
665+
"The policy 'if-outdated' is not implemented yet."
666+
)
667+
schema_fpts = list(set(schema_fpts))
668+
for schema_fpt in schema_fpts:
669+
if not schema_fpt.count(":") == 1:
670+
raise ValueError(
671+
f"Full page title '{schema_fpt}' does not have the correct format. "
672+
"It should be 'Namespace:Name'."
673+
)
674+
self.fetch_schema(OSW.FetchSchemaParam(schema_title=schema_fpts, mode=mode))
675+
676+
@staticmethod
677+
def check_dependencies(dependencies: Dict[str, str]) -> List[str]:
678+
"""Check if the dependencies are installed in the osw.model.entity module.
679+
680+
Parameters
681+
----------
682+
dependencies
683+
A dictionary with the keys being the names of the dependencies and the
684+
values being the full page name (IRI) of the dependencies.
685+
"""
686+
return [dep for dep in dependencies if not hasattr(model, dep)]
687+
688+
def ensure_dependencies(self, dependencies: Dict[str, str]):
689+
"""Ensure that the dependencies are installed in the osw.model.entity module.
690+
691+
Parameters
692+
----------
693+
dependencies
694+
A dictionary with the keys being the names of the dependencies and the
695+
values being the full page name (IRI) of the dependencies.
696+
"""
697+
if self.check_dependencies(dependencies):
698+
self.install_dependencies(dependencies)
699+
620700
class LoadEntityParam(BaseModel):
621701
"""Param for load_entity()"""
622702

‎src/osw/defaults.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ class Params(Defaults):
152152

153153
wiki_domain: str = WIKI_DOMAIN_DEFAULT
154154
"""The default domain of the OSW instance to interact with."""
155+
dependencies: dict = None
156+
"""The default dependencies to install."""
155157

156158
@validator("wiki_domain")
157159
def validate_wiki_domain(cls, v):

‎src/osw/express.py‎

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
# "RemoteFile": "Category:OSW05b244d0a669436e96fe4e1631d5a171", # depends on File
4848
"WikiFile": "Category:OSW11a53cdfbdc24524bf8ac435cbf65d9d", # depends on RemoteFile
4949
}
50+
# Setting dependencies as default
51+
default_params.dependencies = DEPENDENCIES
5052

5153

5254
class OswExpress(OSW):
@@ -158,85 +160,12 @@ def __exit__(self):
158160
"""Close the connection to the OSL instance when exiting the context manager."""
159161
self.close_connection()
160162

161-
def close_connection(self):
162-
"""Close the connection to the OSL instance."""
163-
self.mw_site.connection.close()
164-
165163
def shut_down(self):
166164
"""Makes sure this OSL instance can't be reused after it was shut down,
167165
as the connection can't be reopened except when initializing a new instance."""
168166
self.close_connection()
169167
del self
170168

171-
def install_dependencies(
172-
self,
173-
dependencies: Dict[str, str] = None,
174-
mode: str = "append",
175-
policy: str = "force",
176-
):
177-
"""Installs data models, listed in the dependencies, in the osw.model.entity
178-
module.
179-
180-
Parameters
181-
----------
182-
dependencies
183-
A dictionary with the keys being the names of the dependencies and the
184-
values being the full page name (IRI) of the dependencies.
185-
mode
186-
The mode to use when loading the dependencies. Default is 'append',
187-
which will keep existing data models and only load the missing ones. The
188-
mode 'replace' will replace all existing data models with the new ones.
189-
policy
190-
The policy to use when loading the dependencies. Default is 'force',
191-
which will always load the dependencies. If policy is 'if-missing',
192-
dependencies will only be loaded if they are not already installed.
193-
This may lead to outdated dependencies, if the dependencies have been
194-
updated on the server. If policy is 'if-outdated', dependencies will only
195-
be loaded if they were updated on the server. (not implemented yet)
196-
"""
197-
if dependencies is None:
198-
dependencies = DEPENDENCIES
199-
schema_fpts = []
200-
for k, v in dependencies.items():
201-
if policy != "if-missing" or not hasattr(model, k):
202-
schema_fpts.append(v)
203-
if policy == "if-outdated":
204-
raise NotImplementedError(
205-
"The policy 'if-outdated' is not implemented yet."
206-
)
207-
schema_fpts = list(set(schema_fpts))
208-
for schema_fpt in schema_fpts:
209-
if not schema_fpt.count(":") == 1:
210-
raise ValueError(
211-
f"Full page title '{schema_fpt}' does not have the correct format. "
212-
"It should be 'Namespace:Name'."
213-
)
214-
self.fetch_schema(OSW.FetchSchemaParam(schema_title=schema_fpts, mode=mode))
215-
216-
@staticmethod
217-
def check_dependencies(dependencies: Dict[str, str]) -> List[str]:
218-
"""Check if the dependencies are installed in the osw.model.entity module.
219-
220-
Parameters
221-
----------
222-
dependencies
223-
A dictionary with the keys being the names of the dependencies and the
224-
values being the full page name (IRI) of the dependencies.
225-
"""
226-
return [dep for dep in dependencies if not hasattr(model, dep)]
227-
228-
def ensure_dependencies(self, dependencies: Dict[str, str]):
229-
"""Ensure that the dependencies are installed in the osw.model.entity module.
230-
231-
Parameters
232-
----------
233-
dependencies
234-
A dictionary with the keys being the names of the dependencies and the
235-
values being the full page name (IRI) of the dependencies.
236-
"""
237-
if self.check_dependencies(dependencies):
238-
self.install_dependencies(dependencies)
239-
240169
def download_file(
241170
self,
242171
url_or_title: str,
@@ -1037,10 +966,6 @@ def import_with_fallback(
1037966

1038967
OswExpress.update_forward_refs()
1039968

1040-
# todo:
1041-
# * create a .gitignore in the basepath that lists the default credentials file (
1042-
# accounts.pwd.yaml) OR append to an existing .gitignore#
1043-
1044969
# todo:
1045970
# Ideas:
1046971
# * New express function:

0 commit comments

Comments
 (0)