Completed
Push — master ( c8d778...5b8e36 )
by Alexandre M.
01:11
created

diff()   B

Complexity

Conditions 3

Size

Total Lines 31

Duplication

Lines 31
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
c 1
b 0
f 1
dl 31
loc 31
rs 8.8571
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)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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,
80
               exist_ok=exist_ok,
81
               verbose=(not quiet))
82
83
84
85 View Code Duplication
@cli.command(context_settings=CONTEXT_SETTINGS)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
86
@click.argument('src_crumb', type=CrumbPath(), callback=check_not_none)
87
@click.argument('dst_crumb', type=CrumbPath(), callback=check_not_none)
88
@click.option('-q', '--quiet', is_flag=True, flag_value=True,
89
              help='Flag to remove verbose.')
90
@click.option('-e', '--exist_ok', is_flag=True, flag_value=True,
91
              help='Flag to allow overwriting destination path.')
92
@click.option('-i', '--ignore', type=str, multiple=True,
93
              help='A global ignore fnmatch expression for the listing. '
94
                   'You can add as many of this argument as you want. '
95
                   'Example: ".*" or "*~"')
96
def link(src_crumb, dst_crumb, quiet, ignore, exist_ok):
97
    """Link one file tree to another file tree. The
98
    structure of the destination tree can be modified.
99
    Only the leaf nodes will be linked, the folder structure above will be
100
    created.
101
102
    Examples: \n
103
    crumb link "/data/hansel/cobre/{sid}/{session}/{img}" "/data/hansel/cobre2/{sid}/{img}" \n
104
    crumb link "cobre/{sid}/{session}/{img:anat*}" "cobre_anat/{sid}/{img}" \n
105
    """
106
    from .. import crumb_link
107
108
    if ignore:
109
        src_crumb._ignore = ignore
110
        dst_crumb._ignore = ignore
111
112
    if not src_crumb.ls():
113
        click.echo('Could not find any file that matched {}.'.format(src_crumb))
114
        exit(-1)
115
116
    crumb_link(src_crumb, dst_crumb,
117
               exist_ok=exist_ok,
118
               verbose=(not quiet))
119
120
121 View Code Duplication
@cli.command(context_settings=CONTEXT_SETTINGS)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
122
@click.argument('crumb1', type=CrumbPath(), callback=check_not_none)
123
@click.argument('crumb2', type=CrumbPath(), callback=check_not_none)
124
@click.option('-o', '--on', type=str, multiple=True,
125
              help='Argument name to check for intersection. You can use this '
126
                   'argument more than once.')
127
@click.option('-b', '--base', type=click.Choice(['1', '2']), default='0',
128
              help='1 or 2, to indicate `crumb1` or `crumb2` as a base crumb2 '
129
                   'to print the results')
130
def intersect(crumb1, crumb2, on, base):
131
    """Return the intersection between crumb1 and crumb2 on a given argument.
132
133
    Will not print full paths, but only values for the crumb arguments in `on`.
134
    Unless you specify the crumb you want to use as a base with the `base`
135
    argument.
136
137
    Examples: \n
138
    crumb intersect --on "sid" "/data/hansel/cobre/{sid}/{session}/{img}" "/data/hansel/cobre2/{sid}/{img}"\n
139
    """
140
    from .. import intersection
141
142
    values = intersection(crumb1, crumb2, on=on)
143
    if base == '0':
144
        _print_values_map_as_csv(values)
145
    else:
146
        if base == '1':
147
            base_crumb = crumb1
148
        else:
149
            base_crumb = crumb2
150
151
        echo_list(base_crumb.build_paths(values))
152
153
154 View Code Duplication
@cli.command(context_settings=CONTEXT_SETTINGS)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
155
@click.argument('crumb1', type=CrumbPath(), callback=check_not_none)
156
@click.argument('crumb2', type=CrumbPath(), callback=check_not_none)
157
@click.option('-o', '--on', type=str, multiple=True,
158
              help='Argument name to check for intersection. You can use this '
159
                   'argument more than once.')
160
@click.option('-b', '--base', type=click.Choice(['1', '2']), default='0',
161
              help='1 or 2, to indicate `crumb1` or `crumb2` as a base crumb2 '
162
                   'to print the results')
163
def diff(crumb1, crumb2, on, base):
164
    """Return the difference crumb1 - crumb2 on a given argument.
165
166
    Will not print full paths, but only values for the crumb arguments in `on`.
167
    Unless you specify the crumb you want to use as a base with the `base`
168
    argument.
169
170
    Examples: \n
171
    crumb diff --on "sid" "/data/hansel/cobre/{sid}/{session}/{img}" "/data/hansel/cobre2/{sid}/{img}"\n
172
    """
173
    from .. import difference
174
175
    values = difference(crumb1, crumb2, on=on)
176
    if base == '0':
177
        _print_values_map_as_csv(values)
178
    else:
179
        if base == '1':
180
            base_crumb = crumb1
181
        else:
182
            base_crumb = crumb2
183
184
        echo_list(base_crumb.build_paths(values))
185