|
6 | 6 | from pathlib import Path |
7 | 7 | from typing import Iterable |
8 | 8 | from typing import Optional |
| 9 | +from typing import Sequence |
9 | 10 | from typing import Union |
10 | 11 |
|
11 | 12 | from _pytask.config import hookimpl |
@@ -33,14 +34,15 @@ def latex(options: Optional[Union[str, Iterable[str]]] = None): |
33 | 34 | """ |
34 | 35 | if options is None: |
35 | 36 | options = DEFAULT_OPTIONS.copy() |
36 | | - elif isinstance(options, str): |
37 | | - options = [options] |
38 | | - |
| 37 | + else: |
| 38 | + options = _to_list(options) |
| 39 | + options = [str(i) for i in options] |
39 | 40 | return options |
40 | 41 |
|
41 | 42 |
|
42 | 43 | def compile_latex_document(latex): |
43 | 44 | """Replaces the dummy function provided by the user.""" |
| 45 | + print("Executing " + " ".join(latex) + ".") # noqa: T001 |
44 | 46 | subprocess.run(latex, check=True) |
45 | 47 |
|
46 | 48 |
|
@@ -101,9 +103,7 @@ def pytask_collect_task_teardown(session, task): |
101 | 103 |
|
102 | 104 |
|
103 | 105 | def _get_node_from_dictionary(obj, key, fallback=0): |
104 | | - if isinstance(obj, Path): |
105 | | - pass |
106 | | - elif isinstance(obj, dict): |
| 106 | + if isinstance(obj, dict): |
107 | 107 | obj = obj.get(key) or obj.get(fallback) |
108 | 108 | return obj |
109 | 109 |
|
@@ -135,18 +135,22 @@ def _add_latex_dependencies_retroactively(task, session): |
135 | 135 | # Scan the LaTeX document for included files. |
136 | 136 | latex_dependencies = set(scan(source.path)) |
137 | 137 |
|
138 | | - # Remove duplicated dependencies which have already been added by the user. |
| 138 | + # Remove duplicated dependencies which have already been added by the user and those |
| 139 | + # which do not exist. |
139 | 140 | existing_paths = { |
140 | 141 | i.path for i in task.depends_on.values() if isinstance(i, FilePathNode) |
141 | 142 | } |
142 | | - new_dependencies = latex_dependencies - existing_paths |
| 143 | + new_deps = latex_dependencies - existing_paths |
| 144 | + new_existing_deps = {i for i in new_deps if i.exists()} |
143 | 145 |
|
| 146 | + # Put scanned dependencies in a dictionary with incrementing keys. |
144 | 147 | used_integer_keys = [i for i in task.depends_on if isinstance(i, int)] |
145 | 148 | max_int = max(used_integer_keys) if used_integer_keys else 0 |
| 149 | + new_existing_deps = dict(enumerate(new_existing_deps, max_int + 1)) |
146 | 150 |
|
147 | | - new_dependencies = dict(enumerate(new_dependencies, max_int + 1)) |
| 151 | + # Collect new dependencies and add them to the task. |
148 | 152 | collected_dependencies = _collect_nodes( |
149 | | - session, task.path, task.name, new_dependencies |
| 153 | + session, task.path, task.name, new_existing_deps |
150 | 154 | ) |
151 | 155 | task.depends_on = {**task.depends_on, **collected_dependencies} |
152 | 156 |
|
@@ -211,3 +215,29 @@ def _prepare_cmd_options(session, task, args): |
211 | 215 | latex_document.as_posix(), |
212 | 216 | ] |
213 | 217 | ) |
| 218 | + |
| 219 | + |
| 220 | +def _to_list(scalar_or_iter): |
| 221 | + """Convert scalars and iterables to list. |
| 222 | +
|
| 223 | + Parameters |
| 224 | + ---------- |
| 225 | + scalar_or_iter : str or list |
| 226 | +
|
| 227 | + Returns |
| 228 | + ------- |
| 229 | + list |
| 230 | +
|
| 231 | + Examples |
| 232 | + -------- |
| 233 | + >>> _to_list("a") |
| 234 | + ['a'] |
| 235 | + >>> _to_list(["b"]) |
| 236 | + ['b'] |
| 237 | +
|
| 238 | + """ |
| 239 | + return ( |
| 240 | + [scalar_or_iter] |
| 241 | + if isinstance(scalar_or_iter, str) or not isinstance(scalar_or_iter, Sequence) |
| 242 | + else list(scalar_or_iter) |
| 243 | + ) |
0 commit comments