|
1 | 1 | # flake8: noqa: E402 |
2 | 2 | """ |
3 | 3 | 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 |
4 | 12 | """ |
5 | 13 |
|
6 | 14 | import importlib.util |
@@ -101,25 +109,26 @@ def __init__( |
101 | 109 | "\nIf no domain was set as environmental variable, 'domain' is a " |
102 | 110 | "required argument." |
103 | 111 | ) |
| 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] |
104 | 115 | if cred_filepath is None: |
| 116 | + # If no credential file path is given, try to take it from environment vars |
105 | 117 | if os.getenv("OSW_CRED_FILEPATH") is not None: |
106 | 118 | cred_filepath = os.getenv("OSW_CRED_FILEPATH") |
107 | 119 | elif os.getenv("OSL_CRED_FILEPATH") is not None: |
108 | 120 | cred_filepath = os.getenv("OSL_CRED_FILEPATH") |
109 | 121 | else: |
110 | | - # prompt user to set cred_filepath |
| 122 | + # Otherwise, prompt user to set cred_filepath |
111 | 123 | cred_filepath = input( |
112 | 124 | "No credential file path was provided. Please specify, where to " |
113 | 125 | "save the credential file: " |
114 | 126 | ) |
115 | 127 | 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." |
118 | 131 | ) |
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] |
123 | 132 | if not isinstance(cred_filepath, Path): |
124 | 133 | cred_filepath = Path(cred_filepath) |
125 | 134 | if not cred_filepath.is_file(): |
@@ -323,7 +332,8 @@ class FileResult(OswBaseModel): |
323 | 332 | the credentials_manager or domain and cred_filepath.""" |
324 | 333 | domain: Optional[str] = None |
325 | 334 | """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.""" |
327 | 337 | cred_filepath: Optional[Union[str, Path]] = None |
328 | 338 | """The filepath to the credentials file. Will only be used if cred_mngr is |
329 | 339 | 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): |
478 | 488 | string=url_or_title, |
479 | 489 | ) |
480 | 490 | 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) |
486 | 502 | if data.get("use_cached") and data.get("target_fp").exists(): |
487 | 503 | # Here, no file needs to be downloaded, but self need to be initialized |
488 | 504 | # with the path to the file |
@@ -554,7 +570,8 @@ def osw_download_file( |
554 | 570 | the credentials_manager or domain and cred_filepath. |
555 | 571 | domain |
556 | 572 | 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. |
558 | 575 | cred_filepath |
559 | 576 | The filepath to the credentials file. Will only be used if cred_mngr is |
560 | 577 | None. If cred_filepath is None, a credentials file named 'accounts.pwd.yaml' |
@@ -674,6 +691,10 @@ def __init__( |
674 | 691 | if data.get("domain") is None: |
675 | 692 | if match is not None: |
676 | 693 | data["domain"] = match.group(1) |
| 694 | + else: |
| 695 | + data["domain"] = os.getenv("OSW_WIKI_DOMAIN") or os.getenv( |
| 696 | + "OSL_WIKI_DOMAIN" |
| 697 | + ) |
677 | 698 | else: |
678 | 699 | if match is not None: |
679 | 700 | if not data.get("domain") == match.group(1): |
|
0 commit comments