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

hansel/cli/cli.py (1 issue)

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
    """List all the possible values that match the given crumb path.
28
29
    Examples: \n
30
    crumb ls "/data/hansel/cobre/{sid:4*100}/{session}/{img}"\n
31
    crumb ls -i ".*" "/data/hansel/cobre/{sid}/{session}/{img:anat*}"\n
32
    crumb ls -a "sid" "/data/hansel/cobre/{sid}/{session}/{img:anat*}"\n
33
    """
34
    if not crumb.isabs():
35
        crumb = crumb.abspath()
36
37
    crumb._ignore = ignore
38
    if arg:
39
        lst = crumb[arg]
40
    else:
41
        lst = crumb.ls()
42
43
    if not lst:
44
        exit(-1)
45
46
    echo_list(lst)
47
48
49
@cli.command(context_settings=CONTEXT_SETTINGS)
50 View Code Duplication
@click.argument('src_crumb', type=CrumbPath(), callback=check_not_none)
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
51
@click.argument('dst_crumb', type=CrumbPath(), callback=check_not_none)
52
@click.option('-q', '--quiet', is_flag=True, flag_value=True,
53
              help='Flag to remove verbose.')
54
@click.option('-e', '--exist_ok', is_flag=True, flag_value=True,
55
              help='Flag to allow overwriting destination path.')
56
@click.option('-i', '--ignore', type=str, multiple=True,
57
              help='A global ignore fnmatch expression for the listing. '
58
                   'You can add as many of this argument as you want. '
59
                   'Example: ".*" or "*~"')
60
def copy(src_crumb, dst_crumb, quiet, ignore, exist_ok):
61
    """Copy one file tree to another file tree. The
62
    structure of the destination tree can be modified.
63
64
    Examples: \n
65
    crumb copy "/data/hansel/cobre/{sid}/{session}/{img}" "/data/hansel/cobre2/{sid}/{img}" \n
66
    crumb copy "cobre/{sid}/{session}/{img:anat*}" "cobre_anat/{sid}/{img}" \n
67
    """
68
    from .. import crumb_copy
69
70
    if ignore:
71
        src_crumb._ignore = ignore
72
        dst_crumb._ignore = ignore
73
74
    if not src_crumb.ls():
75
        click.echo('Could not find any file that matched {}.'.format(src_crumb))
76
        exit(-1)
77
78
    crumb_copy(src_crumb, dst_crumb,
79
               exist_ok=exist_ok,
80
               verbose=(not quiet))
81
82
83
84
@cli.command(context_settings=CONTEXT_SETTINGS)
85 View Code Duplication
@click.argument('src_crumb', type=CrumbPath(), callback=check_not_none)
86
@click.argument('dst_crumb', type=CrumbPath(), callback=check_not_none)
87
@click.option('-q', '--quiet', is_flag=True, flag_value=True,
88
              help='Flag to remove verbose.')
89
@click.option('-e', '--exist_ok', is_flag=True, flag_value=True,
90
              help='Flag to allow overwriting destination path.')
91
@click.option('-i', '--ignore', type=str, multiple=True,
92
              help='A global ignore fnmatch expression for the listing. '
93
                   'You can add as many of this argument as you want. '
94
                   'Example: ".*" or "*~"')
95
def link(src_crumb, dst_crumb, quiet, ignore, exist_ok):
96
    """Link one file tree to another file tree. The
97
    structure of the destination tree can be modified.
98
    Only the leaf nodes will be linked, the folder structure above will be
99
    created.
100
101
    Examples: \n
102
    crumb link "/data/hansel/cobre/{sid}/{session}/{img}" "/data/hansel/cobre2/{sid}/{img}" \n
103
    crumb link "cobre/{sid}/{session}/{img:anat*}" "cobre_anat/{sid}/{img}" \n
104
    """
105
    from .. import crumb_link
106
107
    if ignore:
108
        src_crumb._ignore = ignore
109
        dst_crumb._ignore = ignore
110
111
    if not src_crumb.ls():
112
        click.echo('Could not find any file that matched {}.'.format(src_crumb))
113
        exit(-1)
114
115
    crumb_link(src_crumb, dst_crumb,
116
               exist_ok=exist_ok,
117
               verbose=(not quiet))
118
119
120
@cli.command(context_settings=CONTEXT_SETTINGS)
121
@click.argument('crumb1', type=CrumbPath(), callback=check_not_none)
122
@click.argument('crumb2', type=CrumbPath(), callback=check_not_none)
123
@click.option('-o', '--on', type=str, multiple=True,
124
              help='Argument name to check for intersection. You can use this '
125
                   'argument more than once.')
126
@click.option('-a', '--arg', type=str,
127
              help='Will not print full paths, but only values for this '
128
                   'crumb argument.')
129
def intersect(crumb1, crumb2, on, arg):
130
    """Return the intersection between crumb1 and crumb2 on a given 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
    intersects = intersection(crumb1, crumb2, on=on)
138
    for values in intersects:
139
        click.echo(', '.join([arg[1] for arg in values]))
140