|
21 | 21 | from pyld import jsonld |
22 | 22 |
|
23 | 23 | import osw.model.entity as model |
| 24 | +from osw.defaults import params as default_params |
24 | 25 | from osw.model.static import OswBaseModel |
25 | 26 | from osw.utils.templates import ( |
26 | 27 | compile_handlebars_template, |
@@ -94,6 +95,10 @@ def mw_site(self) -> Site: |
94 | 95 | """Returns the mwclient Site object of the OSW instance.""" |
95 | 96 | return self.site.mw_site |
96 | 97 |
|
| 98 | + def close_connection(self): |
| 99 | + """Close the connection to the OSL instance.""" |
| 100 | + self.mw_site.connection.close() |
| 101 | + |
97 | 102 | @staticmethod |
98 | 103 | def get_osw_id(uuid: Union[str, UUID]) -> str: |
99 | 104 | """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: |
617 | 622 | if not site_cache_state: |
618 | 623 | self.site.disable_cache() # restore original state |
619 | 624 |
|
| 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 | + |
620 | 700 | class LoadEntityParam(BaseModel): |
621 | 701 | """Param for load_entity()""" |
622 | 702 |
|
|
0 commit comments