-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
28 lines (24 loc) · 991 Bytes
/
utils.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
import argparse
import os
class IsReadableDir(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
prospective_dir = values
if not os.path.isdir(prospective_dir):
raise argparse.ArgumentTypeError(
"{0} is not a valid path".format(prospective_dir)
)
if os.access(prospective_dir, os.R_OK):
setattr(namespace, self.dest, prospective_dir)
else:
raise argparse.ArgumentTypeError(
"{0} is not a readable directory".format(prospective_dir)
)
class IsValidFile(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
prospective_file = values
if not os.path.exists(prospective_file):
raise argparse.ArgumentTypeError(
"{0} is not a valid file".format(prospective_file)
)
else:
setattr(namespace, self.dest, prospective_file)