From 3f8a55992c76769a29362b29a5989c2a62293579 Mon Sep 17 00:00:00 2001 From: Michael Lindner Date: Tue, 16 Jan 2024 16:50:54 +0100 Subject: [PATCH 1/2] locate project dir if pypsa-eur is a submodule --- scripts/_helpers.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/scripts/_helpers.py b/scripts/_helpers.py index 03bde8407..70eb4df24 100644 --- a/scripts/_helpers.py +++ b/scripts/_helpers.py @@ -223,7 +223,12 @@ def update_to(b=1, bsize=1, tsize=None): urllib.request.urlretrieve(url, file, reporthook=update_to) -def mock_snakemake(rulename, root_dir=None, configfiles=[], **wildcards): +def mock_snakemake( + rulename, + root_dir=None, + configfiles=[], + submodule_dir="workflow/submodules/pypsa-eur", + **wildcards): """ This function is expected to be executed from the 'scripts'-directory of ' the snakemake project. It returns a snakemake.script.Snakemake object, @@ -239,6 +244,9 @@ def mock_snakemake(rulename, root_dir=None, configfiles=[], **wildcards): path to the root directory of the snakemake project configfiles: list, str list of configfiles to be used to update the config + submodule_dir: str, Path + in case PyPSA-Eur is used as a submodule, submodule_dir is + the path of pypsa-eur relative to the project directory. **wildcards: keyword arguments fixing the wildcards. Only necessary if wildcards are needed. @@ -257,7 +265,10 @@ def mock_snakemake(rulename, root_dir=None, configfiles=[], **wildcards): root_dir = Path(root_dir).resolve() user_in_script_dir = Path.cwd().resolve() == script_dir - if user_in_script_dir: + if str(submodule_dir) in __file__: + # the submodule_dir path is only need to locate the project dir + os.chdir(Path(__file__[:__file__.find(str(submodule_dir))])) + elif user_in_script_dir: os.chdir(root_dir) elif Path.cwd().resolve() != root_dir: raise RuntimeError( From 21a223262f145476065e2dd9dd30943bfd24594e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 16 Jan 2024 15:53:15 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- scripts/_helpers.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/scripts/_helpers.py b/scripts/_helpers.py index 70eb4df24..e0982fd46 100644 --- a/scripts/_helpers.py +++ b/scripts/_helpers.py @@ -224,11 +224,12 @@ def update_to(b=1, bsize=1, tsize=None): def mock_snakemake( - rulename, - root_dir=None, - configfiles=[], - submodule_dir="workflow/submodules/pypsa-eur", - **wildcards): + rulename, + root_dir=None, + configfiles=[], + submodule_dir="workflow/submodules/pypsa-eur", + **wildcards, +): """ This function is expected to be executed from the 'scripts'-directory of ' the snakemake project. It returns a snakemake.script.Snakemake object, @@ -245,7 +246,7 @@ def mock_snakemake( configfiles: list, str list of configfiles to be used to update the config submodule_dir: str, Path - in case PyPSA-Eur is used as a submodule, submodule_dir is + in case PyPSA-Eur is used as a submodule, submodule_dir is the path of pypsa-eur relative to the project directory. **wildcards: keyword arguments fixing the wildcards. Only necessary if wildcards are @@ -267,7 +268,7 @@ def mock_snakemake( user_in_script_dir = Path.cwd().resolve() == script_dir if str(submodule_dir) in __file__: # the submodule_dir path is only need to locate the project dir - os.chdir(Path(__file__[:__file__.find(str(submodule_dir))])) + os.chdir(Path(__file__[: __file__.find(str(submodule_dir))])) elif user_in_script_dir: os.chdir(root_dir) elif Path.cwd().resolve() != root_dir: