Update ConfigParser: Add option to load file if exists and do nothing otherwise.
authorRadek Brich <radek.brich@devl.cz>
Wed, 07 May 2014 18:37:13 +0200
changeset 12 d5f749032fa1
parent 11 d1f08fc9de31
child 13 b19248829509
Update ConfigParser: Add option to load file if exists and do nothing otherwise.
pycolib/configparser.py
--- a/pycolib/configparser.py	Mon Apr 14 22:29:07 2014 +0200
+++ b/pycolib/configparser.py	Wed May 07 18:37:13 2014 +0200
@@ -1,4 +1,4 @@
-# simple config file parser utilizing Python's exec() builtin
+import os.path
 
 
 class ConfigParserError(Exception):
@@ -6,6 +6,11 @@
 
 
 class ConfigParser:
+
+    """
+    Simple config file parser utilizing Python's exec() builtin.
+    """
+
     def __init__(self):
         self.options = {}
         self.values = {}
@@ -15,8 +20,10 @@
         self.options[name] = {'type':type, 'default':default}
         self.values[name] = default
 
-    def load(self, fname):
+    def load(self, fname, must_exist=True):
         """Read config file and check loaded values."""
+        if not must_exist and not os.path.isfile(fname):
+            return
         self.read(fname)
         self.check()