|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import re |
| 4 | +from pathlib import Path |
| 5 | +from typing import List, Union |
| 6 | + |
| 7 | +from pydantic.v1 import PrivateAttr, validator |
| 8 | + |
| 9 | +from osw.model.static import OswBaseModel |
| 10 | + |
| 11 | +PACKAGE_ROOT_PATH = Path(__file__).parents[1] |
| 12 | +BASE_PATH = Path.cwd() |
| 13 | +OSW_FILES_DIR_DEFAULT = BASE_PATH / "osw_files" |
| 14 | +DOWNLOAD_DIR_DEFAULT = OSW_FILES_DIR_DEFAULT / "downloads" |
| 15 | +CREDENTIALS_FN_DEFAULT = "credentials.pwd.yaml" |
| 16 | +CREDENTIALS_FP_DEFAULT = OSW_FILES_DIR_DEFAULT / CREDENTIALS_FN_DEFAULT |
| 17 | +WIKI_DOMAIN_DEFAULT = "wiki.open-semantic-lab.org" |
| 18 | + |
| 19 | + |
| 20 | +class FilePathDefault(OswBaseModel): |
| 21 | + """A class to store the default file path. This is a helper class to make the |
| 22 | + default file path, defined within this module, accessible from a calling script.""" |
| 23 | + |
| 24 | + _default: Union[Path] = PrivateAttr(BASE_PATH) |
| 25 | + |
| 26 | + class Config: |
| 27 | + arbitrary_types_allowed = True |
| 28 | + |
| 29 | + def __init__(self, path: Union[str, Path] = BASE_PATH): |
| 30 | + data = {"_default": Path(path) if isinstance(path, str) else path} |
| 31 | + super().__init__(**data) |
| 32 | + |
| 33 | + def __str__(self): |
| 34 | + return str(self.path) |
| 35 | + |
| 36 | + def __repr__(self): |
| 37 | + return f"FilePathDefault(path={self.path})" |
| 38 | + |
| 39 | + def __eq__(self, other): |
| 40 | + other_path = getattr(other, "path", None) |
| 41 | + if other_path is None: |
| 42 | + return False |
| 43 | + |
| 44 | + @property |
| 45 | + def path(self): |
| 46 | + return self._default |
| 47 | + |
| 48 | + def set(self, new: Union[str, Path]): |
| 49 | + self._default = Path(new) if isinstance(new, str) else new |
| 50 | + |
| 51 | + def get(self): |
| 52 | + return self._default |
| 53 | + |
| 54 | + |
| 55 | +class Paths(OswBaseModel): |
| 56 | + """A class to store the default paths. This is a helper class to make the default |
| 57 | + paths, defined within this module, accessible from a calling script.""" |
| 58 | + |
| 59 | + base: Path = BASE_PATH |
| 60 | + """If you want to have the sub folders created in an directory that is not the |
| 61 | + current working directory of the calling script, use Path.base = new_path.""" |
| 62 | + osw_files_dir: Path = OSW_FILES_DIR_DEFAULT |
| 63 | + """If you want to specify the default OSW files directory, use |
| 64 | + Path.osw_files_dir = new_path.""" |
| 65 | + cred_fp: Path = CREDENTIALS_FP_DEFAULT |
| 66 | + """If you want to specify the saving location of the credentials file, use |
| 67 | + Path.cred_fp = new_path.""" |
| 68 | + download_dir: Path = DOWNLOAD_DIR_DEFAULT |
| 69 | + """If you want to specify the default download directory, use |
| 70 | + Path.download_dir = new_path.""" |
| 71 | + _changed: List[str] = PrivateAttr(default_factory=list) |
| 72 | + """A flag to indicate if any of the paths have been changed.""" |
| 73 | + |
| 74 | + def __setattr__(self, name, value): |
| 75 | + old_value = getattr(self, name) |
| 76 | + super().__setattr__(name, value) |
| 77 | + self.on_attribute_set(name, value, old_value) |
| 78 | + if name not in self._changed: |
| 79 | + self._changed.append(name) |
| 80 | + |
| 81 | + def has_changed(self, name): |
| 82 | + return name in self._changed |
| 83 | + |
| 84 | + def on_attribute_set(self, attr_name, new_value, old_value): |
| 85 | + """ |
| 86 | + This method is called every time an attribute is set. It is used to update the |
| 87 | + paths that are dependent on the [base, osw_files_dir] path when the |
| 88 | + [base, osw_files_dir] path is changed. |
| 89 | +
|
| 90 | + Parameters |
| 91 | + ---------- |
| 92 | + attr_name: |
| 93 | + The name of the attribute that was set |
| 94 | + new_value |
| 95 | + The new value the attribute was set to |
| 96 | + old_value |
| 97 | + The value the attribute had before it was set |
| 98 | + """ |
| 99 | + |
| 100 | + def update_attr(set_attr, to_update, old_val, new_val): |
| 101 | + for attr_name in to_update: |
| 102 | + if old_val in getattr(self, attr_name).parents: |
| 103 | + old_rel_path = getattr(self, attr_name).relative_to(old_val) |
| 104 | + new_rel_path = new_val / old_rel_path |
| 105 | + setattr(self, attr_name, new_rel_path) |
| 106 | + print( |
| 107 | + f"Following the setting of {self.__name__}.{set_attr}, " |
| 108 | + f"{self.__name__}.{attr_name} was updated to {new_rel_path}." |
| 109 | + ) |
| 110 | + |
| 111 | + if attr_name == "base": |
| 112 | + update_attr( |
| 113 | + "base", |
| 114 | + ["osw_files_dir", "cred_fp", "download_dir"], |
| 115 | + old_value, |
| 116 | + new_value, |
| 117 | + ) |
| 118 | + elif attr_name == "osw_files_dir": |
| 119 | + update_attr( |
| 120 | + "osw_files_dir", ["cred_fp", "download_dir"], old_value, new_value |
| 121 | + ) |
| 122 | + |
| 123 | + |
| 124 | +# Create an instance of the Paths class to make the default paths accessible from a |
| 125 | +# calling script. |
| 126 | +paths = Paths() |
| 127 | +"""To overwrite a default path, execute, e.g.,: paths.base = new_path""" |
| 128 | + |
| 129 | + |
| 130 | +class Params(OswBaseModel): |
| 131 | + """A class to store the default parameters. This is a helper class to make the |
| 132 | + default parameters, defined within this module, accessible from a calling script.""" |
| 133 | + |
| 134 | + wiki_domain: str = WIKI_DOMAIN_DEFAULT |
| 135 | + """The default domain of the OSW instance to interact with.""" |
| 136 | + _changed: List[str] = PrivateAttr(default_factory=list) |
| 137 | + """A flag to indicate if any of the parameters have been changed.""" |
| 138 | + |
| 139 | + @validator("wiki_domain") |
| 140 | + def validate_wiki_domain(cls, v): |
| 141 | + pattern = r"^(?!-)[A-Za-z0-9.-]{1,63}(?<!-)\.[A-Za-z]{2,}$" |
| 142 | + assert re.match(pattern, v), "The wiki domain is not valid." |
| 143 | + return v |
| 144 | + |
| 145 | + def __setattr__(self, name, value): |
| 146 | + super().__setattr__(name, value) |
| 147 | + if name not in self._changed: |
| 148 | + self._changed.append(name) |
| 149 | + |
| 150 | + def has_changed(self, name): |
| 151 | + return name in self._changed |
| 152 | + |
| 153 | + |
| 154 | +params = Params() |
0 commit comments