7 key_pattern = re.compile('^[A-Za-z0-9_-]+$')
9 class MoeMetaInvalid(Exception):
18 def __getitem__(self, k):
19 if not self.meta.has_key(k): return None
21 if type(v) == types.ListType:
22 if len(v) > 0: return v[0]
26 def __setitem__(self, k, v):
29 def get_list(self, k):
33 elif type(m[k]) != types.ListType:
37 def write(self, file=None, name=None):
40 file = open(name, 'w')
43 self.write_nested(file, 0)
45 def write_nested(self, file, indent):
46 for k,v in self.meta.items():
47 if type(v) == types.ListType: vals = v
50 if isinstance(w, MoeMeta):
51 file.write("\t" * indent + k + "(\n")
52 w.write_nested(file, indent+1);
53 file.write("\t" * indent + ")\n")
55 file.write("\t" * indent + k + ':' + str(w) + "\n")
57 def read(self, file=None, name=None):
60 file = open(name, 'r')
66 def read_val(self, k, v):
67 if not key_pattern.match(k):
68 raise MoeMetaInvalid, "Parse error: invalid key syntax"
73 self.get_list(k).append(v)
75 def do_read(self, file):
78 for x in file.readlines():
79 x = x.rstrip("\n").lstrip(" \t")
80 if x=='' or x.startswith('#'):
96 raise MoeMetaInvalid, "Parse error: incorrect nesting"
100 raise MoeMetaInvalid, "Parse error: malformed line"