-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathargparse_utils.py
More file actions
36 lines (27 loc) · 866 Bytes
/
argparse_utils.py
File metadata and controls
36 lines (27 loc) · 866 Bytes
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import os
"""Some helpers for argparse."""
def readable(f):
"""Validates if the pathname is readable (dir or file)."""
f = os.path.normpath(f)
if not os.access(f, os.F_OK | os.R_OK):
raise argparse.ArgumentTypeError("%s is not readable." % f)
return f
def writeable(f):
"""Validates if the pathname is writable (dir or file)."""
f = os.path.normpath(f)
if os.access(f, os.F_OK):
if not os.access(f, os.W_OK):
raise argparse.ArgumentTypeError("%s is not writable." % f)
else:
raise argparse.ArgumentTypeError("%s is not writable." % f)
return f
def int16(s):
"""Validates an hexadecimal (0x...) value"""
try:
i = int(s, 16)
except Exception as e:
raise argparse.ArgumentTypeError(e)
return i