Skip to content

Commit a0fce17

Browse files
committed
Making basic default functionality reusable
1 parent d8e4384 commit a0fce17

1 file changed

Lines changed: 27 additions & 34 deletions

File tree

‎src/osw/defaults.py‎

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
from osw.model.static import OswBaseModel
1010

11-
PACKAGE_ROOT_PATH = Path(__file__).parents[1]
11+
PACKAGE_ROOT_PATH = Path(__file__).parents[2]
12+
SRC_PATH = PACKAGE_ROOT_PATH / "src"
1213
BASE_PATH = Path.cwd()
1314
OSW_FILES_DIR_DEFAULT = BASE_PATH / "osw_files"
1415
DOWNLOAD_DIR_DEFAULT = OSW_FILES_DIR_DEFAULT / "downloads"
@@ -52,7 +53,30 @@ def get(self):
5253
return self._default
5354

5455

55-
class Paths(OswBaseModel):
56+
class Defaults(OswBaseModel):
57+
"""Helper class to create an inheriting classes for storing default values."""
58+
59+
_changed: List[str] = PrivateAttr(default_factory=list)
60+
"""A flag to indicate if any of the paths have been changed."""
61+
62+
def __init__(self, **data):
63+
super().__init__(**data)
64+
self._changed = [key for key in data.keys() if key != "_changed"]
65+
66+
def __setattr__(self, name, value):
67+
super().__setattr__(name, value)
68+
if name != "_changed" and name not in self._changed:
69+
self._changed.append(name)
70+
71+
def has_changed(self, name):
72+
return name in self._changed
73+
74+
@property
75+
def changed(self):
76+
return self._changed
77+
78+
79+
class Paths(Defaults):
5680
"""A class to store the default paths. This is a helper class to make the default
5781
paths, defined within this module, accessible from a calling script."""
5882

@@ -68,12 +92,6 @@ class Paths(OswBaseModel):
6892
download_dir: Path = DOWNLOAD_DIR_DEFAULT
6993
"""If you want to specify the default download directory, use
7094
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 __init__(self, **data):
75-
super().__init__(**data)
76-
self._changed = [key for key in data.keys() if key != "_changed"]
7795

7896
def __setattr__(self, name, value):
7997
old_value = getattr(self, name)
@@ -82,13 +100,6 @@ def __setattr__(self, name, value):
82100
if name not in self._changed:
83101
self._changed.append(name)
84102

85-
def has_changed(self, name):
86-
return name in self._changed
87-
88-
@property
89-
def changed(self):
90-
return self._changed
91-
92103
def on_attribute_set(self, attr_name, new_value, old_value):
93104
"""
94105
This method is called every time an attribute is set. It is used to update the
@@ -135,36 +146,18 @@ def update_attr(set_attr, to_update, old_val, new_val):
135146
"""To overwrite a default path, execute, e.g.,: paths.base = new_path"""
136147

137148

138-
class Params(OswBaseModel):
149+
class Params(Defaults):
139150
"""A class to store the default parameters. This is a helper class to make the
140151
default parameters, defined within this module, accessible from a calling script."""
141152

142153
wiki_domain: str = WIKI_DOMAIN_DEFAULT
143154
"""The default domain of the OSW instance to interact with."""
144-
_changed: List[str] = PrivateAttr(default_factory=list)
145-
"""A flag to indicate if any of the parameters have been changed."""
146-
147-
def __init__(self, **data):
148-
super().__init__(**data)
149-
self._changed = [key for key in data.keys() if key != "_changed"]
150-
151-
def __setattr__(self, name, value):
152-
super().__setattr__(name, value)
153-
if name != "_changed" and name not in self._changed:
154-
self._changed.append(name)
155155

156156
@validator("wiki_domain")
157157
def validate_wiki_domain(cls, v):
158158
pattern = r"^(?!-)[A-Za-z0-9.-]{1,63}(?<!-)\.[A-Za-z]{2,}$"
159159
assert re.match(pattern, v), "The wiki domain is not valid."
160160
return v
161161

162-
def has_changed(self, name):
163-
return name in self._changed
164-
165-
@property
166-
def changed(self):
167-
return self._changed
168-
169162

170163
params = Params()

0 commit comments

Comments
 (0)