Skip to content

Commit 302a337

Browse files
committed
Introducing osw.defaults and adding the osw.defaults.paths.osw_files_dir to .gitignore programmatically
1 parent 8b97e70 commit 302a337

7 files changed

Lines changed: 232 additions & 131 deletions

File tree

‎examples/use_express_functions.py‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
from pathlib import Path
22

3-
from osw.express import (
4-
OswExpress,
5-
cred_filepath_default,
6-
osw_download_file,
7-
osw_upload_file,
8-
)
3+
from osw.defaults import params as default_params
4+
from osw.defaults import paths as default_paths
5+
6+
# The domain, osw.express will be initialized with
7+
default_params.wiki_domain = "wiki-dev.open-semantic-lab.org"
8+
9+
from osw.express import OswExpress, osw_download_file, osw_upload_file # noqa: E402
910

1011
# (Optional) Set the default credentials filepath to desired location. Otherwise,
1112
# it will use the default location (current working directory)
1213
# cred_filepath_default.set_default(r"C:\Users\gold\ownCloud\Personal\accounts.pwd.yaml")
1314

1415
# Check setting
15-
print(f"Credentials loaded from '{str(cred_filepath_default)}")
16+
print(f"Credentials loaded from '{str(default_paths.cred_fp)}'")
1617

17-
# Create an OswExpress object
18+
# The domain to connect to
1819
domain = "wiki-dev.open-semantic-lab.org"
19-
# domain = "arkeve.test.digital.isc.fraunhofer.de"
20+
# domain = "demo.open-semantic-lab.org"
21+
# Create an OswExpress object
2022
osw_obj = OswExpress(domain=domain)
2123

2224
# Create a file

‎src/osw/auth.py‎

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
from pydantic.v1 import PrivateAttr
1111

1212
from osw.custom_types import PossibleFilePath
13+
from osw.defaults import paths as default_paths
1314
from osw.model.static import OswBaseModel
1415

15-
CREDENTIALS_FN_DEFAULT = "credentials.pwd.yaml"
16-
1716

1817
class CredentialManager(OswBaseModel):
1918
"""Handles credentials"""
@@ -253,15 +252,16 @@ def save_credentials_to_file(
253252
If True, the cred_filepath is set to the given filepath. If False, the
254253
cred_filepath of the CredentialManager is not changed.
255254
"""
256-
filepath_ = [filepath]
255+
cred_filepaths = [filepath]
256+
"""The filepath to save the credentials to."""
257257
if filepath is None:
258-
filepath_ = self.cred_filepath
258+
cred_filepaths = self.cred_filepath
259259
if self.cred_filepath is None:
260-
filepath_ = [Path.cwd() / "osw_files" / CREDENTIALS_FN_DEFAULT]
260+
cred_filepaths = [default_paths.cred_fp]
261261
if set_cred_filepath:
262262
# Creates error if file does not exist -> Using custom FilePath
263-
self.cred_filepath = filepath_
264-
for fp in filepath_:
263+
self.cred_filepath = cred_filepaths
264+
for fp in cred_filepaths:
265265
file = Path(fp)
266266
if not file.parent.exists():
267267
file.parent.mkdir(parents=True)
@@ -286,29 +286,57 @@ def save_credentials_to_file(
286286
cwd / ".gitignore",
287287
cwd.parent / ".gitignore",
288288
]
289-
write_to_fp = potential_fp[0]
289+
gitignore_fp = potential_fp[0]
290+
# Stops if a .gitignore file is found
290291
for fp in potential_fp:
291292
if fp.exists():
292-
write_to_fp = fp
293+
gitignore_fp = fp
293294
break
294-
if not write_to_fp.exists():
295-
if not write_to_fp.parent.exists():
296-
write_to_fp.parent.mkdir(parents=True)
297-
write_to_fp.touch()
298-
with open(write_to_fp, "r") as stream:
295+
# Creates a .gitignore file if none is found
296+
if not gitignore_fp.exists():
297+
if not gitignore_fp.parent.exists():
298+
gitignore_fp.parent.mkdir(parents=True)
299+
gitignore_fp.touch()
300+
# Reads the .gitignore file
301+
with open(gitignore_fp, "r") as stream:
299302
content = stream.read()
300303
comment_set = False
301-
for _ii, fp in enumerate(filepath_):
302-
if fp.name not in content:
303-
print(f"Adding '{fp.name}' to gitignore file '{write_to_fp}'.")
304-
with open(write_to_fp, "a") as stream:
305-
if comment_set:
304+
osw_dir_added = False
305+
# For every file path in the list of credentials file paths
306+
for _ii, fp in enumerate(cred_filepaths):
307+
to_add = ""
308+
if default_paths.osw_files_dir in fp.parents and not osw_dir_added:
309+
print(
310+
f"Adding '{default_paths.osw_files_dir}' to gitignore file "
311+
f"'{gitignore_fp}'."
312+
)
313+
containing_gitignore = gitignore_fp.parent.absolute()
314+
315+
if containing_gitignore in default_paths.osw_files_dir.parents:
316+
# If the default_path.osw_files_dir is a subdirectory of the directory
317+
# containing the .gitignore file, add the relative path to the
318+
# .gitignore file
319+
rel = default_paths.osw_files_dir.relative_to(containing_gitignore)
320+
to_add = f"\n*{str(rel.as_posix())}/*"
321+
else:
322+
# Test if the default_path.osw_files_dir is a subdirectory of the
323+
# directory containing the .gitignore file
324+
to_add = f"\n*{default_paths.osw_files_dir.absolute().as_posix()}/*"
325+
osw_dir_added = True
326+
elif fp.name not in content:
327+
print(f"Adding '{fp.name}' to gitignore file '{gitignore_fp}'.")
328+
to_add = f"\n*{fp.name}"
329+
# If to_add is not empty, write to .gitignore file
330+
if to_add:
331+
with open(gitignore_fp, "a") as stream:
332+
# Only add comment if not already set
333+
if not comment_set:
306334
stream.write(
307335
"\n# Automatically added by osw.auth.CredentialManager."
308336
"save_credentials_to_file:"
309337
)
310338
comment_set = True
311-
stream.write(f"\n*{fp.name}")
339+
stream.write(to_add)
312340

313341

314342
CredentialManager.CredentialConfig.update_forward_refs()

‎src/osw/data/import_utility.py‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,14 @@
1515
from osw import wiki_tools as wt
1616
from osw.auth import CredentialManager
1717
from osw.core import OSW
18+
from osw.defaults import paths as default_paths
1819
from osw.model import entity as model
1920
from osw.utils.regex import MatchResult, RegExPatternExtended
20-
from osw.utils.regex_pattern import REGEX_PATTERN_LIB, REGEX_PATTERN_LIST
21+
from osw.utils.regex_pattern import REGEX_PATTERN_LIB
2122
from osw.wtsite import WtSite
2223

2324
# Constants
24-
PACKAGE_ROOT_PATH = Path(__file__).parents[2]
25-
CREDENTIALS_FILE_PATH_DEFAULT = PACKAGE_ROOT_PATH / "examples" / "accounts.pwd.yaml"
2625
ENABLE_SORTING = True
27-
# For compatibility with the old version of the module
28-
REGEX_PATTERN = {rep.description: rep.dict() for rep in REGEX_PATTERN_LIST}
2926

3027

3128
# Classes
@@ -879,7 +876,7 @@ def translate_list_with_deepl(
879876
) -> dict:
880877
"""Translates a list of strings with DeepL."""
881878
if credentials_file_path is None:
882-
credentials_file_path = CREDENTIALS_FILE_PATH_DEFAULT
879+
credentials_file_path = default_paths.cred_fp
883880
if translations is None:
884881
translations = {}
885882
domains, accounts = wt.read_domains_from_credentials_file(credentials_file_path)

‎src/osw/defaults.py‎

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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

Comments
 (0)