Skip to content

Commit 1e6c0c9

Browse files
committed
Updated express.py to expected behavior
1 parent 55e16b6 commit 1e6c0c9

1 file changed

Lines changed: 35 additions & 14 deletions

File tree

‎src/osw/express.py‎

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# flake8: noqa: E402
22
"""
33
This module provides convenience functions for osw-python.
4+
5+
This module expects environment variables to be set and available, e.g. through
6+
dotenv.load_dotenv()
7+
- OSW_WIKI_DOMAIN: domain of the OSL instance to connect to
8+
- OSW_CRED_FILEPATH: filepath to the credential file, if not specified the default
9+
file path based on current working directory will be used
10+
- OSW_DOWNLOAD_DIR: directory to download files, if not specified the default
11+
directory based on the current working directory will be used
412
"""
513

614
import importlib.util
@@ -101,25 +109,26 @@ def __init__(
101109
"\nIf no domain was set as environmental variable, 'domain' is a "
102110
"required argument."
103111
)
112+
if cred_mngr is not None and cred_mngr.cred_filepath is not None:
113+
# If a credential manager is explicitly defined, that should have priority
114+
cred_filepath = cred_mngr.cred_filepath[0]
104115
if cred_filepath is None:
116+
# If no credential file path is given, try to take it from environment vars
105117
if os.getenv("OSW_CRED_FILEPATH") is not None:
106118
cred_filepath = os.getenv("OSW_CRED_FILEPATH")
107119
elif os.getenv("OSL_CRED_FILEPATH") is not None:
108120
cred_filepath = os.getenv("OSL_CRED_FILEPATH")
109121
else:
110-
# prompt user to set cred_filepath
122+
# Otherwise, prompt user to set cred_filepath
111123
cred_filepath = input(
112124
"No credential file path was provided. Please specify, where to "
113125
"save the credential file: "
114126
)
115127
print(
116-
f"Credential file path changed to '{cred_filepath}'. Please set "
117-
f"environment variable 'OSW_CRED_FILEPATH' accordingly."
128+
f"Credential file path changed to '{cred_filepath}'."
129+
"\nPlease set environment variable 'OSW_CRED_FILEPATH' accordingly."
130+
"\nIf adequate, make sure to load the .env file."
118131
)
119-
if cred_mngr is not None:
120-
if cred_mngr.cred_filepath is not None:
121-
# But overwrite if the cred_mngr has a cred_filepath
122-
cred_filepath = cred_mngr.cred_filepath[0]
123132
if not isinstance(cred_filepath, Path):
124133
cred_filepath = Path(cred_filepath)
125134
if not cred_filepath.is_file():
@@ -323,7 +332,8 @@ class FileResult(OswBaseModel):
323332
the credentials_manager or domain and cred_filepath."""
324333
domain: Optional[str] = None
325334
"""The domain of the OSL instance to download the file from. Required if
326-
urL_or_title is a full page title. If None the domain is parsed from the URL."""
335+
urL_or_title is a full page title and the domain is not set in the environment
336+
variable 'OSW_WIKI_DOMAIN'. If None the domain is parsed from the URL."""
327337
cred_filepath: Optional[Union[str, Path]] = None
328338
"""The filepath to the credentials file. Will only be used if cred_mngr is
329339
None. If cred_filepath is None, a credentials file named 'accounts.pwd.yaml' is
@@ -478,11 +488,17 @@ def __init__(self, url_or_title, **data):
478488
string=url_or_title,
479489
)
480490
if match is None:
481-
raise ValueError(
482-
f"Could not parse domain from URL: {url_or_title}. "
483-
f"Either specify URL or domain and full page title."
484-
)
485-
data["domain"] = match.group(1)
491+
if os.getenv("OSW_WIKI_DOMAIN") is not None:
492+
data["domain"] = os.getenv("OSW_WIKI_DOMAIN")
493+
elif os.getenv("OSL_WIKI_DOMAIN") is not None:
494+
data["domain"] = os.getenv("OSL_WIKI_DOMAIN")
495+
else:
496+
raise ValueError(
497+
f"Could not parse domain from URL: {url_or_title}. "
498+
f"Either specify URL or domain and full page title."
499+
)
500+
else:
501+
data["domain"] = match.group(1)
486502
if data.get("use_cached") and data.get("target_fp").exists():
487503
# Here, no file needs to be downloaded, but self need to be initialized
488504
# with the path to the file
@@ -554,7 +570,8 @@ def osw_download_file(
554570
the credentials_manager or domain and cred_filepath.
555571
domain
556572
The domain of the OSL instance to download the file from. Required if
557-
urL_or_title is a full page title. If None the domain is parsed from the URL.
573+
urL_or_title is a full page title and the domain is not set in the environment
574+
variable 'OSW_WIKI_DOMAIN'. If None the domain is parsed from the URL.
558575
cred_filepath
559576
The filepath to the credentials file. Will only be used if cred_mngr is
560577
None. If cred_filepath is None, a credentials file named 'accounts.pwd.yaml'
@@ -674,6 +691,10 @@ def __init__(
674691
if data.get("domain") is None:
675692
if match is not None:
676693
data["domain"] = match.group(1)
694+
else:
695+
data["domain"] = os.getenv("OSW_WIKI_DOMAIN") or os.getenv(
696+
"OSL_WIKI_DOMAIN"
697+
)
677698
else:
678699
if match is not None:
679700
if not data.get("domain") == match.group(1):

0 commit comments

Comments
 (0)