From be7eace0fe04af2ae779be85d87ef116f4d3e0a9 Mon Sep 17 00:00:00 2001 From: pwwang <1188067+pwwang@users.noreply.github.com> Date: Fri, 20 Oct 2023 09:23:36 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=96=200.12.4=20(#186)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Modify sys.argv before the module is loaded in `utils.load_pipeline()` * Fix linting * 🔖 0.12.4 --- docs/CHANGELOG.md | 4 ++++ pipen/utils.py | 61 ++++++++++++++++++++++++++--------------------- pipen/version.py | 2 +- pyproject.toml | 2 +- 4 files changed, 40 insertions(+), 29 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index ae36c751..85b0d1bb 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## 0.12.4 + +- Modify sys.argv before the module is loaded in `utils.load_pipeline()` + ## 0.12.3 - Change cli_args to argv0 and argv1p for utils.load_pipeline diff --git a/pipen/utils.py b/pipen/utils.py index b3cbc065..742d061e 100644 --- a/pipen/utils.py +++ b/pipen/utils.py @@ -665,40 +665,43 @@ async def load_pipeline( from .proc import Proc from .procgroup import ProcGroup - if isinstance(obj, str): - obj = _get_obj_from_spec(obj) - if ( - not isinstance(obj, type) - or not issubclass(obj, (Pipen, Proc, ProcGroup)) - ): - raise TypeError( - f"Expected a Pipen, Proc or ProcGroup class, got {type(obj)}" - ) - - pipeline = obj - if isinstance(obj, type) and issubclass(obj, Proc): - kwargs.setdefault("name", f"{obj.name}Pipeline") - pipeline = Pipen(**kwargs).set_starts(obj) - - if isinstance(obj, type) and issubclass(obj, ProcGroup): - pipeline = obj().as_pipen(**kwargs) - - if isinstance(obj, type) and issubclass(obj, Pipen): - # Avoid "pipeline" to be used as pipeline name by varname - (pipeline, ) = (obj(**kwargs), ) - - if not isinstance(pipeline, Pipen): - raise TypeError( - f"Expected a Pipen, Proc or ProcGroup class, got {type(pipeline)}" - ) - old_argv = sys.argv if argv0 is None: argv0 = LOADING_ARGV0 if argv1p is None: argv1p = sys.argv[1:] sys.argv = [argv0] + list(argv1p) + try: + if isinstance(obj, str): + obj = _get_obj_from_spec(obj) + if ( + not isinstance(obj, type) + or not issubclass(obj, (Pipen, Proc, ProcGroup)) + ): + raise TypeError( + "Expected a Pipen, Proc or ProcGroup class, " + f"got {type(obj)}" + ) + + pipeline = obj + if isinstance(obj, type) and issubclass(obj, Proc): + kwargs.setdefault("name", f"{obj.name}Pipeline") + pipeline = Pipen(**kwargs).set_starts(obj) + + if isinstance(obj, type) and issubclass(obj, ProcGroup): + pipeline = obj().as_pipen(**kwargs) + + if isinstance(obj, type) and issubclass(obj, Pipen): + # Avoid "pipeline" to be used as pipeline name by varname + (pipeline, ) = (obj(**kwargs), ) + + if not isinstance(pipeline, Pipen): + raise TypeError( + "Expected a Pipen, Proc or ProcGroup class, " + f"got {type(pipeline)}" + ) + # Initialize the pipeline so that the arguments definied by # other plugins (i.e. pipen-args) to take in place. pipeline.workdir = Path(pipeline.config.workdir).joinpath( @@ -717,6 +720,10 @@ def is_loading_pipeline() -> bool: """Check if we are loading the pipeline. Works only when `argv0` is "@pipen" while loading the pipeline. + Note if you are using this function at compile time, make + sure you load your pipeline using the string form (`part1:part2`) + See more with `load_pipline()`. + Returns: True if we are loading the pipeline (argv[0] == "@pipen"), otherwise False diff --git a/pipen/version.py b/pipen/version.py index 8bb84572..aab091bc 100644 --- a/pipen/version.py +++ b/pipen/version.py @@ -1,3 +1,3 @@ """Provide version of pipen""" -__version__ = "0.12.3" +__version__ = "0.12.4" diff --git a/pyproject.toml b/pyproject.toml index 39293e83..c0fc8a79 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.masonry.api" [tool.poetry] name = "pipen" -version = "0.12.3" +version = "0.12.4" description = "A pipeline framework for python" authors = [ "pwwang ",] license = "MIT"