-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
88 lines (67 loc) · 2.28 KB
/
config.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import argparse
import os
import sys
import json
import logfile
import traceback
import StringIO
import contextlib
import __builtin__
_options_parser = argparse.ArgumentParser(conflict_handler='resolve')
_options_parser.add_argument('--run_dir', '-R', type=str, default=None)
def get_options_parser():
return _options_parser
_options = None
def options(allow_partial=False):
global _options
if allow_partial:
opts, extras = _options_parser.parse_known_args()
return opts
if _options is None:
_options = _options_parser.parse_args()
dump_pretty(vars(_options), 'config.json')
return _options
def get_file_path(filename):
opts = options(allow_partial=True)
if not opts.run_dir:
return None
return os.path.join(opts.run_dir, filename)
def open(filename, *args, **kwargs):
file_path = get_file_path(filename)
if not file_path:
# create a dummy file because we don't have a run dir
return contextlib.closing(StringIO.StringIO())
return __builtin__.open(file_path, *args, **kwargs)
def boolean(arg):
"""Convert a string to a bool treating 'false' and 'no' as False."""
if arg in ('true', 'True', 'yes', '1', 1):
return True
elif arg in ('false', 'False', 'no', '0', 0):
return False
else:
raise argparse.ArgumentTypeError(
'could not interpret "%s" as true or false' % (arg,))
def redirect_output():
outfile = get_file_path('stdout.log')
if outfile is None: return
logfile.log_stdout_to(outfile)
logfile.log_stderr_to(get_file_path('stderr.log'))
redirect_output()
def dump(data, filename, lines=False, *args, **kwargs):
try:
with open(filename, 'w') as outfile:
if lines:
for item in data:
json.dump(item, outfile, *args, **kwargs)
outfile.write('\n')
else:
json.dump(data, outfile, *args, **kwargs)
except IOError:
traceback.print_exc()
print >>sys.stderr, 'Unable to write %s' % filename
except TypeError:
traceback.print_exc()
print >>sys.stderr, 'Unable to write %s' % filename
def dump_pretty(data, filename):
dump(data, filename,
sort_keys=True, indent=2, separators=(',', ': '))