Skip to content

Commit 6e55d06

Browse files
committed
Fix: Unexpected credentials and domain prompt
1 parent 9304b6e commit 6e55d06

2 files changed

Lines changed: 54 additions & 17 deletions

File tree

‎src/osw/controller/page_package.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,8 @@ class CreationConfig(OswBaseModel):
367367
"""Working directory. Will be created automatically if not existing."""
368368
skip_slot_suffix_for_main: bool = False
369369
ignore_titles: Optional[List[str]] = None
370-
"""List of page titles to ignore in auto-detected dependencies
371-
, e.g. example files"""
370+
"""List of page titles to ignore in auto-detected dependencies, e.g.,
371+
example files"""
372372

373373
def create(
374374
self,

‎src/osw/express.py‎

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,20 @@ def __init__(
7171
cred_mngr: CredentialManager = None,
7272
):
7373
if cred_filepath is None:
74+
# Set default
7475
cred_filepath = default_paths.cred_filepath
7576
if cred_mngr is not None:
7677
if cred_mngr.cred_filepath is not None:
78+
# But overwrite if the cred_mngr has a cred_filepath
7779
cred_filepath = cred_mngr.cred_filepath[0]
7880
if not isinstance(cred_filepath, Path):
7981
cred_filepath = Path(cred_filepath)
8082
if cred_mngr is None:
81-
if cred_filepath.exists():
82-
cred_mngr = CredentialManager(cred_filepath=cred_filepath)
83-
else:
83+
# Create a credentials manager
84+
if cred_filepath is None:
8485
cred_mngr = CredentialManager()
86+
else: # Reuse passed cred_filepath
87+
cred_mngr = CredentialManager(cred_filepath=cred_filepath)
8588
if not cred_mngr.iri_in_file(domain):
8689
cred = cred_mngr.get_credential(
8790
CredentialManager.CredentialConfig(
@@ -294,10 +297,18 @@ def import_with_fallback(
294297
module: str = None,
295298
dependencies: Dict[str, str] = None,
296299
domain: str = None,
300+
cred_filepath: Union[str, Path] = None,
301+
osw_express: OswExpress = None,
297302
):
298303
"""Imports data models with a fallback to fetch the dependencies from an OSL
299304
instance if the data models are not available in the local osw.model.entity module.
300305
306+
DISCLAIMER: Will prompt the user to enter domain and credentials if no domain and
307+
cred_filepath or osw_express was passed.
308+
309+
BEST USAGE: See examples, for best usage in a script to be rerun multiple times
310+
(without refetching datamodels).
311+
301312
Parameters
302313
----------
303314
to_import
@@ -315,12 +326,24 @@ def import_with_fallback(
315326
domain
316327
The domain of the OSL instance to connect to, if the dependencies are not
317328
available in the local osw.model.entity module.
329+
cred_filepath
330+
The filepath to the credentials file. If None, a credentials file is created
331+
under the default path.
332+
osw_express
333+
An OswExpress object. If None, a new OswExpress object will be created using
334+
the credentials_manager or domain and cred_filepath.
318335
319336
Examples
320337
--------
338+
>>> from pathlib import Path
339+
>>> from osw.defaults import paths, params
340+
>>> from osw.express import import_with_fallback, DataModel, OswExpress
341+
>>> wiki_domain__ = "wiki-dev.open-semantic-lab.org"
342+
>>> cred_filepath__ = Path(r"accounts.pwd.yaml")
343+
>>> osw_obj = OswExpress(domain=wiki_domain__, cred_filepath=cred_filepath__)
321344
>>> import_with_fallback(
322345
>>> [DataModel(module="osw.controller.file.base",class_name="FileController")],
323-
>>> globals(),
346+
>>> globals(), osw_express=osw_obj
324347
>>> )
325348
326349
Returns
@@ -368,22 +391,31 @@ def import_with_fallback(
368391
dependencies.update(new_dependencies)
369392
if not dependencies:
370393
raise AttributeError(
371-
f"An exception occurred while loading the module dependencies: \n'{e}'"
394+
f"An exception occurred while loading the module dependencies: \n"
395+
f'"{e}"\n'
372396
"No 'dependencies' were passed to the function import_with_fallback() "
373397
"and could not be derived from 'to_import'!"
374398
)
375399
warn(
376-
f"An exception occurred while loading the module dependencies: \n'{e}'"
400+
f'An exception occurred while loading the module dependencies: \n"'
401+
f'{e}"\n'
377402
"You will now have to connect to an OSW instance to fetch the "
378403
"dependencies from!"
379404
)
405+
# If the user has set the default domain, use it
380406
if domain is None:
381-
domain = input("Please enter the domain of the OSW instance to connect to:")
382-
if domain == "" or domain is None:
383-
domain = default_params.wiki_domain
384-
osw_express = OswExpress(
385-
domain=domain, cred_filepath=default_paths.cred_filepath
386-
)
407+
if default_params.has_changed("wiki_domain"):
408+
domain = default_params.wiki_domain
409+
else:
410+
domain = input(
411+
"Please enter the domain of the OSW instance to connect to:"
412+
)
413+
if domain == "":
414+
domain = default_params.wiki_domain
415+
if cred_filepath is None:
416+
cred_filepath = default_paths.cred_filepath
417+
if osw_express is None:
418+
osw_express = OswExpress(domain=domain, cred_filepath=cred_filepath)
387419

388420
osw_express.install_dependencies(dependencies, mode="append")
389421
osw_express.shut_down() # Avoiding connection error
@@ -397,9 +429,13 @@ def import_with_fallback(
397429

398430
# If the default was not changed, make sure the user is prompted to enter the domain
399431
if default_params.has_changed("wiki_domain"):
400-
wiki_domain = default_params.wiki_domain
432+
wiki_domain_ = default_params.wiki_domain
433+
else:
434+
wiki_domain_ = None
435+
if default_paths.has_changed("cred_filepath"):
436+
cred_filepath_ = default_paths.cred_filepath
401437
else:
402-
wiki_domain = None
438+
cred_filepath_ = None
403439
import_with_fallback(
404440
to_import=[
405441
DataModel(
@@ -421,7 +457,8 @@ def import_with_fallback(
421457
],
422458
caller_globals=globals(),
423459
dependencies=DEPENDENCIES,
424-
domain=wiki_domain,
460+
domain=wiki_domain_,
461+
cred_filepath=cred_filepath_,
425462
)
426463

427464
if TYPE_CHECKING:

0 commit comments

Comments
 (0)