44"""
55
66import importlib .util
7+ import os
78import re
89from io import TextIOWrapper
910from pathlib import Path
2829
2930import osw .model .entity as model
3031from 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
3136from osw .core import OSW , OVERWRITE_CLASS_OPTIONS , OverwriteOptions
3237from osw .defaults import params as default_params
3338from osw .defaults import paths as default_paths
3439from osw .utils .wiki import namespace_from_full_title , title_from_full_title
3540from 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
5443class 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- "\n If 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+ "\n If 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-
328309class 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