1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
""" |
3
|
|
|
Utilities for the CLI functions. |
4
|
|
|
""" |
5
|
|
|
import os.path as path |
6
|
|
|
import re |
7
|
|
|
|
8
|
|
|
import click |
9
|
|
|
|
10
|
|
|
from hansel import Crumb |
11
|
|
|
|
12
|
|
|
# different context options |
13
|
|
|
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help']) |
14
|
|
|
UNKNOWN_OPTIONS = dict( |
15
|
|
|
allow_extra_args=True, |
16
|
|
|
ignore_unknown_options=True |
17
|
|
|
) |
18
|
|
|
|
19
|
|
|
ExistingDirPath = click.Path(exists=True, file_okay=False, resolve_path=True) |
20
|
|
|
ExistingFilePath = click.Path(exists=True, dir_okay=False, resolve_path=True) |
21
|
|
|
UnexistingFilePath = click.Path(dir_okay=False, resolve_path=True) |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
def check_not_none(ctx, param, value): |
25
|
|
|
if value is None: |
26
|
|
|
raise click.BadParameter('got {}.'.format(value)) |
27
|
|
|
return value |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
class RegularExpression(click.ParamType): |
31
|
|
|
name = 'regex' |
32
|
|
|
|
33
|
|
|
def convert(self, value, param, ctx): |
34
|
|
|
try: |
35
|
|
|
rex = re.compile(value, re.IGNORECASE) |
36
|
|
|
except ValueError: |
37
|
|
|
self.fail('%s is not a valid regular expression.' % value, param, ctx) |
38
|
|
|
else: |
39
|
|
|
return rex |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
class CrumbPath(click.ParamType): |
43
|
|
|
name = 'crumb' |
44
|
|
|
|
45
|
|
|
def convert(self, value, param, ctx): |
46
|
|
|
try: |
47
|
|
|
cr = Crumb(path.expanduser(value), ignore_list=['.*']) |
48
|
|
|
except ValueError: |
49
|
|
|
self.fail('%s is not a valid crumb path.' % value, param, ctx) |
50
|
|
|
else: |
51
|
|
|
return cr |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
def echo_list(alist): |
55
|
|
|
for i in alist: |
56
|
|
|
click.echo(i) |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
def _print_values_map_as_csv(list_of_lists): |
60
|
|
|
for values in list_of_lists: |
61
|
|
|
click.echo(','.join([item[1] for item in values])) |
62
|
|
|
|