From 25d2bd53c9ea73205e74b2d7457740f8446bbdf4 Mon Sep 17 00:00:00 2001 From: David Michaels Date: Sun, 7 Jan 2024 14:47:31 -0500 Subject: [PATCH] minor tweaks to portal_utils --- dcicutils/portal_utils.py | 14 +++++++------- pyproject.toml | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/dcicutils/portal_utils.py b/dcicutils/portal_utils.py index 57308cc9f..43e3e4885 100644 --- a/dcicutils/portal_utils.py +++ b/dcicutils/portal_utils.py @@ -48,7 +48,8 @@ class Portal: def __init__(self, arg: Optional[Union[Portal, TestApp, VirtualApp, PyramidRouter, dict, tuple, str]] = None, env: Optional[str] = None, server: Optional[str] = None, - app: Optional[OrchestratedApp] = None) -> None: + app: Optional[OrchestratedApp] = None, + raise_exception: bool = True) -> None: def init(unspecified: Optional[list] = []) -> None: self._ini_file = None @@ -56,7 +57,6 @@ def init(unspecified: Optional[list] = []) -> None: self._keys_file = None self._env = None self._server = None - self._app = None self._vapp = None for arg in unspecified: if arg is not None: @@ -96,7 +96,7 @@ def init_from_key(key: dict, server: Optional[str], unspecified: Optional[list] raise Exception("Portal init error; from key.") def init_from_key_pair(key_pair: tuple, server: Optional[str], unspecified: Optional[list] = []) -> None: - if len(key_pair) == 2: + if len(key_pair) >= 2: init_from_key({"key": key_pair[0], "secret": key_pair[1]}, server, unspecified=unspecified) else: raise Exception("Portal init error; from key-pair.") @@ -147,6 +147,7 @@ def normalize_server(server: str) -> Optional[str]: if app and not (app := Portal._valid_app(app)): raise Exception(f"Portal init error; invalid app: {app}") + self._app = app if isinstance(arg, Portal): init_from_portal(arg, unspecified=[env, server, app]) elif isinstance(arg, (TestApp, VirtualApp, PyramidRouter)): @@ -163,10 +164,9 @@ def normalize_server(server: str) -> Optional[str]: init_from_env_server_app(arg, server, app, unspecified=[env]) elif (isinstance(env, str) and env) or (isinstance(server, str) and server): init_from_env_server_app(env, server, app, unspecified=[arg]) - else: + elif raise_exception: raise Exception("Portal init error; insufficient args.") - self._app = app - if not self.vapp and not self.key: + if not self.vapp and not self.key and raise_exception: raise Exception("Portal init error; neither key nor vapp defined.") @property @@ -372,7 +372,7 @@ def _default_keys_file(self, app: Optional[str], env: Optional[str] = None) -> O def infer_app_from_env(env: str) -> Optional[str]: # noqa if isinstance(env, str) and (lowercase_env := env.lower()): if app := [app for app in ORCHESTRATED_APPS if lowercase_env.startswith(app.lower())]: - return app[0] + return self._valid_app(app[0]) if (app := Portal._valid_app(app)) or (app := infer_app_from_env(env)): keys_file = os.path.expanduser(os.path.join(Portal.KEYS_FILE_DIRECTORY, f".{app.lower()}-keys.json")) return keys_file if os.path.exists(keys_file) else None diff --git a/pyproject.toml b/pyproject.toml index 753da5a5c..99ffd968d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "dcicutils" -version = "8.7.0.1b16" # TODO: To become 8.7.1 +version = "8.7.0.1b17" # TODO: To become 8.7.1 description = "Utility package for interacting with the 4DN Data Portal and other 4DN resources" authors = ["4DN-DCIC Team "] license = "MIT"