-
Notifications
You must be signed in to change notification settings - Fork 649
Expand file tree
/
Copy pathvalidation.py
More file actions
49 lines (39 loc) · 1.57 KB
/
validation.py
File metadata and controls
49 lines (39 loc) · 1.57 KB
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
import click
from os import path
def check_not_null(ctx, param, value):
"""
Click callback method used to verify command line parameter is not an empty string.
:param ctx: Python Click library Context object.
:param param: Python Click Context object params attribute.
:param value: Value passed in for a given command line parameter.
"""
if not value:
raise click.BadParameter("Value cannot be empty / null")
return value
def check_input_file_type(ctx, param, value):
"""
Click callback method used for file type input validation.
:param ctx: Python Click library Context object.
:param param: Python Click Context object params attribute.
:param value: Value passed in for a given command line parameter.
"""
idx = value.find(".")
if idx == -1 or value[idx:] != ".json.gz":
raise click.BadParameter("Input file type must be '.json.gz'")
return value
def check_filename_filepath(ctx, param, value):
"""
Click callback method used for file path input validation.
:param ctx: Python Click library Context object.
:param param: Python Click Context object params attribute.
:param value: Value passed in for a given command line parameter.
"""
filename = value.strip()
folder_idx = filename.rfind('/')
if filename == '':
raise click.click.BadParameter('Empty filename')
if folder_idx != -1:
parent_folder = filename[0: folder_idx + 1]
if not path.exists(parent_folder):
raise click.BadParameter('File path does not exist.')
return value