-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_params.py
63 lines (47 loc) · 1.58 KB
/
custom_params.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import click
import os
import stat
class Method(click.ParamType):
def __init__(self):
self.name = "method"
def convert(self, value, param, ctx):
if value in ["mse", "nrmse", "psnr", "ssim"]:
return value
else:
self.fail('Must be one of: mse, nrmse, psnr, ssim.', param, ctx)
class PathList(click.ParamType):
def __init__(self):
self.name = "path_list"
self.path_type = 'File'
def check_path(self, path, param, ctx):
try:
st = os.stat(path)
except OSError:
self.fail('%s "%s" does not exist.' % (
self.path_type,
path
), param, ctx)
if stat.S_ISDIR(st.st_mode):
self.fail('%s "%s" is a directory.' % (
self.path_type,
path
), param, ctx)
if not os.access(path, os.R_OK):
self.fail('%s "%s" is not readable.' % (
self.path_type,
path
), param, ctx)
def convert(self, value, param, ctx):
out = []
if len(value.split(',')) > 1:
if value[0] is not '[' and value[-1] is not ']':
self.fail('List of paths must be comma separated with no spaces and encased in [].', param, ctx)
value = value[1:-1]
paths = value.split(',')
for path in paths:
self.check_path(path, param, ctx)
out.append(path)
else:
self.check_path(value, param, ctx)
out.append(value)
return out