import re
import sys
-key_pattern = re.compile('^[A-Za-z0-9_-]+$')
-ref_pattern = re.compile('^[A-Za-z0-9_-]+')
+key_pattern = re.compile("^[A-Za-z0-9_-]+$")
+ref_pattern = re.compile("^[A-Za-z0-9_-]+")
class MoeConfigInvalid(Exception):
pass
self.load(file)
elif name is not None:
self.name = name
- self.load(open(name, 'r'))
+ self.load(open(name, "r"))
def parse_line(self, x):
x = x.rstrip("\n").lstrip(" \t")
- if x=='' or x.startswith('#'):
+ if x=="" or x.startswith("#"):
pass
else:
- sep = x.find('=')
+ sep = x.find("=")
if sep >= 0:
k = x[:sep]
v = x[sep+1:]
if k.endswith("+"):
k = k[:-1]
if not self.vars.has_key(k):
- self.vars[k] = [('a','')];
+ self.vars[k] = [("a","")];
else:
self.vars[k] = []
if not key_pattern.match(k):
v=v[1:]
if not v.endswith("'"):
raise MoeConfigInvalid, "Misquoted string"
- self.vars[k].append(('s', v[:-1]))
+ self.vars[k].append(("s", v[:-1]))
elif v.startswith('"'):
v=v[1:]
if not v.endswith('"'):
try:
self.parse_line(x)
except MoeConfigInvalid, x:
- msg = x.message + ' at line ' + str(lino)
- if hasattr(self, 'name'):
- msg += ' of ' + self.name
+ msg = x.message + " at line " + str(lino)
+ if hasattr(self, "name"):
+ msg += " of " + self.name
raise MoeConfigInvalid, msg
def parse_interpolated(self, list, s):
- while s<>'':
- if s.startswith('$'):
+ while s<>"":
+ if s.startswith("$"):
s = s[1:]
- if s.startswith('{'):
- p = s.find('}')
+ if s.startswith("{"):
+ p = s.find("}")
if not p:
raise MoeConfigInvalid, "Unbalanced braces"
k, s = s[1:p], s[p+1:]
k, s = s[:m.end()], s[m.end():]
else:
raise MoeConfigInvalid, "Invalid variable reference"
- list.append(('i', k))
+ list.append(("i", k))
else:
- p = s.find('$')
+ p = s.find("$")
if p < 0:
p = len(s)
- list.append(('s', s[:p]))
+ list.append(("s", s[:p]))
s = s[p:]
def dump(self, file=sys.stdout):
for k,v in self.vars.items():
file.write(k)
- if len(v) > 0 and v[0][0] == 'a':
- file.write('+')
+ if len(v) > 0 and v[0][0] == "a":
+ file.write("+")
v = v[1:]
- file.write('=')
+ file.write("=")
for t,w in v:
- if t == 's':
+ if t == "s":
file.write("'" + w + "'")
- elif t == 'i':
+ elif t == "i":
file.write('"$' + w + '"')
file.write("\n")
cfg = self.stk[pos]
if cfg.vars.has_key(k):
new = cfg.vars[k]
- if new[0][0] == 'a':
+ if new[0][0] == "a":
v = self.do_get(k, pos-1)
else:
- v = ''
+ v = ""
for op,arg in new:
- if op == 's':
+ if op == "s":
v = v + arg
- elif op == 'i':
+ elif op == "i":
v = v + self[arg]
return v
pos -= 1
- return ''
+ return ""
def keys(self):
seen = {}
import types
import re
-key_pattern = re.compile('^[A-Za-z0-9_-]+$')
+key_pattern = re.compile("^[A-Za-z0-9_-]+$")
class MoeMetaInvalid(Exception):
pass
def write(self, file=None, name=None):
if file is None:
if name is not None:
- file = open(name, 'w')
+ file = open(name, "w")
else:
file = sys.stdout
self.write_nested(file, 0)
w.write_nested(file, indent+1);
file.write("\t" * indent + ")\n")
else:
- file.write("\t" * indent + k + ':' + str(w) + "\n")
+ file.write("\t" * indent + k + ":" + str(w) + "\n")
def read(self, file=None, name=None):
if file is None:
if name is not None:
- file = open(name, 'r')
+ file = open(name, "r")
else:
file = sys.stdin
self.meta = {}
this = self
for x in file.readlines():
x = x.rstrip("\n").lstrip(" \t")
- if x=='' or x.startswith('#'):
+ if x=="" or x.startswith("#"):
pass
else:
- sep = x.find(':')
+ sep = x.find(":")
if sep >= 0:
k = x[:sep]
v = x[sep+1:]
this.read_val(k, v)
- elif x.endswith('('):
+ elif x.endswith("("):
k = x[:-1]
new = MoeMeta()
this.read_val(k, new)
stk.append(this)
this = new
- elif x == ')':
+ elif x == ")":
if len(stk) == 0:
raise MoeMetaInvalid, "Parse error: incorrect nesting"
else: