Skip to content

Commit 1425a51

Browse files
feat: make EnvBase generic; enable EnvSpecBase recursive attribute modification
1 parent c44fc12 commit 1425a51

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

  • snakemake_interface_software_deployment_plugins

snakemake_interface_software_deployment_plugins/__init__.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Generic
2+
13
__author__ = "Johannes Köster"
24
__copyright__ = "Copyright 2024, Johannes Köster"
35
__email__ = "johannes.koester@uni-due.de"
@@ -23,6 +25,7 @@
2325
Type,
2426
TypeVar,
2527
Union,
28+
Callable,
2629
)
2730
import subprocess as sp
2831

@@ -92,24 +95,32 @@ def has_source_paths(self) -> bool:
9295
return True
9396
return False
9497

95-
def modify_source_paths(self, modify_func) -> Self:
98+
def modify_source_paths(self, modify_func: Callable) -> Self:
99+
return self._modify_attributes("source_path_attributes", modify_func)
100+
101+
def modify_identity_attributes(self, modify_func: Callable) -> Self:
102+
return self._modify_attributes("identity_attributes", modify_func)
103+
104+
def _modify_attributes(self, attribute_method: str, modify_func: Callable) -> Self:
96105
if self.has_source_paths():
97106
self_or_copied = copy(self)
98107
else:
99108
return self
100-
for attr_name in self_or_copied.source_path_attributes():
109+
for attr_name in getattr(self_or_copied, attribute_method):
101110
current_value = getattr(self_or_copied, attr_name)
102111
if current_value is not None:
103112
setattr(self_or_copied, attr_name, modify_func(current_value))
104113

105114
if self_or_copied.within is not None:
106-
self_or_copied.within = self_or_copied.within.modify_source_paths(
107-
modify_func
115+
self_or_copied.within = self_or_copied.within._modify_attributes(
116+
attribute_method,
117+
modify_func,
108118
)
109119

110120
if self_or_copied.fallback is not None:
111-
self_or_copied.fallback = self_or_copied.fallback.modify_source_paths(
112-
modify_func
121+
self_or_copied.fallback = self_or_copied.fallback._modify_attributes(
122+
attribute_method,
123+
modify_func,
113124
)
114125
return self_or_copied
115126

@@ -160,14 +171,14 @@ def run(self, cmd: str, **kwargs) -> sp.CompletedProcess:
160171
TSettings = TypeVar("TSettings", bound="SoftwareDeploymentSettingsBase")
161172

162173

163-
class EnvBase(ABC):
174+
class EnvBase(ABC, Generic[TSettings]):
164175
_cache: ClassVar[Dict[Tuple[Type["EnvBase"], Optional["EnvBase"]], Any]] = {}
165176

166177
def __init__(
167178
self,
168179
spec: EnvSpecBase,
169180
within: Optional["EnvBase"],
170-
settings: Optional[SoftwareDeploymentSettingsBase],
181+
settings: Optional[TSettings],
171182
shell_executable: ShellExecutable,
172183
tempdir: Path,
173184
source_cache: Path,
@@ -177,7 +188,7 @@ def __init__(
177188
):
178189
self.spec = spec
179190
self.within = within
180-
self.settings: Optional[SoftwareDeploymentSettingsBase] = settings
191+
self.settings: Optional[TSettings] = settings
181192
self.shell_executable = shell_executable
182193
self.tempdir = tempdir
183194
self.source_cache: Path = source_cache

0 commit comments

Comments
 (0)