import re
import sys
+import moe
key_pattern = re.compile("^[A-Za-z0-9_-]+$")
ref_pattern = re.compile("^[A-Za-z0-9_-]+")
-class MoeConfigInvalid(Exception):
+class MoeConfigInvalid(moe.MoeErr):
pass
-class MoeConfigEvalErr(Exception):
+class MoeConfigEvalErr(moe.MoeErr):
pass
class MoeConfig:
"""Moe configuration file. Should be immutable once a part of a stack."""
- def __init__(self, file=None, name=None):
+ def __init__(self, file=None, name=None, type="<unnamed>"):
self.vars = {}
+ self.type = type
if file is not None:
self.load(file)
elif name is not None:
else:
self.load(file)
+ def set(self, k, v):
+ self.vars[k] = [("s", v)]
+
def parse_line(self, x):
x = x.rstrip("\n").lstrip(" \t")
if x=="" or x.startswith("#"):
list.append(("s", s[:p]))
s = s[p:]
- def dump(self, file=sys.stdout):
+ def dump(self, file=sys.stdout, prefix=""):
for k,v in self.vars.items():
+ file.write(prefix)
file.write(k)
if len(v) > 0 and v[0][0] == "a":
file.write("+")
self.in_progress[k] = 1;
v = self.do_get(k, len(self.stk)-1)
del self.in_progress[k]
- self.cache[k] = v
+ ## FIXME: This is disabled, because the immutability invariant is broken!
+ # self.cache[k] = v
return v
def do_get(self, k, pos):
cfg = self.stk[pos]
if cfg.vars.has_key(k):
new = cfg.vars[k]
- if new[0][0] == "a":
+ if len(new) > 0 and new[0][0] == "a":
v = self.do_get(k, pos-1)
else:
v = ""
seen[k] = None
return seen.keys()
- def dump(self, file=sys.stdout):
- for k in self.keys():
+ def dump(self, file=sys.stdout, prefix=""):
+ for k in sorted(self.keys()):
v = self[k]
- file.write("%s=%s\n" % (k,v))
+ file.write("%s%s=%s\n" % (prefix,k,v))
- def dump_defs(self, file=sys.stdout):
- file.write("Configuration stack:\n")
+ def dump_defs(self, file=sys.stdout, prefix=""):
level = 0
for cfg in self.stk:
level += 1
- file.write("(level %d)\n" % level)
- cfg.dump(file)
- file.write("(end)\n")
+ file.write("%s(level %d: %s)\n" % (prefix,level,cfg.type))
+ cfg.dump(file, prefix + "\t")
+ file.write("%s(end)\n" % prefix)
def apply_overrides(self, prefix):
newstk = []
for cfg in self.stk:
- over = MoeConfig()
+ over = MoeConfig(type = cfg.type + '-overrides')
changed = False
for k in cfg.vars.keys():
if k.startswith(prefix):
over.vars[k[len(prefix):]] = cfg.vars[k]
changed = True
if changed:
- clean = MoeConfig()
+ clean = MoeConfig(type = cfg.type)
for k in cfg.vars.keys():
if not k.startswith(prefix):
clean.vars[k] = cfg.vars[k]
newstk.append(cfg)
self.stk = newstk
self.reset_cache()
+
+def parse_overrides(argv):
+ cfg = None
+ argv0 = argv.pop(0)
+ while len(argv) > 0 and argv[0].find("=") >= 0:
+ if cfg is None:
+ cfg = MoeConfig(type='cmdline')
+ cfg.parse_line(argv.pop(0))
+ argv.insert(0, argv0)
+ return cfg