Completed
Push — master ( 0e7781...b2dc6b )
by Alexandre M.
01:04
created

copy()   B

Complexity

Conditions 2

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 2
c 2
b 0
f 2
dl 0
loc 31
rs 8.8571
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
    crumb._ignore = ignore
36
    if arg:
37
        echo_list(crumb[arg])
38
    else:
39
        echo_list(crumb.ls())
40
41
42
@cli.command(context_settings=CONTEXT_SETTINGS)
43
@click.argument('src_path', type=CrumbPath(), callback=check_not_none)
44
@click.argument('dst_path', type=CrumbPath(), callback=check_not_none)
45
@click.option('-l', '--link', is_flag=True, flag_value=True,
46
              help='Flag to indicate to whether make links instead of copying.')
47
@click.option('-q', '--quiet', is_flag=True, flag_value=True,
48
              help='Flag to remove verbose.')
49
@click.option('-e', '--exist_ok', is_flag=True, flag_value=True,
50
              help='Flag to allow overwriting destination path.')
51
@click.option('-i', '--ignore', type=str, multiple=True,
52
              help='A global ignore fnmatch expression for the listing. '
53
                   'You can add as many of this argument as you want. '
54
                   'Example: ".*" or "*~"')
55
def copy(src_crumb, dst_crumb, link, quiet, ignore, exist_ok):
56
    """Uses hansel.Crumb to copy one file tree to another file tree. The
57
    structure of the destination tree can be modified.
58
59
    Examples: \n
60
    crumb copy --link "/data/hansel/cobre/{sid}/{session}/{img}" "/data/hansel/cobre2/{sid}/{img}" \n
61
    crumb copy "cobre/{sid}/{session}/{img:anat*}" "cobre_anat/{sid}/{img}" \n
62
    """
63
    from .. import crumb_copy
64
65
    if ignore:
66
        src_crumb._ignore = ignore
67
        dst_crumb._ignore = ignore
68
69
    crumb_copy(src_crumb, dst_crumb,
70
               make_links=link,
71
               exist_ok=exist_ok,
72
               verbose=(not quiet))
73
74
75
@cli.command(context_settings=CONTEXT_SETTINGS)
76
@click.argument('crumb1', type=CrumbPath(), callback=check_not_none)
77
@click.argument('crumb2', type=CrumbPath(), callback=check_not_none)
78
@click.option('-o', '--on', type=str, multiple=True,
79
              help='Argument name to check for intersection. You can use this '
80
                   'argument more than once.')
81
@click.option('-a', '--arg', type=str,
82
              help='Will not print full paths, but only values for this '
83
                   'crumb argument.')
84
def intersect(crumb1, crumb2, on, arg):
85
    """Uses hansel.Crumb to copy one file tree to another file tree. The
86
    structure of the destination tree can be modified.
87
88
    Examples: \n
89
    crumb intersect --on "sid" "/data/hansel/cobre/{sid}/{session}/{img}" "/data/hansel/cobre2/{sid}/{img}"\n
90
    """
91
    from .. import intersection
92
93
    intersects = intersection(crumb1, crumb2, on=on)
94
    for values in intersects:
95
        click.echo(', '.join([arg[1] for arg in values]))
96