transcript.md 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- transcript/__init__.py +35 -0
- transcript/_rule/__init__.py +0 -0
- transcript/_rule/archive.py +128 -0
- transcript/_rule/conda.py +52 -0
- transcript/_rule/container.py +290 -0
- transcript/_rule/core.py +524 -0
- transcript/_rule/lambdas.py +120 -0
- transcript/_rule/registry.py +54 -0
- transcript/_rule/serialize.py +26 -0
- transcript/_rule/snakefile.py +81 -0
- transcript/_rule/wildcards.py +110 -0
- transcript/cli.py +363 -0
- transcript/document.py +56 -0
- transcript/execute.py +240 -0
- transcript/html/__init__.py +17 -0
- transcript/html/_assets/.frontend.sha256 +1 -0
- transcript/html/_assets/__init__.py +21 -0
- transcript/html/_assets/style.min.css +1 -0
- transcript/html/_assets/transcript.min.js +21 -0
- transcript/html/_island.py +72 -0
- transcript/html/_navigate.py +198 -0
- transcript/html/_output.py +100 -0
- transcript/html/_resources.py +171 -0
- transcript/html/_review.py +34 -0
- transcript/html/markup.py +26 -0
- transcript/html/page.py +107 -0
- transcript/output.py +308 -0
- transcript/parse.py +256 -0
- transcript/renderers/__init__.py +126 -0
- transcript/renderers/bitmap.py +166 -0
- transcript/renderers/compose.py +174 -0
- transcript/renderers/compose_runtime.js +233 -0
- transcript/renderers/datavzrd.py +321 -0
- transcript/renderers/datavzrd_runtime.js +362 -0
- transcript/renderers/label.py +211 -0
- transcript/renderers/markdown.py +45 -0
- transcript/renderers/table.py +32 -0
- transcript/renderers/vegalite.py +250 -0
- transcript/review/__init__.py +13 -0
- transcript/review/_block.py +167 -0
- transcript/review/_migrate.py +212 -0
- transcript/review/_records.py +238 -0
- transcript/review/_span.py +287 -0
- transcript/review/load.py +64 -0
- transcript/tmd.py +159 -0
- transcript_md-0.1.0.dist-info/METADATA +17 -0
- transcript_md-0.1.0.dist-info/RECORD +49 -0
- transcript_md-0.1.0.dist-info/WHEEL +4 -0
- transcript_md-0.1.0.dist-info/entry_points.txt +2 -0
transcript/__init__.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""Executable Python documents with recorded outputs."""
|
|
2
|
+
|
|
3
|
+
from .execute import md
|
|
4
|
+
from .renderers.bitmap import bitmap, image
|
|
5
|
+
from .renderers.compose import compose
|
|
6
|
+
from .renderers.datavzrd import datavzrd
|
|
7
|
+
from .renderers.label import code, label
|
|
8
|
+
from .renderers.markdown import markdown
|
|
9
|
+
from .renderers.table import table
|
|
10
|
+
from .renderers.vegalite import vegalite
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"bitmap",
|
|
14
|
+
"code",
|
|
15
|
+
"compose",
|
|
16
|
+
"datavzrd",
|
|
17
|
+
"image",
|
|
18
|
+
"label",
|
|
19
|
+
"markdown",
|
|
20
|
+
"md",
|
|
21
|
+
"rule",
|
|
22
|
+
"table",
|
|
23
|
+
"vegalite",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def rule(name=None):
|
|
28
|
+
"""Declare this document's Snakemake rule.
|
|
29
|
+
|
|
30
|
+
Requires the optional ``snakemake`` extra, imported on first call so that
|
|
31
|
+
``import transcript`` stays dependency-free.
|
|
32
|
+
"""
|
|
33
|
+
from ._rule.core import rule as declare
|
|
34
|
+
|
|
35
|
+
return declare(name)
|
|
File without changes
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"""The canonical ``# transcript:rule`` block: build it and read it back."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ast
|
|
6
|
+
import re
|
|
7
|
+
from typing import TYPE_CHECKING
|
|
8
|
+
|
|
9
|
+
from snakemake.exceptions import WorkflowError
|
|
10
|
+
|
|
11
|
+
from ..parse import line_offsets, with_marker_text
|
|
12
|
+
from .lambdas import _StaticLambda
|
|
13
|
+
from .serialize import _entry
|
|
14
|
+
|
|
15
|
+
if TYPE_CHECKING:
|
|
16
|
+
from .core import Rule
|
|
17
|
+
|
|
18
|
+
_RULE_MARKER_RE = re.compile(
|
|
19
|
+
r"# transcript:rule(?: (?P<name>[A-Za-z_][A-Za-z0-9_]*):)?"
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def declarations(rule: Rule, target: str):
|
|
24
|
+
"""Return the canonical declarations, each possibly spanning several lines."""
|
|
25
|
+
for container in (rule._input, rule._output, rule._log, rule._params):
|
|
26
|
+
args = [_entry(name, value) for name, value in container._raw_items()]
|
|
27
|
+
if args:
|
|
28
|
+
yield 1, f"{target}.{container._kind}("
|
|
29
|
+
for arg in args:
|
|
30
|
+
yield 2, arg
|
|
31
|
+
yield 1, ")"
|
|
32
|
+
if rule._threads is not None:
|
|
33
|
+
yield 1, f"{target}.threads = {rule._threads}"
|
|
34
|
+
if rule._resources:
|
|
35
|
+
yield 1, f"{target}.resources("
|
|
36
|
+
for key, value in rule._resources.items():
|
|
37
|
+
yield 2, _entry(key, value)
|
|
38
|
+
yield 1, ")"
|
|
39
|
+
if rule._conda is not None:
|
|
40
|
+
yield 1, f"{target}.conda("
|
|
41
|
+
for i, line in rule._conda._item():
|
|
42
|
+
yield i + 1, line
|
|
43
|
+
yield 1, ")"
|
|
44
|
+
if rule._wildcard_state.defaults is not None:
|
|
45
|
+
yield 1, f"{target}.wildcards("
|
|
46
|
+
for key, value in rule._declared_wildcards().items():
|
|
47
|
+
yield 2, _entry(key, value)
|
|
48
|
+
yield 1, ")"
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def make_rule_block(rule: Rule, TAB=" "):
|
|
52
|
+
if not rule.edit:
|
|
53
|
+
return
|
|
54
|
+
(start, end), context_source = rule.edit
|
|
55
|
+
if not rule.target:
|
|
56
|
+
raise NameError("transcript.rule() must be assigned to a name")
|
|
57
|
+
prefix = "\n\n" if context_source is None else ""
|
|
58
|
+
if context_source and context_source != rule.target:
|
|
59
|
+
assign = f"{context_source} as "
|
|
60
|
+
else:
|
|
61
|
+
assign = ""
|
|
62
|
+
marker = "# transcript:rule"
|
|
63
|
+
marker_name = "" if rule.name is None else f" {rule.name}:"
|
|
64
|
+
header = f"{prefix}with {assign}{rule.target}: {marker}{marker_name}"
|
|
65
|
+
body = [f"\n{i * TAB}{line}" for i, line in declarations(rule, rule.target)]
|
|
66
|
+
yield start, end, header + "".join(body or [f"\n{TAB}pass"])
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def from_tmd(cls: type[Rule], source: str, filename=None):
|
|
70
|
+
"""Rebuild a rule from its canonical ``# transcript:rule`` block."""
|
|
71
|
+
tree = ast.parse(source)
|
|
72
|
+
position = line_offsets(source)
|
|
73
|
+
for node in tree.body:
|
|
74
|
+
if isinstance(node, ast.With) and len(node.items) == 1:
|
|
75
|
+
marker = with_marker_text(source, node, position)[3]
|
|
76
|
+
match = _RULE_MARKER_RE.fullmatch(marker)
|
|
77
|
+
if match:
|
|
78
|
+
self = from_node(cls, node, match["name"])
|
|
79
|
+
if filename is not None:
|
|
80
|
+
self._filename = str(filename)
|
|
81
|
+
return self
|
|
82
|
+
raise WorkflowError(
|
|
83
|
+
"No # transcript:rule block found. "
|
|
84
|
+
"Please define input/output with transcript.rule() in the document."
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def from_node(cls: type[Rule], node: ast.With, name: str | None):
|
|
89
|
+
rule = cls(name)
|
|
90
|
+
with rule:
|
|
91
|
+
for statement in node.body:
|
|
92
|
+
if isinstance(statement, ast.Assign):
|
|
93
|
+
target = statement.targets[0] if len(statement.targets) == 1 else None
|
|
94
|
+
kind = _direct_attribute(target) if target is not None else None
|
|
95
|
+
if kind != "threads":
|
|
96
|
+
raise WorkflowError(f"unsupported rule declaration: {kind!r}")
|
|
97
|
+
rule.threads = ast.literal_eval(statement.value)
|
|
98
|
+
continue
|
|
99
|
+
if isinstance(statement, ast.Expr) and isinstance(
|
|
100
|
+
statement.value, ast.Call
|
|
101
|
+
):
|
|
102
|
+
call = statement.value
|
|
103
|
+
kind = _direct_attribute(call.func)
|
|
104
|
+
declaration = getattr(rule, kind, None) if kind else None
|
|
105
|
+
if not callable(declaration):
|
|
106
|
+
raise WorkflowError(f"unsupported rule declaration: {kind!r}")
|
|
107
|
+
|
|
108
|
+
def read(value):
|
|
109
|
+
if kind in cls._LAMBDA_AUX and isinstance(value, ast.Lambda):
|
|
110
|
+
return _StaticLambda.from_node(cls._LAMBDA_AUX[kind], value)
|
|
111
|
+
return ast.literal_eval(value)
|
|
112
|
+
|
|
113
|
+
declaration(
|
|
114
|
+
*(read(arg) for arg in call.args),
|
|
115
|
+
**{
|
|
116
|
+
keyword.arg: read(keyword.value)
|
|
117
|
+
for keyword in call.keywords
|
|
118
|
+
if keyword.arg
|
|
119
|
+
},
|
|
120
|
+
)
|
|
121
|
+
return rule
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _direct_attribute(node):
|
|
125
|
+
"""Return ``node``'s attribute name if it is ``<name>.<attr>``."""
|
|
126
|
+
if isinstance(node, ast.Attribute) and isinstance(node.value, ast.Name):
|
|
127
|
+
return node.attr
|
|
128
|
+
return None
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"""The rule's conda environment declaration."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from snakemake.exceptions import WorkflowError
|
|
6
|
+
|
|
7
|
+
from .serialize import _literal
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _string_list(value):
|
|
11
|
+
"""Accept a string or a list of strings; return a canonical list."""
|
|
12
|
+
if isinstance(value, str):
|
|
13
|
+
value = [value]
|
|
14
|
+
if not isinstance(value, (list, tuple)) or not all(
|
|
15
|
+
isinstance(item, str) for item in value
|
|
16
|
+
):
|
|
17
|
+
raise WorkflowError("value must be a string or a list of strings")
|
|
18
|
+
return list(value)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class Conda:
|
|
22
|
+
def __init__(self, dependencies=(), *, pip=(), channels=("conda-forge",)):
|
|
23
|
+
if pip and not dependencies:
|
|
24
|
+
dependencies = "pip"
|
|
25
|
+
if not dependencies:
|
|
26
|
+
raise WorkflowError("at least one dependency must be declared")
|
|
27
|
+
self.dependencies = _string_list(dependencies)
|
|
28
|
+
self.pip = _string_list(pip)
|
|
29
|
+
self.channels = _string_list(channels)
|
|
30
|
+
|
|
31
|
+
def _item(self):
|
|
32
|
+
for key in ("dependencies", "pip", "channels"):
|
|
33
|
+
value = getattr(self, key)
|
|
34
|
+
if value:
|
|
35
|
+
yield 1, f"{key}=["
|
|
36
|
+
for item in value:
|
|
37
|
+
yield 2, f"{_literal(item)},"
|
|
38
|
+
yield 1, "],"
|
|
39
|
+
|
|
40
|
+
def to_yaml(self):
|
|
41
|
+
"""Serialize a conda-style environment mapping without a YAML dependency."""
|
|
42
|
+
lines = ["channels:"]
|
|
43
|
+
for channel in self.channels:
|
|
44
|
+
lines.append(f" - {channel}")
|
|
45
|
+
lines.append("dependencies:")
|
|
46
|
+
for dependency in self.dependencies:
|
|
47
|
+
lines.append(f" - {dependency}")
|
|
48
|
+
pip = self.pip
|
|
49
|
+
if pip:
|
|
50
|
+
lines.append(" - pip:")
|
|
51
|
+
lines.extend(f" - {item}" for item in pip)
|
|
52
|
+
return "\n".join(lines) + "\n"
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
"""The rule's declared containers (input/output/log/params) and wildcard view."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ast
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import TYPE_CHECKING, Any, overload
|
|
8
|
+
|
|
9
|
+
from snakemake.exceptions import WorkflowError
|
|
10
|
+
from snakemake.io import get_wildcard_names
|
|
11
|
+
|
|
12
|
+
try: # Snakemake >= 9.16
|
|
13
|
+
from snakemake.iocontainers import Namedlist
|
|
14
|
+
except ImportError: # Snakemake 9.x before the module split
|
|
15
|
+
from snakemake.io import Namedlist # type: ignore
|
|
16
|
+
|
|
17
|
+
from .lambdas import _StaticLambda
|
|
18
|
+
from .wildcards import _iter_str, _matches
|
|
19
|
+
|
|
20
|
+
if TYPE_CHECKING:
|
|
21
|
+
from .core import Rule
|
|
22
|
+
|
|
23
|
+
_MISSING = object()
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class DeclaredList:
|
|
27
|
+
"""An ordered, named declaration of paths or params.
|
|
28
|
+
|
|
29
|
+
Declarations made inside the rule block are collected in a Snakemake
|
|
30
|
+
``Namedlist``; declarations made after it are appended to ``_later`` and
|
|
31
|
+
merged after the block entries when read or serialized. Path values
|
|
32
|
+
resolve through the owning rule when read.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
def __init__(self, rule: Rule, kind: str, *, is_path=True):
|
|
36
|
+
self._rule = rule
|
|
37
|
+
self._kind = kind
|
|
38
|
+
self._is_path = is_path
|
|
39
|
+
self._named: Namedlist | None = None
|
|
40
|
+
self._later: list[tuple[str | None, Any]] = []
|
|
41
|
+
|
|
42
|
+
@overload
|
|
43
|
+
def __call__(self, *args) -> Namedlist: ...
|
|
44
|
+
|
|
45
|
+
@overload
|
|
46
|
+
def __call__(self, **kwargs) -> Namedlist: ...
|
|
47
|
+
|
|
48
|
+
@overload
|
|
49
|
+
def __call__(self, *nargs, **kwargs) -> None: ...
|
|
50
|
+
|
|
51
|
+
def __call__(self, *nargs, **kwargs):
|
|
52
|
+
_coerce = self._coerce
|
|
53
|
+
_nargs = (_coerce(value) for value in nargs)
|
|
54
|
+
_kwargs = ((name, _coerce(value)) for name, value in kwargs.items())
|
|
55
|
+
if self._rule.with_declare:
|
|
56
|
+
if self._named is None:
|
|
57
|
+
self._named = Namedlist()
|
|
58
|
+
for value in _nargs:
|
|
59
|
+
self._named.append(value)
|
|
60
|
+
for name, value in _kwargs:
|
|
61
|
+
self._named.append(value)
|
|
62
|
+
self._named._add_name(name)
|
|
63
|
+
later, self._later = self._later, []
|
|
64
|
+
for name, value in later:
|
|
65
|
+
self._add_kwargs(name, value) if name else self._add_nargs(value)
|
|
66
|
+
return
|
|
67
|
+
if nargs:
|
|
68
|
+
if kwargs:
|
|
69
|
+
raise WorkflowError(
|
|
70
|
+
"named and anonymous entries can only be mixed"
|
|
71
|
+
" in the rule declaration"
|
|
72
|
+
)
|
|
73
|
+
return Namedlist(self._resolve(self._add_nargs(value)) for value in _nargs)
|
|
74
|
+
return Namedlist(
|
|
75
|
+
fromdict={
|
|
76
|
+
name: self._resolve(self._add_kwargs(name, value))
|
|
77
|
+
for name, value in _kwargs
|
|
78
|
+
}
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
def __setattr__(self, name: str, value):
|
|
82
|
+
if name[:1] == "_":
|
|
83
|
+
object.__setattr__(self, name, value)
|
|
84
|
+
else:
|
|
85
|
+
if self._rule.with_declare is True:
|
|
86
|
+
raise WorkflowError(
|
|
87
|
+
"parameter setting can only be outside the rule declaration"
|
|
88
|
+
)
|
|
89
|
+
self._add_kwargs(name, self._coerce(value))
|
|
90
|
+
|
|
91
|
+
def __getattribute__(self, name):
|
|
92
|
+
if name[:1] != "_":
|
|
93
|
+
value = self._raw(name)
|
|
94
|
+
if value is not _MISSING:
|
|
95
|
+
return self._resolve(value)
|
|
96
|
+
return object.__getattribute__(self, name)
|
|
97
|
+
|
|
98
|
+
def __setitem__(self, key: str, value):
|
|
99
|
+
if self._rule.with_declare is True:
|
|
100
|
+
raise WorkflowError(
|
|
101
|
+
"parameter setting can only be outside the rule declaration"
|
|
102
|
+
)
|
|
103
|
+
if isinstance(key, str):
|
|
104
|
+
self._add_kwargs(key, self._coerce(value))
|
|
105
|
+
else:
|
|
106
|
+
raise WorkflowError(
|
|
107
|
+
f"{self._kind} only supports name assignment, got {key!r}"
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
def __getitem__(self, key: str | int):
|
|
111
|
+
if isinstance(key, str):
|
|
112
|
+
value = self._raw(key)
|
|
113
|
+
if value is _MISSING:
|
|
114
|
+
raise WorkflowError(f"{self._kind}.{key} is not declared")
|
|
115
|
+
return self._resolve(value)
|
|
116
|
+
if isinstance(key, int) and not isinstance(key, bool):
|
|
117
|
+
items = list(self._raw_items())
|
|
118
|
+
return self._resolve(items[key][1])
|
|
119
|
+
raise WorkflowError(f"{self._kind} key must be a name or index, got {key!r}")
|
|
120
|
+
|
|
121
|
+
def __iter__(self):
|
|
122
|
+
for _, value in self._raw_items():
|
|
123
|
+
yield self._resolve(value)
|
|
124
|
+
|
|
125
|
+
def __len__(self):
|
|
126
|
+
return sum(1 for _ in self._raw_items())
|
|
127
|
+
|
|
128
|
+
def __contains__(self, name: str):
|
|
129
|
+
return self._raw(name) is not _MISSING
|
|
130
|
+
|
|
131
|
+
def _raw_items(self):
|
|
132
|
+
"""Yield ``(name, value)``: block entries first, then later additions."""
|
|
133
|
+
named = self._named
|
|
134
|
+
if named is not None:
|
|
135
|
+
index_to_name = {index: name for name, (index, _) in named._names.items()}
|
|
136
|
+
for index, value in enumerate(named):
|
|
137
|
+
yield index_to_name.get(index), value
|
|
138
|
+
yield from self._later
|
|
139
|
+
|
|
140
|
+
def _raw(self, name: str):
|
|
141
|
+
named = self._named
|
|
142
|
+
if named is not None and name in named._names:
|
|
143
|
+
return named[named._names[name][0]]
|
|
144
|
+
for existing_name, value in self._later:
|
|
145
|
+
if existing_name == name:
|
|
146
|
+
return value
|
|
147
|
+
return _MISSING
|
|
148
|
+
|
|
149
|
+
def _remove_named(self, name: str):
|
|
150
|
+
"""Drop a block entry after execution (internal use)."""
|
|
151
|
+
named = self._named
|
|
152
|
+
if named is None or name not in named._names:
|
|
153
|
+
self._later = [(n, v) for n, v in self._later if n != name]
|
|
154
|
+
return
|
|
155
|
+
index = named._names[name][0]
|
|
156
|
+
names = {i: n for n, (i, _) in named._names.items() if n != name}
|
|
157
|
+
rebuilt = Namedlist()
|
|
158
|
+
for i, item in enumerate(named):
|
|
159
|
+
if i == index:
|
|
160
|
+
continue
|
|
161
|
+
rebuilt.append(item)
|
|
162
|
+
if i in names:
|
|
163
|
+
rebuilt._add_name(names[i])
|
|
164
|
+
self._named = rebuilt
|
|
165
|
+
|
|
166
|
+
@property
|
|
167
|
+
def _coerce(self):
|
|
168
|
+
"""Accept anything string-like from callers; store the canonical value."""
|
|
169
|
+
if self._kind == "input":
|
|
170
|
+
return self._validate_input
|
|
171
|
+
return str if self._is_path else self._validate_param
|
|
172
|
+
|
|
173
|
+
def _validate_input(self, value):
|
|
174
|
+
"""Input coercion: retain strings and wildcard-only lambdas."""
|
|
175
|
+
value_ = self._validate_lambda(value)
|
|
176
|
+
if value_ is None:
|
|
177
|
+
return str(value)
|
|
178
|
+
return value_
|
|
179
|
+
|
|
180
|
+
def _validate_param(self, value):
|
|
181
|
+
"""Params coercion: retain literals and self-contained lambdas."""
|
|
182
|
+
value_ = self._validate_lambda(value)
|
|
183
|
+
if value_ is None:
|
|
184
|
+
try:
|
|
185
|
+
if ast.literal_eval(repr(value)) == value:
|
|
186
|
+
return value
|
|
187
|
+
except ValueError, SyntaxError, TypeError:
|
|
188
|
+
pass
|
|
189
|
+
raise WorkflowError(
|
|
190
|
+
f"{self._kind} value must be a literal or a self-contained lambda, "
|
|
191
|
+
f"got {value!r}"
|
|
192
|
+
)
|
|
193
|
+
return value_
|
|
194
|
+
|
|
195
|
+
def _validate_lambda(self, value):
|
|
196
|
+
"""retain self-contained lambdas or None"""
|
|
197
|
+
if isinstance(value, _StaticLambda):
|
|
198
|
+
return value
|
|
199
|
+
if callable(value):
|
|
200
|
+
return self._rule._declare_lambda(self._kind, value)
|
|
201
|
+
return None
|
|
202
|
+
|
|
203
|
+
def _add_kwargs(self, name: str, value):
|
|
204
|
+
return self._rule._add_out_of_block(self, name, value)
|
|
205
|
+
|
|
206
|
+
def _add_nargs(self, value):
|
|
207
|
+
return self._rule._add_out_of_block(self, None, value)
|
|
208
|
+
|
|
209
|
+
@classmethod
|
|
210
|
+
def _reserved(cls, name: str):
|
|
211
|
+
"""Whether *name* collides with a container or ``Namedlist`` attribute."""
|
|
212
|
+
return hasattr(cls, name) or hasattr(Namedlist, name)
|
|
213
|
+
|
|
214
|
+
def _new_kwarg(self, name: str, value):
|
|
215
|
+
"""Validate a named declaration; return whether it is new."""
|
|
216
|
+
if self._reserved(name):
|
|
217
|
+
raise WorkflowError(f"{self._kind}.{name} is reserved for internal use")
|
|
218
|
+
existing = self._raw(name)
|
|
219
|
+
if existing is _MISSING:
|
|
220
|
+
return True
|
|
221
|
+
if _matches(existing, value):
|
|
222
|
+
return False
|
|
223
|
+
raise WorkflowError(
|
|
224
|
+
f"{self._kind}.{name} is already declared as {existing!r}, not {value!r}"
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
def _new_narg(self, value):
|
|
228
|
+
"""Validate an anonymous declaration; return whether it is new."""
|
|
229
|
+
items = list(self._raw_items())
|
|
230
|
+
for _, existing in items:
|
|
231
|
+
if _matches(existing, value):
|
|
232
|
+
return False
|
|
233
|
+
if any(name is not None for name, _ in items):
|
|
234
|
+
raise WorkflowError(
|
|
235
|
+
f"{self._kind}: an anonymous entry cannot follow a named entry"
|
|
236
|
+
)
|
|
237
|
+
return True
|
|
238
|
+
|
|
239
|
+
def _resolve(self, value):
|
|
240
|
+
if isinstance(value, _StaticLambda):
|
|
241
|
+
if self._kind == "input":
|
|
242
|
+
return self._rule._run_input_lambda(value)
|
|
243
|
+
return self._rule._run_param_lambda(value)
|
|
244
|
+
if self._is_path:
|
|
245
|
+
return Path(self._rule.resolve(value))
|
|
246
|
+
if isinstance(value, str):
|
|
247
|
+
return self._rule.resolve(value)
|
|
248
|
+
if isinstance(value, list):
|
|
249
|
+
return [
|
|
250
|
+
self._rule.resolve(item) if isinstance(item, str) else item
|
|
251
|
+
for item in value
|
|
252
|
+
]
|
|
253
|
+
return value
|
|
254
|
+
|
|
255
|
+
def _all_wildcards(self):
|
|
256
|
+
"""Return every wildcard name used by *container*'s path patterns."""
|
|
257
|
+
names: set[str] = set()
|
|
258
|
+
for _, value in self._raw_items():
|
|
259
|
+
for pattern in _iter_str(value):
|
|
260
|
+
names |= get_wildcard_names(pattern)
|
|
261
|
+
return names
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
class _WildcardsView:
|
|
265
|
+
"""The rule's wildcards: call to declare defaults, read to get values.
|
|
266
|
+
|
|
267
|
+
``r.wildcards(sample="s1")`` records the defaults exactly once, while
|
|
268
|
+
``r.wildcards.sample`` and ``r.wildcards["sample"]`` read the effective
|
|
269
|
+
value: declared defaults, then inferred captures, then ``--wildcards``.
|
|
270
|
+
"""
|
|
271
|
+
|
|
272
|
+
def __init__(self, rule: Rule):
|
|
273
|
+
self._rule: Rule
|
|
274
|
+
object.__setattr__(self, "_rule", rule)
|
|
275
|
+
|
|
276
|
+
def __call__(self, **defaults: str):
|
|
277
|
+
return self._rule._declare_wildcards(**defaults)
|
|
278
|
+
|
|
279
|
+
def __getattr__(self, name: str):
|
|
280
|
+
if name.startswith("_"):
|
|
281
|
+
raise AttributeError(name)
|
|
282
|
+
return self._rule.resolve("{" + name + "}")
|
|
283
|
+
|
|
284
|
+
def __getitem__(self, name: str):
|
|
285
|
+
if not isinstance(name, str):
|
|
286
|
+
raise WorkflowError("wildcards key must be a name")
|
|
287
|
+
return getattr(self, name)
|
|
288
|
+
|
|
289
|
+
def __contains__(self, name: str):
|
|
290
|
+
return name in self._rule._effective_wildcards()
|