]> mj.ucw.cz Git - moe.git/blob - t/moe/status.py
Pipeline functions now get keyword arguments
[moe.git] / t / moe / status.py
1 #!/usr/bin/env python
2
3 import sys
4 import types
5 import re
6
7 key_pattern = re.compile("^[A-Za-z0-9_-]+$")
8
9 class MoeStatusInvalid(Exception):
10     pass
11
12 class MoeStatus:
13     """Moe status file."""
14
15     def __init__(self):
16         self.stat = {}
17
18     def __getitem__(self, k):
19         if not self.stat.has_key(k): return None
20         v = self.stat[k]
21         if type(v) == types.ListType:
22             if len(v) > 0: return v[0]
23             else: return None
24         else: return v
25
26     def __setitem__(self, k, v):
27         self.stat[k] = v
28
29     def keys(self):
30         return self.stat.keys()
31
32     def get_list(self, k):
33         m = self.stat
34         if not m.has_key(k):
35             m[k] = []
36         elif type(m[k]) != types.ListType:
37             m[k] = [m[k]]
38         return m[k]
39
40     def str_lines(self, indent=""):
41         "Return a list of lines, `indent` is indent prefix."
42         l = []
43         for k,v in self.stat.items():
44             if type(v) == types.ListType: vals = v
45             else: vals = [v]
46             for w in vals:
47                 if isinstance(w, MoeStatus):
48                     l.append("\t" * indent + k + "(\n")
49                     l.extend(self.str_lines(indent+"  "))
50                     l.append("\t" * indent + ")\n")
51                 else:
52                     l.append("\t" * indent + k + ":" + str(w) + "\n")
53         return l
54         
55     def write(self, file=None, name=None):
56         if file is None:
57             if name is not None:
58                 file = open(name, "w")
59             else:
60                 file = sys.stdout
61         self.write_nested(file, 0)
62
63     def write_nested(self, file, indent):
64         for k,v in self.stat.items():
65             if type(v) == types.ListType: vals = v
66             else: vals = [v]
67             for w in vals:
68                 if isinstance(w, MoeStatus):
69                     file.write("\t" * indent + k + "(\n")
70                     w.write_nested(file, indent+1);
71                     file.write("\t" * indent + ")\n")
72                 else:
73                     file.write("\t" * indent + k + ":" + str(w) + "\n")
74
75     def read(self, file=None, name=None):
76         if file is None:
77             if name is not None:
78                 file = open(name, "r")
79             else:
80                 file = sys.stdin
81         self.stat = {}
82         self.do_read(file)
83
84     def read_val(self, k, v):
85         if not key_pattern.match(k):
86             raise MoeStatusInvalid, "Parse error: invalid key syntax"
87         m = self.stat
88         if not m.has_key(k):
89             m[k] = v
90         else:
91             self.get_list(k).append(v)
92
93     def do_read(self, file):
94         stk = []
95         this = self
96         for x in file.readlines():
97             x = x.rstrip("\n").lstrip(" \t")
98             if x=="" or x.startswith("#"):
99                 pass
100             else:
101                 sep = x.find(":")
102                 if sep >= 0:
103                     k = x[:sep]
104                     v = x[sep+1:]
105                     this.read_val(k, v)
106                 elif x.endswith("("):
107                     k = x[:-1]
108                     new = MoeStatus()
109                     this.read_val(k, new)
110                     stk.append(this)
111                     this = new
112                 elif x == ")":
113                     if len(stk) == 0:
114                         raise MoeStatusInvalid, "Parse error: incorrect nesting"
115                     else:
116                         this = stk.pop()
117                 else:
118                     raise MoeStatusInvalid, "Parse error: malformed line"