diff py/params.py @ 233:19569cb5ed46

better arg parser. seems close to ready
author Matt Johnston <matt@ucc.asn.au>
date Sat, 11 Apr 2015 23:32:59 +0800
parents e39ed85d87a5
children 0a1b642e3086
line wrap: on
line diff
--- a/py/params.py	Sat Apr 11 21:09:13 2015 +0800
+++ b/py/params.py	Sat Apr 11 23:32:59 2015 +0800
@@ -33,13 +33,7 @@
         self[k]
         self[k] = v
 
-    def load(self, f = None):
-        if not f:
-            try:
-                f = open(config.PARAMS_FILE, 'r')
-            except IOError as e:
-                W("Missing parameter file, using defaults. %s", e)
-                return
+    def _do_load(self, f):
         try:
             u = json.load(f)
         except Exception as e:
@@ -55,14 +49,29 @@
         L("Loaded parameters")
         L(self.save_string())
 
+    def load(self, f = None):
+        if f:
+            return self._do_load(f)
+        else:
+            with open(config.PARAMS_FILE, 'r') as f:
+                try:
+                    return self._do_load(f)
+                except IOError as e:
+                    W("Missing parameter file, using defaults. %s" % str(e))
+                    return
 
-    def save(self, f = None):
-        if not f:
-            f = file(config.PARAMS_FILE, 'w')
+    def _do_save(self, f):
         json.dump(self, f, sort_keys=True, indent=4)
         f.write('\n')
         f.flush()
 
+    def save(self, f = None):
+        if f:
+            return self._do_save(f)
+        else:
+            with file(config.PARAMS_FILE, 'w') as f:
+                return self._do_save(f)
+
     def save_string(self):
         s = io.StringIO()
         self.save(s)