This repository was archived by the owner on Oct 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigfile.py
109 lines (95 loc) · 3.54 KB
/
configfile.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
############################################################################
# #
# Copyright (c) 2017 eBay Inc. #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# #
############################################################################
from __future__ import print_function
from __future__ import division
import re
import os
from compat import quote_plus
def get_config( filename, verbose=True ):
with open(filename) as F:
c = parse_config(F.read(), filename)
if verbose:
print_config(c)
return c
def print_config(config):
print('-'*79)
X = {
'method_directories' : lambda x : ', '.join('\"%s\"' % t for t in x),
'workspace' : lambda x : ''.join('\n - %-12s %-40s %2d' % (s, t[0], t[1]) for s, t in x.items()),
}
for x, y in config.items():
print(" %-30s : " % (x,), end="")
if x in X:
print(X[x](y))
else:
print(y)
print('-'*79)
_re_var = re.compile(r'\$\{([^\}=]*)(?:=([^\}]*))?\}')
def _interpolate(s):
"""Replace ${FOO=BAR} with os.environ.get('FOO', 'BAR')
(just ${FOO} is of course also supported, but not $FOO)"""
return _re_var.subn(lambda m: os.environ.get(m.group(1), m.group(2)), s)[0]
def resolve_socket_url(path):
if '://' in path:
return path
else:
return 'unixhttp://' + quote_plus(os.path.realpath(path))
def parse_config(string, filename=None):
ret = {}
for line in string.split('\n'):
line = line.split('#')[0].strip()
if len(line)==0:
continue
try:
key, val = line.split('=', 1)
val = _interpolate(val)
if key =='workspace':
# create a dict {name : (path, slices), ...}
ret.setdefault(key, {})
val = val.split(':')
name = val[0]
path = val[1]
if len(val)==2:
# there is no slice information
slices = -1
else:
slices = val[2]
ret[key][name] = (path, int(slices))
elif key in ('remote_workspaces', 'method_directories',):
# create a set of (name, ...)
ret.setdefault(key, set())
ret[key].update(val.split(','))
elif key == 'urd':
ret[key] = resolve_socket_url(val)
else:
ret[key] = val
except:
print("Error parsing config %s: \"%s\"" % (filename, line,))
if 'workspace' not in ret:
raise Exception("Error, missing workspace in config " + filename)
return ret
def sanity_check(config_dict):
ok = True
if 'main_workspace' not in config_dict:
print("# Error in configfile, must specify main_workspace.")
ok = False
if 'workspace' not in config_dict:
print("# Error in configfile, must specify at least one workspace.")
ok = False
if not ok:
exit(1)