-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilities.py
executable file
·36 lines (33 loc) · 1.21 KB
/
utilities.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
import argparse
import os
def get_args():
"""
:return args: Namespace object containing the command line arguments
"""
parser = argparse.ArgumentParser("CABINET")
# Required positional arguments
parser.add_argument(
"parameter_json", type=valid_readable_json,
help=("Required. Valid path to existing readable parameter .JSON "
"file. See README.md and example parameter .JSON files for more "
"information on parameters.")
)
# Option flag arguments
parser.add_argument(
"--dryrun", action="store_true",
help=("Include this flag to validate the parameter JSON without running any containers.")
)
args = parser.parse_args()
return args
def valid_readable_json(path):
"""
:param path: Parameter to check if it represents a valid .json file path
:return: String representing a valid .json file path
"""
try:
assert os.access(path, os.R_OK)
assert os.path.splitext(path)[-1] == ".json"
return path
except (OSError, TypeError, AssertionError, ValueError,
argparse.ArgumentTypeError):
raise argparse.ArgumentTypeError(f"{path} is not a path to a readable .json file")