1 | #!python |
||
2 | import click |
||
3 | |||
4 | from .utils import (CONTEXT_SETTINGS, |
||
5 | CrumbPath, |
||
6 | echo_list, |
||
7 | _print_values_map_as_csv, |
||
8 | check_not_none,) |
||
9 | |||
10 | |||
11 | # declare the CLI group |
||
12 | @click.group(context_settings=CONTEXT_SETTINGS) |
||
13 | def cli(): |
||
14 | pass |
||
15 | |||
16 | |||
17 | @cli.command(context_settings=CONTEXT_SETTINGS) |
||
18 | @click.argument('crumb', type=CrumbPath(), callback=check_not_none) |
||
19 | @click.option('-i', '--ignore', type=str, multiple=True, |
||
20 | help='A global ignore fnmatch expression for the listing. ' |
||
21 | 'You can add as many of this argument as you want. ' |
||
22 | 'Example: ".*" or "*~"') |
||
23 | @click.option('-a', '--arg', type=str, |
||
24 | help='Name of the argument in `crumb` to print the values from.' |
||
25 | 'Will not print full paths, but only values for this ' |
||
26 | 'crumb argument.') |
||
27 | def ls(crumb, ignore, arg): |
||
28 | """List all the possible values that match the given crumb path. |
||
29 | |||
30 | Examples: \n |
||
31 | crumb ls "/data/hansel/cobre/{sid:4*100}/{session}/{img}"\n |
||
32 | crumb ls -i ".*" "/data/hansel/cobre/{sid}/{session}/{img:anat*}"\n |
||
33 | crumb ls -a "sid" "/data/hansel/cobre/{sid}/{session}/{img:anat*}"\n |
||
34 | """ |
||
35 | if not crumb.isabs(): |
||
36 | crumb = crumb.abspath() |
||
37 | |||
38 | crumb._ignore = ignore |
||
39 | if arg: |
||
40 | lst = crumb[arg] |
||
41 | else: |
||
42 | lst = crumb.ls() |
||
43 | |||
44 | if not lst: |
||
45 | exit(-1) |
||
46 | |||
47 | echo_list(lst) |
||
48 | |||
49 | |||
50 | View Code Duplication | @cli.command(context_settings=CONTEXT_SETTINGS) |
|
51 | @click.argument('src_crumb', type=CrumbPath(), callback=check_not_none) |
||
52 | @click.argument('dst_crumb', type=CrumbPath(), callback=check_not_none) |
||
53 | @click.option('-q', '--quiet', is_flag=True, flag_value=True, |
||
54 | help='Flag to remove verbose.') |
||
55 | @click.option('-e', '--exist_ok', is_flag=True, flag_value=True, |
||
56 | help='Flag to allow overwriting destination path.') |
||
57 | @click.option('-i', '--ignore', type=str, multiple=True, |
||
58 | help='A global ignore fnmatch expression for the listing. ' |
||
59 | 'You can add as many of this argument as you want. ' |
||
60 | 'Example: ".*" or "*~"') |
||
61 | def copy(src_crumb, dst_crumb, quiet, ignore, exist_ok): |
||
62 | """Copy one file tree to another file tree. The |
||
63 | structure of the destination tree can be modified. |
||
64 | |||
65 | Examples: \n |
||
66 | crumb copy "/data/hansel/cobre/{sid}/{session}/{img}" "/data/hansel/cobre2/{sid}/{img}" \n |
||
67 | crumb copy "cobre/{sid}/{session}/{img:anat*}" "cobre_anat/{sid}/{img}" \n |
||
68 | """ |
||
69 | from .. import crumb_copy |
||
70 | |||
71 | if ignore: |
||
72 | src_crumb._ignore = ignore |
||
73 | dst_crumb._ignore = ignore |
||
74 | |||
75 | if not src_crumb.ls(): |
||
76 | click.echo('Could not find any file that matched {}.'.format(src_crumb)) |
||
77 | exit(-1) |
||
78 | |||
79 | crumb_copy(src_crumb, dst_crumb, exist_ok=exist_ok, verbose=(not quiet)) |
||
80 | |||
81 | |||
82 | @cli.command(context_settings=CONTEXT_SETTINGS) |
||
83 | @click.argument('src_crumb', type=CrumbPath(), callback=check_not_none) |
||
84 | View Code Duplication | @click.argument('dst_crumb', type=CrumbPath(), callback=check_not_none) |
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
85 | @click.option('-q', '--quiet', is_flag=True, flag_value=True, |
||
86 | help='Flag to remove verbose.') |
||
87 | @click.option('-e', '--exist_ok', is_flag=True, flag_value=True, |
||
88 | help='Flag to allow overwriting destination path.') |
||
89 | @click.option('-i', '--ignore', type=str, multiple=True, |
||
90 | help='A global ignore fnmatch expression for the listing. ' |
||
91 | 'You can add as many of this argument as you want. ' |
||
92 | 'Example: ".*" or "*~"') |
||
93 | def link(src_crumb, dst_crumb, quiet, ignore, exist_ok): |
||
94 | """Link one file tree to another file tree. The |
||
95 | structure of the destination tree can be modified. |
||
96 | Only the leaf nodes will be linked, the folder structure above will be |
||
97 | created. |
||
98 | |||
99 | Examples: \n |
||
100 | crumb link "/data/hansel/cobre/{sid}/{session}/{img}" "/data/hansel/cobre2/{sid}/{img}" \n |
||
101 | crumb link "cobre/{sid}/{session}/{img:anat*}" "cobre_anat/{sid}/{img}" \n |
||
102 | """ |
||
103 | from .. import crumb_link |
||
104 | |||
105 | if ignore: |
||
106 | src_crumb._ignore = ignore |
||
107 | dst_crumb._ignore = ignore |
||
108 | |||
109 | if not src_crumb.ls(): |
||
110 | click.echo('Could not find any file that matched {}.'.format(src_crumb)) |
||
111 | exit(-1) |
||
112 | |||
113 | crumb_link(src_crumb, dst_crumb, exist_ok=exist_ok, verbose=(not quiet)) |
||
114 | |||
115 | |||
116 | @cli.command(context_settings=CONTEXT_SETTINGS) |
||
117 | @click.argument('crumb1', type=CrumbPath(), callback=check_not_none) |
||
118 | @click.argument('crumb2', type=CrumbPath(), callback=check_not_none) |
||
119 | @click.option('-o', '--on', type=str, multiple=True, |
||
120 | View Code Duplication | help='Argument name to check for intersection. You can use this ' |
|
0 ignored issues
–
show
|
|||
121 | 'argument more than once.') |
||
122 | @click.option('-b', '--base', type=click.Choice(['1', '2']), default='0', |
||
123 | help='1 or 2, to indicate `crumb1` or `crumb2` as a base crumb2 ' |
||
124 | 'to print the results') |
||
125 | def intersect(crumb1, crumb2, on, base): |
||
126 | """Return the intersection between crumb1 and crumb2 on a given argument. |
||
127 | |||
128 | Will not print full paths, but only values for the crumb arguments in `on`. |
||
129 | Unless you specify the crumb you want to use as a base with the `base` |
||
130 | argument. |
||
131 | |||
132 | Examples: \n |
||
133 | crumb intersect --on "sid" "/data/hansel/cobre/{sid}/{session}/{img}" "/data/hansel/cobre2/{sid}/{img}"\n |
||
134 | """ |
||
135 | from .. import intersection |
||
136 | |||
137 | values = intersection(crumb1, crumb2, on=on) |
||
138 | if base == '0': |
||
139 | _print_values_map_as_csv(values) |
||
140 | else: |
||
141 | if base == '1': |
||
142 | base_crumb = crumb1 |
||
143 | else: |
||
144 | base_crumb = crumb2 |
||
145 | |||
146 | echo_list(base_crumb.build_paths(values)) |
||
147 | |||
148 | |||
149 | @cli.command(context_settings=CONTEXT_SETTINGS) |
||
150 | @click.argument('crumb1', type=CrumbPath(), callback=check_not_none) |
||
151 | @click.argument('crumb2', type=CrumbPath(), callback=check_not_none) |
||
152 | @click.option('-o', '--on', type=str, multiple=True, |
||
153 | View Code Duplication | help='Argument name to check for intersection. You can use this ' |
|
0 ignored issues
–
show
|
|||
154 | 'argument more than once.') |
||
155 | @click.option('-b', '--base', type=click.Choice(['1', '2']), default='0', |
||
156 | help='1 or 2, to indicate `crumb1` or `crumb2` as a base crumb2 ' |
||
157 | 'to print the results') |
||
158 | def diff(crumb1, crumb2, on, base): |
||
159 | """Return the difference crumb1 - crumb2 on a given argument. |
||
160 | |||
161 | Will not print full paths, but only values for the crumb arguments in `on`. |
||
162 | Unless you specify the crumb you want to use as a base with the `base` |
||
163 | argument. |
||
164 | |||
165 | Examples: \n |
||
166 | crumb diff --on "sid" "/data/hansel/cobre/{sid}/{session}/{img}" "/data/hansel/cobre2/{sid}/{img}"\n |
||
167 | """ |
||
168 | from .. import difference |
||
169 | |||
170 | values = difference(crumb1, crumb2, on=on) |
||
171 | if base == '0': |
||
172 | _print_values_map_as_csv(values) |
||
173 | else: |
||
174 | if base == '1': |
||
175 | base_crumb = crumb1 |
||
176 | else: |
||
177 | base_crumb = crumb2 |
||
178 | |||
179 | echo_list(base_crumb.build_paths(values)) |
||
180 |