]> mj.ucw.cz Git - moe.git/commitdiff
Writing status uses dump
authorTomas Gavenciak <gavento@ucw.cz>
Sat, 18 Sep 2010 21:38:03 +0000 (23:38 +0200)
committerTomas Gavenciak <gavento@ucw.cz>
Sat, 18 Sep 2010 21:38:03 +0000 (23:38 +0200)
Also use 'with' around IO

t/moe/status.py

index 320d7c9308d566d5082c0217462f05fd9a6fdbd8..5a5adf22c5b4851b572ff955ca08a0814eacb903 100644 (file)
@@ -38,7 +38,10 @@ class MoeStatus:
        return m[k]
 
     def dump(self, prefix=""):
-       "Return a list of lines, `prefix` is indent prefix."
+       """
+       Dump status in metafile format.
+       Return a list of lines, `prefix` is indent prefix.
+       """
        l = []
         for k,v in self.stat.items():
            if type(v) == types.ListType: vals = v
@@ -52,25 +55,19 @@ class MoeStatus:
                    l.append(prefix + k + ":" + str(w) + "\n")
        return l
        
-    def write(self, file=None, name=None):
-       if file is None:
-           if name is not None:
-               file = open(name, "w")
-           else:
-               file = sys.stdout
-       self.write_nested(file, 0)
-
-    def write_nested(self, file, indent):
-        for k,v in self.stat.items():
-           if type(v) == types.ListType: vals = v
-           else: vals = [v]
-           for w in vals:
-               if isinstance(w, MoeStatus):
-                   file.write("\t" * indent + k + "(\n")
-                   w.write_nested(file, indent+1);
-                   file.write("\t" * indent + ")\n")
-               else:
-                   file.write("\t" * indent + k + ":" + str(w) + "\n")
+    def write(self, f=None, name=None):
+       """
+       Write status (as a metafile) to `f` or file `name` or `stdout`
+       """
+       if not f and name is not None:
+           with open(name, "w") as f:
+               for l in self.dump():
+                   f.write(l+"\n")
+       else:
+           if not f: 
+               f = sys.stdout
+           for l in self.dump():
+               f.write(l+"\n")
 
     def read(self, file=None, name=None):
        if file is None: