From 0dd84495dab65895468389abff7dd4aca1fe2840 Mon Sep 17 00:00:00 2001 From: Federica Brando Date: Tue, 12 Mar 2024 12:55:39 +0100 Subject: [PATCH 1/3] add: function to load expected environment variables. - set `OPENVAR_PLUGIN`to `$HOME/.local/share/openvariant` - check if environment variable are not in the env otherwise adds them - check if path extists already, otherwise create directory - update requiremente with appdir package --- openvariant/commands/openvar.py | 7 ++++--- openvariant/utils/utils.py | 17 +++++++++++++++++ setup.py | 2 +- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/openvariant/commands/openvar.py b/openvariant/commands/openvar.py index 0150818..1950a94 100644 --- a/openvariant/commands/openvar.py +++ b/openvariant/commands/openvar.py @@ -6,6 +6,7 @@ from openvariant.tasks.count import count as count_task from openvariant.tasks.groupby import group_by as group_by_task from openvariant.tasks.plugin import PluginActions +from openvariant.utils.utils import loadEnvironmentVariables @click.group(context_settings={'help_option_names': ['-h', '--help']}) @@ -13,6 +14,7 @@ def openvar(): """'openvar' is the command-line interface of OpenVariant. Parsing and data transformation of multiple input formats.""" + loadEnvironmentVariables() pass @@ -106,10 +108,9 @@ def groupby(input_path: str, script: str, where: str, group_by: str, cores: int, @openvar.command(name="plugin", short_help='Actions to execute for a plugin: create.') @click.argument('action', type=click.Choice(['create'])) @click.option('--name', '-n', type=click.STRING, help="Name of the plugin.") -@click.option('--directory', '-d', type=click.STRING, help="Directory to reach the plugin.") -def plugin(action, name: str or None, directory: str or None): +def plugin(action, name: str or None): """Actions to apply on the plugin system.""" - PluginActions[action.upper()].value(name, directory) + PluginActions[action.upper()].value(name) if __name__ == "__main__": diff --git a/openvariant/utils/utils.py b/openvariant/utils/utils.py index 2e9364b..6d355a2 100644 --- a/openvariant/utils/utils.py +++ b/openvariant/utils/utils.py @@ -1,8 +1,25 @@ import re +import os +from appdirs import user_data_dir from fnmatch import fnmatch from os.path import basename +ENV_VAR = { + 'OPENVAR_PLUGIN': user_data_dir('openvariant', 'bbglab') + } + +def loadEnvironmentVariables(): + """Load environment variable into the environment.""" + + missing_vars = set(ENV_VAR.keys()).difference(set(os.environ)) + + for env_var in missing_vars: + os.environ[env_var] = ENV_VAR[env_var] + os.makedirs(ENV_VAR[env_var], exist_ok=True) + + return + def check_extension(ext: str, path: str) -> bool: """Check if file matches with the annotation pattern""" return fnmatch(basename(path), ext) if ext[0] == '*' else re.match(ext, basename(path)) is not None diff --git a/setup.py b/setup.py index c3a6fe4..0355448 100644 --- a/setup.py +++ b/setup.py @@ -17,7 +17,7 @@ keywords='bioinformatics,openvariant,openvar,bbglab', packages=find_packages(exclude=["tests.*", "tests"]), include_package_data=True, - install_requires=['pyyaml', 'tqdm', 'click', 'pyliftover'], + install_requires=['pyyaml', 'tqdm', 'click', 'pyliftover', 'appdirs'], entry_points={ 'console_scripts': [ 'openvar = openvariant.commands.openvar:openvar', From 7b5cb717daa494d59330b726a6aabac23721016d Mon Sep 17 00:00:00 2001 From: Federica Brando Date: Tue, 12 Mar 2024 12:58:55 +0100 Subject: [PATCH 2/3] update: builder func and create plugin func with env var - update plugin builder to look for external plugin in `OPENVAR_PLUGIN` - update create plugin function to use env_var by default - Add `loadEnvironmentVariables()` so plugin tests do not fail. --- openvariant/annotation/builder.py | 6 +++--- openvariant/tasks/plugin.py | 4 ++-- tests/__init__.py | 3 +++ 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/openvariant/annotation/builder.py b/openvariant/annotation/builder.py index 2e6a3c0..206ff7f 100644 --- a/openvariant/annotation/builder.py +++ b/openvariant/annotation/builder.py @@ -249,9 +249,9 @@ def _plugin_builder(x: dict, base_path: str = None) -> PluginBuilder: ctxt = _get_plugin_context(mod) except ModuleNotFoundError: try: - files = list(glob.iglob(f"{os.getcwd()}/**/{x[AnnotationTypes.PLUGIN.value]}", recursive=True)) + files = list(glob.iglob(f"{os.environ['OPENVAR_PLUGIN']}/**/{x[AnnotationTypes.PLUGIN.value]}", recursive=True)) if len(files) == 0: - raise FileNotFoundError(f"Unable to find '{x[AnnotationTypes.PLUGIN.value]}' plugin in '{os.getcwd()}'") + raise FileNotFoundError(f"Unable to find '{x[AnnotationTypes.PLUGIN.value]}' plugin in '{os.environ['OPENVAR_PLUGIN']}'") else: try: for package in files: @@ -263,7 +263,7 @@ def _plugin_builder(x: dict, base_path: str = None) -> PluginBuilder: func = _get_plugin_function(mod) ctxt = _get_plugin_context(mod) except (ImportError, AttributeError): - raise ImportError(f"Unable to import 'run' on the plugin.") + raise ImportError("Unable to import 'run' on the plugin.") except ModuleNotFoundError: raise ModuleNotFoundError(f"Unable to found '{x[AnnotationTypes.PLUGIN.value]}' plugin.") except (ImportError, AttributeError) as e: diff --git a/openvariant/tasks/plugin.py b/openvariant/tasks/plugin.py index 9c2f1e9..163471e 100644 --- a/openvariant/tasks/plugin.py +++ b/openvariant/tasks/plugin.py @@ -8,7 +8,7 @@ from functools import partial -def _add_action(name: str, directory: str = None) -> None: +def _add_action(name: str) -> None: """Create a new plugin It will generate all the required structure for a new plugin (files and folders). @@ -20,7 +20,7 @@ def _add_action(name: str, directory: str = None) -> None: directory : str The path to create the plugin. """ - path = directory if directory is not None else os.getcwd() + path = os.environ['OPENVAR_PLUGIN'] if os.path.exists(f"{path}/{name}"): raise FileExistsError(f"Directory {path}/{name} already exists.") os.mkdir(f"{path}/{name}") diff --git a/tests/__init__.py b/tests/__init__.py index e69de29..1cbff9a 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1,3 @@ +from openvariant.utils.utils import loadEnvironmentVariables + +loadEnvironmentVariables() \ No newline at end of file From 51689e5a427fc3dea1b741d112da9513a023eb3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20L=C3=B3pez-Elorduy?= <107858804+CarlosLopezElorduy@users.noreply.github.com> Date: Wed, 13 Mar 2024 15:52:22 +0100 Subject: [PATCH 3/3] add: return type hinting of loadEnvironmentVariables --- openvariant/utils/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openvariant/utils/utils.py b/openvariant/utils/utils.py index 6d355a2..7fc5e2d 100644 --- a/openvariant/utils/utils.py +++ b/openvariant/utils/utils.py @@ -7,9 +7,9 @@ ENV_VAR = { 'OPENVAR_PLUGIN': user_data_dir('openvariant', 'bbglab') - } +} -def loadEnvironmentVariables(): +def loadEnvironmentVariables() -> None: """Load environment variable into the environment.""" missing_vars = set(ENV_VAR.keys()).difference(set(os.environ))