1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- |
3
|
|
|
# vi: set ft=python sts=4 ts=4 sw=4 et: |
4
|
|
|
""" |
5
|
|
|
Crumb class: the smart path model class. |
6
|
|
|
""" |
7
|
|
|
|
8
|
|
|
from collections import Mapping |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
def mktree(crumb, values_map): |
12
|
|
|
""" Create the tree of folders given the values for the crumb arguments |
13
|
|
|
of the current crumb path. |
14
|
|
|
Parameters |
15
|
|
|
---------- |
16
|
|
|
crumb: Crumb |
17
|
|
|
|
18
|
|
|
values_map: Sequence[Sequence[2-Tuple[str, str]]] or Sequence[Dict[str, str]] |
19
|
|
|
The lists of values to substitute each crumb argument that you want. |
20
|
|
|
Do not leave dependent arguments alone. |
21
|
|
|
Example: [[('subject_id', 'pat_0001'), ('session_id', 'session_1')], |
22
|
|
|
[('subject_id', 'pat_0002'), ('session_id', 'session_1')], |
23
|
|
|
.... |
24
|
|
|
] |
25
|
|
|
|
26
|
|
|
Example: [{'subject_id': 'pat_0001', 'session_id': 'session_1'}, |
27
|
|
|
{'subject_id': 'pat_0002', 'session_id': 'session_1'}, |
28
|
|
|
.... |
29
|
|
|
] |
30
|
|
|
|
31
|
|
|
Returns |
32
|
|
|
------- |
33
|
|
|
paths: list of Paths |
34
|
|
|
The paths that have been created. |
35
|
|
|
""" |
36
|
|
|
if values_map is None: |
37
|
|
|
return [crumb.touch()] |
38
|
|
|
|
39
|
|
|
if not isinstance(values_map, (list, dict)): |
40
|
|
|
raise TypeError("Expected keys in `values_map` to be a Sequence, " |
41
|
|
|
"got {}.".format(type(values_map))) |
42
|
|
|
|
43
|
|
|
paths = [] |
44
|
|
|
for idx, aval in enumerate(values_map): |
45
|
|
|
if not isinstance(aval, Mapping): |
46
|
|
|
aval = dict(aval) |
47
|
|
|
|
48
|
|
|
if not set(aval.keys()).issubset(set(crumb._argidx.keys())): |
49
|
|
|
raise ValueError("Expected keys in `values_map` item to be a subset of {}, " |
50
|
|
|
"got {}.".format(crumb._argidx.keys(), aval.keys())) |
51
|
|
|
|
52
|
|
|
rem_deps = crumb._remaining_deps(list(aval.keys())) |
53
|
|
|
if rem_deps: |
54
|
|
|
raise KeyError("Expected `values_map` item to not leave crumbs alone," |
55
|
|
|
" you forgot to add: {} in item {}".format(rem_deps, idx)) |
56
|
|
|
|
57
|
|
|
paths.append(crumb.replace(**aval)) |
58
|
|
|
|
59
|
|
|
return [crumb._touch(str(path)) for path in paths] |
60
|
|
|
|