7 key_pattern = re.compile("^[A-Za-z0-9_-]+$")
9 class MoeStatusInvalid(Exception):
13 """Moe status file."""
18 def __getitem__(self, k):
19 if not self.stat.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):
30 return self.stat.keys()
32 def get_list(self, k):
36 elif type(m[k]) != types.ListType:
40 def write(self, file=None, name=None):
43 file = open(name, "w")
46 self.write_nested(file, 0)
48 def write_nested(self, file, indent):
49 for k,v in self.stat.items():
50 if type(v) == types.ListType: vals = v
53 if isinstance(w, MoeStatus):
54 file.write("\t" * indent + k + "(\n")
55 w.write_nested(file, indent+1);
56 file.write("\t" * indent + ")\n")
58 file.write("\t" * indent + k + ":" + str(w) + "\n")
60 def read(self, file=None, name=None):
63 file = open(name, "r")
69 def read_val(self, k, v):
70 if not key_pattern.match(k):
71 raise MoeStatusInvalid, "Parse error: invalid key syntax"
76 self.get_list(k).append(v)
78 def do_read(self, file):
81 for x in file.readlines():
82 x = x.rstrip("\n").lstrip(" \t")
83 if x=="" or x.startswith("#"):
99 raise MoeStatusInvalid, "Parse error: incorrect nesting"
103 raise MoeStatusInvalid, "Parse error: malformed line"