]> mj.ucw.cz Git - eval.git/blob - t/moe/config.py
a96fdb6207e2dea03870934573b26f39fcff001e
[eval.git] / t / moe / config.py
1 #!/usr/bin/env python
2
3 import re
4 import sys
5 import moe
6
7 key_pattern = re.compile("^[A-Za-z0-9_-]+$")
8 ref_pattern = re.compile("^[A-Za-z0-9_-]+")
9
10 class MoeConfigInvalid(moe.MoeError):
11     pass
12
13 class MoeConfigEvalError(moe.MoeError):
14     pass
15
16 class MoeConfig:
17     """Moe configuration file. Should be immutable once a part of a stack."""
18
19     def __init__(self, file=None, name=None, type="<unnamed>"):
20         self.vars = {}
21         self.type = type
22         if file is not None:
23             self.load(file)
24         elif name is not None:
25             self.name = name
26             try:
27                 file = open(name, "r")
28             except IOError, err:
29                 raise MoeConfigInvalid, "Cannot open configuration file %s: %s" % (name, err.strerror)
30             else:
31                 self.load(file)
32
33     def set(self, k, v):
34         self.vars[k] = [("s", v)]
35
36     def parse_line(self, x):
37         x = x.rstrip("\n").lstrip(" \t")
38         if x=="" or x.startswith("#"):
39             pass
40         else:
41             sep = x.find("=")
42             if sep >= 0:
43                 k = x[:sep]
44                 v = x[sep+1:]
45                 if k.endswith("+"):
46                     k = k[:-1]
47                     if not self.vars.has_key(k):
48                         self.vars[k] = [("a","")];
49                     else:
50                         self.vars[k] += [("s"," ")]
51                 else:
52                     self.vars[k] = []
53                 if not key_pattern.match(k):
54                     raise MoeConfigInvalid, "Malformed name of configuration variable"
55                 if v.startswith("'"):
56                     v=v[1:]
57                     if not v.endswith("'"):
58                         raise MoeConfigInvalid, "Misquoted string"
59                     self.vars[k].append(("s", v[:-1]))
60                 elif v.startswith('"'):
61                     v=v[1:]
62                     if not v.endswith('"'):
63                         raise MoeConfigInvalid, "Misquoted string"
64                     self.parse_interpolated(self.vars[k], v[:-1])
65                 else:
66                     self.parse_interpolated(self.vars[k], v)
67             else:
68                 raise MoeConfigInvalid, "Parse error"
69
70     def load(self, file):
71         lino = 0
72         for x in file.readlines():
73             lino += 1
74             try:
75                 self.parse_line(x)
76             except MoeConfigInvalid, x:
77                 msg = x.message + " at line " + str(lino)
78                 if hasattr(self, "name"):
79                     msg += " of " + self.name
80                 raise MoeConfigInvalid, msg
81
82     def parse_interpolated(self, list, s):
83         while s<>"":
84             if s.startswith("$"):
85                 s = s[1:]
86                 if s.startswith("{"):
87                     p = s.find("}")
88                     if not p:
89                         raise MoeConfigInvalid, "Unbalanced braces"
90                     k, s = s[1:p], s[p+1:]
91                     if not key_pattern.match(k):
92                         raise MoeConfigInvalid, "Invalid variable name"
93                 else:
94                     m = ref_pattern.match(s)
95                     if m:
96                         k, s = s[:m.end()], s[m.end():]
97                     else:
98                         raise MoeConfigInvalid, "Invalid variable reference"
99                 list.append(("i", k))
100             else:
101                 p = s.find("$")
102                 if p < 0:
103                     p = len(s)
104                 list.append(("s", s[:p]))
105                 s = s[p:]
106
107     def dump(self, file=sys.stdout, prefix=""):
108         for k,v in self.vars.items():
109             file.write(prefix)
110             file.write(k)
111             if len(v) > 0 and v[0][0] == "a":
112                 file.write("+")
113                 v = v[1:]
114             file.write("=")
115             for t,w in v:
116                 if t == "s":
117                     file.write("'" + w + "'")
118                 elif t == "i":
119                     file.write('"$' + w + '"')
120             file.write("\n")
121
122 class MoeConfigStack:
123     """Stack of configuration files."""
124
125     def __init__(self, base=None):
126         if base:
127             self.stk = base.stk[:]
128         else:
129             self.stk = []
130         self.in_progress = {}
131
132     def push(self, cfg):
133         self.stk.append(cfg)
134
135     def __getitem__(self, k):
136         if self.in_progress.has_key(k):
137             raise MoeConfigEvalError, "Definition of $%s is recursive" % k;
138         self.in_progress[k] = 1;
139         v = self.do_get(k, len(self.stk)-1)
140         del self.in_progress[k]
141         return v
142
143     def do_get(self, k, pos):
144         while pos >= 0:
145             cfg = self.stk[pos]
146             if cfg.vars.has_key(k):
147                 new = cfg.vars[k]
148                 if len(new) > 0 and new[0][0] == "a":
149                     v = self.do_get(k, pos-1)
150                     if v != "" and not v.endswith(" "):
151                         v += " "
152                 else:
153                     v = ""
154                 for op,arg in new:
155                     if op == "s":
156                         v = v + arg
157                     elif op == "i":
158                         v = v + self[arg]
159                 return v
160             pos -= 1
161         return ""
162
163     def keys(self):
164         seen = {}
165         for cfg in self.stk:
166             for k in cfg.vars.keys():
167                 seen[k] = None
168         return seen.keys()
169
170     def dump(self, file=sys.stdout, prefix=""):
171         for k in sorted(self.keys()):
172             v = self[k]
173             file.write("%s%s=%s\n" % (prefix,k,v))
174
175     def dump_defs(self, file=sys.stdout, prefix=""):
176         level = 0
177         for cfg in self.stk:
178             level += 1
179             file.write("%s(level %d: %s)\n" % (prefix,level,cfg.type))
180             cfg.dump(file, prefix + "\t")
181         file.write("%s(end)\n" % prefix)
182
183     def apply_overrides(self, prefix):
184         newstk = []
185         for cfg in self.stk:
186             over = MoeConfig(type = cfg.type + '-overrides')
187             changed = False
188             for k in cfg.vars.keys():
189                 if k.startswith(prefix):
190                     over.vars[k[len(prefix):]] = cfg.vars[k]
191                     changed = True
192             if changed:
193                 clean = MoeConfig(type = cfg.type)
194                 for k in cfg.vars.keys():
195                     if not k.startswith(prefix):
196                         clean.vars[k] = cfg.vars[k]
197                 newstk.append(clean)
198                 newstk.append(over)
199             else:
200                 newstk.append(cfg)
201         self.stk = newstk
202
203 def parse_overrides(argv):
204     cfg = None
205     argv0 = argv.pop(0)
206     while len(argv) > 0 and argv[0].find("=") >= 0:
207         if cfg is None:
208             cfg = MoeConfig(type='cmdline')
209         cfg.parse_line(argv.pop(0))
210     argv.insert(0, argv0)
211     return cfg