Completed
Push — master ( 4f6468...f17592 )
by Alexandre M.
01:09
created

link()   B

Complexity

Conditions 3

Size

Total Lines 35

Duplication

Lines 35
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
dl 35
loc 35
rs 8.8571
c 0
b 0
f 0
1
#!python
2
import click
3
4
from .utils import (CONTEXT_SETTINGS,
5
                    CrumbPath,
6
                    echo_list,
7
                    check_not_none,)
8
9
10
# declare the CLI group
11
@click.group(context_settings=CONTEXT_SETTINGS)
12
def cli():
13
    pass
14
15
16
@cli.command(context_settings=CONTEXT_SETTINGS)
17
@click.argument('crumb', type=CrumbPath(), callback=check_not_none)
18
@click.option('-i', '--ignore', type=str, multiple=True,
19
              help='A global ignore fnmatch expression for the listing. '
20
                   'You can add as many of this argument as you want. '
21
                   'Example: ".*" or "*~"')
22
@click.option('-a', '--arg', type=str,
23
              help='Name of the argument in `crumb` to print the values from.'
24
                   'Will not print full paths, but only values for this '
25
                   'crumb argument.')
26
def ls(crumb, ignore, arg):
27
    """Uses hansel.Crumb to list all the possible values that match the
28
    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 Alexandre Manhaes Savio
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, link, quiet, ignore, exist_ok):
62
    """Uses hansel.Crumb to 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
               make_links=False,
81
               exist_ok=exist_ok,
82
               verbose=(not quiet))
83
84
85
86 View Code Duplication
@cli.command(context_settings=CONTEXT_SETTINGS)
0 ignored issues
show
Duplication introduced by Alexandre M. Savio
This code seems to be duplicated in your project.
Loading history...
87
@click.argument('src_crumb', type=CrumbPath(), callback=check_not_none)
88
@click.argument('dst_crumb', type=CrumbPath(), callback=check_not_none)
89
@click.option('-q', '--quiet', is_flag=True, flag_value=True,
90
              help='Flag to remove verbose.')
91
@click.option('-e', '--exist_ok', is_flag=True, flag_value=True,
92
              help='Flag to allow overwriting destination path.')
93
@click.option('-i', '--ignore', type=str, multiple=True,
94
              help='A global ignore fnmatch expression for the listing. '
95
                   'You can add as many of this argument as you want. '
96
                   'Example: ".*" or "*~"')
97
def link(src_crumb, dst_crumb, link, quiet, ignore, exist_ok):
98
    """Uses hansel.Crumb to link one file tree to another file tree. The
99
    structure of the destination tree can be modified.
100
    Only the leaf nodes will be linked, the folder structure above will be
101
    created.
102
103
    Examples: \n
104
    crumb link "/data/hansel/cobre/{sid}/{session}/{img}" "/data/hansel/cobre2/{sid}/{img}" \n
105
    crumb link "cobre/{sid}/{session}/{img:anat*}" "cobre_anat/{sid}/{img}" \n
106
    """
107
    from .. import crumb_copy
108
109
    if ignore:
110
        src_crumb._ignore = ignore
111
        dst_crumb._ignore = ignore
112
113
    if not src_crumb.ls():
114
        click.echo('Could not find any file that matched {}.'.format(src_crumb))
115
        exit(-1)
116
117
    crumb_copy(src_crumb, dst_crumb,
118
               make_links=True,
119
               exist_ok=exist_ok,
120
               verbose=(not quiet))
121
122
123
@cli.command(context_settings=CONTEXT_SETTINGS)
124
@click.argument('crumb1', type=CrumbPath(), callback=check_not_none)
125
@click.argument('crumb2', type=CrumbPath(), callback=check_not_none)
126
@click.option('-o', '--on', type=str, multiple=True,
127
              help='Argument name to check for intersection. You can use this '
128
                   'argument more than once.')
129
@click.option('-a', '--arg', type=str,
130
              help='Will not print full paths, but only values for this '
131
                   'crumb argument.')
132
def intersect(crumb1, crumb2, on, arg):
133
    """Uses hansel.Crumb to copy one file tree to another file tree. The
134
    structure of the destination tree can be modified.
135
136
    Examples: \n
137
    crumb intersect --on "sid" "/data/hansel/cobre/{sid}/{session}/{img}" "/data/hansel/cobre2/{sid}/{img}"\n
138
    """
139
    from .. import intersection
140
141
    intersects = intersection(crumb1, crumb2, on=on)
142
    for values in intersects:
143
        click.echo(', '.join([arg[1] for arg in values]))
144