From fdcf242575c60cef1a05706f0fd6fd35ad2d6e1d Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Thu, 14 Jan 2021 10:57:52 +0100 Subject: [PATCH 1/4] implemented `fill_root_with_path` in anatomy to be able use custom root path --- pypeapp/lib/anatomy.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pypeapp/lib/anatomy.py b/pypeapp/lib/anatomy.py index 14ee318e..fae3102c 100644 --- a/pypeapp/lib/anatomy.py +++ b/pypeapp/lib/anatomy.py @@ -256,6 +256,24 @@ def fill_root(self, template_path): # NOTE does not care if there are different keys than "root" return template_path.format(**{"root": self.roots}) + def fill_root_with_path(self, rootless_path, root_path): + """Fill rootless template path with passed path. + + This is helper to fill root with different directory no matter if + is single or multiroot. + + Args: + rootless_path (str): Path without filled "root" key. Example: + "{root[work]}/MyProject/..." + root_path (str): What should replace root key in `rootless_path`. + """ + output = str(rootless_path) + for group in re.findall(self.root_key_regex, rootless_path): + replacement = "{" + group + "}" + output = output.replace(replacement, root_path) + + return output + def replace_root_with_env_key(self, filepath, template=None): """Replace root of path with environment key. From 90575b698d4568615d725f47e3671dbcf343a9e5 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Thu, 14 Jan 2021 10:58:02 +0100 Subject: [PATCH 2/4] modified docstring --- pypeapp/lib/anatomy.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pypeapp/lib/anatomy.py b/pypeapp/lib/anatomy.py index fae3102c..7b983ade 100644 --- a/pypeapp/lib/anatomy.py +++ b/pypeapp/lib/anatomy.py @@ -257,15 +257,21 @@ def fill_root(self, template_path): return template_path.format(**{"root": self.roots}) def fill_root_with_path(self, rootless_path, root_path): - """Fill rootless template path with passed path. + """Fill path without filled "root" key with passed path. - This is helper to fill root with different directory no matter if - is single or multiroot. + This is helper to fill root with different directory path than anatomy + has defined no matter if is single or multiroot. + + Output path is same as input path if `rootless_path` does not contain + unfilled root key. Args: rootless_path (str): Path without filled "root" key. Example: "{root[work]}/MyProject/..." root_path (str): What should replace root key in `rootless_path`. + + Returns: + str: Path with filled root. """ output = str(rootless_path) for group in re.findall(self.root_key_regex, rootless_path): From db3deaa6b803458f2594dc9a717445e9f47dab95 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Thu, 14 Jan 2021 11:01:39 +0100 Subject: [PATCH 3/4] fill_root_with_path is classmethod --- pypeapp/lib/anatomy.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pypeapp/lib/anatomy.py b/pypeapp/lib/anatomy.py index 7b983ade..29fcf7ac 100644 --- a/pypeapp/lib/anatomy.py +++ b/pypeapp/lib/anatomy.py @@ -256,7 +256,8 @@ def fill_root(self, template_path): # NOTE does not care if there are different keys than "root" return template_path.format(**{"root": self.roots}) - def fill_root_with_path(self, rootless_path, root_path): + @classmethod + def fill_root_with_path(cls, rootless_path, root_path): """Fill path without filled "root" key with passed path. This is helper to fill root with different directory path than anatomy @@ -274,7 +275,7 @@ def fill_root_with_path(self, rootless_path, root_path): str: Path with filled root. """ output = str(rootless_path) - for group in re.findall(self.root_key_regex, rootless_path): + for group in re.findall(cls.root_key_regex, rootless_path): replacement = "{" + group + "}" output = output.replace(replacement, root_path) From 583a6c39916764cdf3218bfe8fbdce44700cdf64 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Thu, 14 Jan 2021 19:26:05 +0100 Subject: [PATCH 4/4] skip empty roots when looking for rootless path --- pypeapp/lib/anatomy.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pypeapp/lib/anatomy.py b/pypeapp/lib/anatomy.py index 29fcf7ac..55abcb0f 100644 --- a/pypeapp/lib/anatomy.py +++ b/pypeapp/lib/anatomy.py @@ -1411,6 +1411,10 @@ def find_root_template_from_path(self, path): root_paths = list(self.cleaned_data.values()) mod_path = self.clean_path(path) for root_path in root_paths: + # Skip empty paths + if not root_path: + continue + if mod_path.startswith(root_path): result = True replacement = "{" + self.full_key() + "}"