Skip to content

Commit 8f99363

Browse files
committed
refactor: rework OswExpress init to use env vars and update tests
- Replace default_params-based domain/cred_filepath resolution with environment variable lookups (OSW_/OSL_ prefixed) - Move controller imports to module top level, removing the try/except fallback that triggered interactive dependency installation on import - Add input() prompt for cred_filepath when no env var is set - Fix str-to-Path conversion bug in FileResult.process_init_data - Add env var support for download_dir in DownloadFileResult - Update integration tests to match new init behavior - Pin Python version upper bound to 3.13 (pydantic v1 incompatibility)
1 parent 7191678 commit 8f99363

4 files changed

Lines changed: 107 additions & 116 deletions

File tree

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,6 @@ src/osw/model/*.json
6363
playground
6464

6565
.env
66+
67+
# Automatically added by osw.auth.CredentialManager.save_credentials_to_file:
68+
*/osw_files/*

‎setup.cfg‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ package_dir =
4141
=src
4242

4343
# Require a min/specific Python version (comma-separated conditions)
44-
python_requires = >=3.10
44+
python_requires = >=3.10, <=3.13
4545

4646
# Add here dependencies of your project (line-separated), e.g. requests>=2.2,<3.0.
4747
# Version specifiers like >=2.2,<3.0 avoid problems due to API changes in

‎src/osw/express.py‎

Lines changed: 78 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
import importlib.util
7+
import os
78
import re
89
from io import TextIOWrapper
910
from pathlib import Path
@@ -28,33 +29,19 @@
2829

2930
import osw.model.entity as model
3031
from osw.auth import CredentialManager
32+
from osw.controller.file.base import FileController # depends on File
33+
from osw.controller.file.local import LocalFileController # depends on LocalFile
34+
from osw.controller.file.memory import InMemoryController # depends on LocalFile
35+
from osw.controller.file.wiki import WikiFileController # depends on WikiFile
3136
from osw.core import OSW, OVERWRITE_CLASS_OPTIONS, OverwriteOptions
3237
from osw.defaults import params as default_params
3338
from osw.defaults import paths as default_paths
3439
from osw.utils.wiki import namespace_from_full_title, title_from_full_title
3540
from osw.wtsite import WtSite
3641

37-
# Definition of constants
38-
DEPENDENCIES = {
39-
# "Entity": "Category:Entity", # depends on nothing#
40-
"Category": "Category:Category", # depends on Entity
41-
"Property": "Category:Property", # depends on Entity
42-
# "Item": "Category:Item", # depends on Entity
43-
"Characteristics": "Category:OSW93ccae36243542ceac6c951450a81d47", # depends on Item
44-
# "Data": "Category:OSW2ac4493f8635481eaf1db961b63c8325", # depends on Item
45-
# "File": "Category:OSWff333fd349af4f65a69100405a9e60c7", # depends on Data
46-
"LocalFile": "Category:OSW3e3f5dd4f71842fbb8f270e511af8031", # depends on File
47-
# "RemoteFile": "Category:OSW05b244d0a669436e96fe4e1631d5a171", # depends on File
48-
"WikiFile": "Category:OSW11a53cdfbdc24524bf8ac435cbf65d9d", # depends on RemoteFile
49-
}
50-
# Setting dependencies as default
51-
default_params.dependencies = DEPENDENCIES
52-
5342

5443
class OswExpress(OSW):
55-
"""
56-
This class provides convenience functions for osw-python.
57-
"""
44+
"""This class provides convenience functions for osw-python."""
5845

5946
domain: str
6047
"""The domain of the OSL instance to connect to."""
@@ -74,6 +61,17 @@ def validate_domain(cls, v):
7461
assert re.match(pattern, v), "The domain is not valid."
7562
return v
7663

64+
@overload
65+
def __init__(
66+
self,
67+
domain: str = None,
68+
cred_filepath: Union[str, Path] = None,
69+
cred_mngr: CredentialManager = None,
70+
) -> None: ...
71+
72+
# case: nothing specified, all parameters to be loaded from .env file or
73+
# environment parameters
74+
7775
@overload
7876
def __init__(
7977
self,
@@ -82,31 +80,54 @@ def __init__(
8280
cred_mngr: CredentialManager = None,
8381
) -> None: ...
8482

83+
# case: domain is specified, rest is to be loaded from .env file or environment
84+
# parameters
85+
8586
def __init__(
8687
self,
8788
domain: str = None,
8889
cred_filepath: Union[str, Path] = None,
8990
cred_mngr: CredentialManager = None,
9091
):
9192
if domain is None:
92-
if default_params.has_changed("wiki_domain"):
93-
domain = default_params.wiki_domain
93+
if os.getenv("OSW_WIKI_DOMAIN") is not None:
94+
domain = os.getenv("OSW_WIKI_DOMAIN")
95+
elif os.getenv("OSL_WIKI_DOMAIN") is not None:
96+
domain = os.getenv("OSL_WIKI_DOMAIN")
9497
else:
9598
raise TypeError(
96-
"The constructor of OswExpress is missing 1 required positional argument: 'domain'." # noqa: E501
97-
"\nIf no domain was set via "
98-
"osw.defaults.params.wiki_domain = <domain>, "
99-
"'domain' is a required argument."
99+
"The constructor of OswExpress is missing 1 required positional "
100+
"argument: 'domain'."
101+
"\nIf no domain was set as environmental variable, 'domain' is a "
102+
"required argument."
100103
)
101104
if cred_filepath is None:
102-
# Set default
103-
cred_filepath = default_paths.cred_filepath
105+
if os.getenv("OSW_CRED_FILEPATH") is not None:
106+
cred_filepath = os.getenv("OSW_CRED_FILEPATH")
107+
elif os.getenv("OSL_CRED_FILEPATH") is not None:
108+
cred_filepath = os.getenv("OSL_CRED_FILEPATH")
109+
else:
110+
# prompt user to set cred_filepath
111+
cred_filepath = input(
112+
"No credential file path was provided. Please specify, where to "
113+
"save the credential file: "
114+
)
115+
print(
116+
f"Credential file path changed to '{cred_filepath}'. Please set "
117+
f"environment variable 'OSW_CRED_FILEPATH' accordingly."
118+
)
104119
if cred_mngr is not None:
105120
if cred_mngr.cred_filepath is not None:
106121
# But overwrite if the cred_mngr has a cred_filepath
107122
cred_filepath = cred_mngr.cred_filepath[0]
108123
if not isinstance(cred_filepath, Path):
109124
cred_filepath = Path(cred_filepath)
125+
if not cred_filepath.is_file():
126+
print(f"Credential file '{cred_filepath}' is not a file. ")
127+
if not cred_filepath.exists():
128+
print(
129+
f"Credential file '{cred_filepath}' does not exist and will be created."
130+
)
110131
if cred_mngr is None:
111132
# Create a credentials manager
112133
if cred_filepath is None:
@@ -285,46 +306,6 @@ def upload_file(
285306
return UploadFileResult(source=source, **data)
286307

287308

288-
try:
289-
# To load the dependencies that are not part of the osw.model.entity module as
290-
# uploaded to the repository
291-
from osw.controller.file.base import FileController # depends on File
292-
from osw.controller.file.local import LocalFileController # depends on LocalFile
293-
from osw.controller.file.memory import InMemoryController # depends on LocalFile
294-
from osw.controller.file.wiki import WikiFileController # depends on WikiFile
295-
except AttributeError as e:
296-
warn(
297-
f"An exception occurred while loading the module dependencies: \n"
298-
f'"{e}"\n'
299-
"A connection to an OSW instance, to fetch the dependencies from, has to be "
300-
"established!"
301-
)
302-
# If the default was not changed, make sure the user is prompted to enter the domain
303-
if default_params.has_changed("wiki_domain"):
304-
domain_ = default_params.wiki_domain
305-
else:
306-
domain_ = input("Please enter the domain of the OSW instance to connect to:")
307-
if domain_ == "":
308-
domain_ = default_params.wiki_domain
309-
if default_paths.has_changed("cred_filepath"):
310-
osw_express_ = OswExpress(
311-
domain=domain_, cred_filepath=default_paths.cred_filepath
312-
)
313-
else:
314-
osw_express_ = OswExpress(domain=domain_)
315-
osw_express_.install_dependencies(DEPENDENCIES)
316-
osw_express_.shut_down() # Avoiding connection error
317-
print(
318-
"Dependencies specified in the module 'osw.express' have been fetched from "
319-
"OSW."
320-
)
321-
# Try again
322-
from osw.controller.file.base import FileController # depends on File
323-
from osw.controller.file.local import LocalFileController # depends on LocalFile
324-
from osw.controller.file.memory import InMemoryController # depends on LocalFile
325-
from osw.controller.file.wiki import WikiFileController # depends on WikiFile
326-
327-
328309
class FileResult(OswBaseModel):
329310
url_or_title: Optional[str] = None
330311
"""The URL or full page title of the WikiFile page."""
@@ -419,7 +400,16 @@ def process_init_data(self, data: Dict[str, Any]) -> Dict[str, Any]:
419400
# set by the source file controller
420401
del data["label"]
421402
if data.get("cred_filepath") is None:
422-
data["cred_filepath"] = default_paths.cred_filepath
403+
# Take cred_filepath from environment variables
404+
if os.getenv("OSW_CRED_FILEPATH") is not None:
405+
data["cred_filepath"] = os.getenv("OSW_CRED_FILEPATH")
406+
elif os.getenv("OSL_CRED_FILEPATH") is not None:
407+
data["cred_filepath"] = os.getenv("OSL_CRED_FILEPATH")
408+
# Fallback to the package default (based on CWD)
409+
else:
410+
data["cred_filepath"] = default_paths.cred_filepath
411+
if not isinstance(data.get("cred_filepath"), Path):
412+
data["cred_filepath"] = Path(data.get("cred_filepath"))
423413
if not data.get("cred_filepath").parent.exists():
424414
data["cred_filepath"].parent.mkdir(parents=True)
425415
if data.get("cred_mngr") is None:
@@ -468,7 +458,14 @@ def __init__(self, url_or_title, **data):
468458
if data.get("target_fn") is None:
469459
data["target_fn"] = url_or_title.split("File:")[-1]
470460
if data.get("target_dir") is None:
471-
data["target_dir"] = default_paths.download_dir
461+
# Take target_dir from environment variables
462+
if os.getenv("OSW_DOWNLOAD_DIR") is not None:
463+
data["target_dir"] = os.getenv("OSW_DOWNLOAD_DIR")
464+
elif os.getenv("OSL_DOWNLOAD_DIR") is not None:
465+
data["target_dir"] = os.getenv("OSL_DOWNLOAD_DIR")
466+
# Fallback to the package default (based on CWD)
467+
else:
468+
data["target_dir"] = default_paths.download_dir
472469
if isinstance(data.get("target_dir"), str):
473470
data["target_dir"] = Path(data.get("target_dir"))
474471
if data.get("target_fp") is None:
@@ -837,6 +834,7 @@ def import_with_fallback(
837834
cred_filepath: Union[str, Path] = None,
838835
osw_express: OswExpress = None,
839836
):
837+
# todo: rework this function to work as OswExpress -
840838
"""Imports data models with a fallback to fetch the dependencies from an OSL
841839
instance if the data models are not available in the local osw.model.entity module.
842840
@@ -942,7 +940,11 @@ def import_with_fallback(
942940
# todo: should this be taken from globals?
943941
# If the user has set the default domain, use it
944942
if domain is None:
945-
if default_params.has_changed("wiki_domain"):
943+
if os.getenv("OSW_WIKI_DOMAIN") is not None:
944+
domain = os.getenv("OSW_WIKI_DOMAIN")
945+
elif os.getenv("OSL_WIKI_DOMAIN") is not None:
946+
domain = os.getenv("OSL_WIKI_DOMAIN")
947+
elif default_params.has_changed("wiki_domain"):
946948
domain = default_params.wiki_domain
947949
else:
948950
domain = input(
@@ -951,7 +953,12 @@ def import_with_fallback(
951953
if domain == "":
952954
domain = default_params.wiki_domain
953955
if cred_filepath is None:
954-
cred_filepath = default_paths.cred_filepath
956+
if os.getenv("OSW_CRED_FILEPATH") is not None:
957+
cred_filepath = os.getenv("OSW_CRED_FILEPATH")
958+
elif os.getenv("OSL_CRED_FILEPATH") is not None:
959+
cred_filepath = os.getenv("OSL_CRED_FILEPATH")
960+
else:
961+
cred_filepath = default_paths.cred_filepath
955962
if osw_express is None:
956963
osw_express = OswExpress(domain=domain, cred_filepath=cred_filepath)
957964

0 commit comments

Comments
 (0)