Refactor.
authorRadek Brich <radek.brich@devl.cz>
Sat, 13 Apr 2013 14:11:26 +0200
changeset 4 fdaa7dc9035e
parent 3 cc27136cdead
child 5 055f7dfb3e4f
Refactor.
README
pycolib/configparser.py
tests/test_configparser.py
--- a/README	Sat Apr 06 00:07:17 2013 +0200
+++ b/README	Sat Apr 13 14:11:26 2013 +0200
@@ -5,6 +5,7 @@
 
  * ansicolor - change color of text in terminal using ANSI escape sequences
  * coloredformatter - logging.Formatter which adds ANSI colors to output
+ * configparser - simple config file parser utilizing Python's exec() builtin
  * makeurl - adjust string for using in URL by replacing symbols and combined unicode chars
  * prettysize - print file sizes in human readable form
  * soap - minimal SOAP client
--- a/pycolib/configparser.py	Sat Apr 06 00:07:17 2013 +0200
+++ b/pycolib/configparser.py	Sat Apr 13 14:11:26 2013 +0200
@@ -1,4 +1,4 @@
-# simple config file reader using Python's syntax and parser
+# simple config file parser utilizing Python's exec() builtin
 
 
 class ConfigParserError(Exception):
@@ -7,46 +7,45 @@
 
 class ConfigParser:
     def __init__(self):
-        self.args = {}       # config file arguments
-        self.registered_args = {}
+        self.options = {}
+        self.values = {}
 
-    def add_argument(self, name, type_=str, default=None):
-        """Register argument name, type and default value."""
-        self.registered_args[name] = {'type':type_, 'default':default}
-        self.args[name] = default
+    def add_option(self, name, type_=str, default=None):
+        """Register option name, type and default value."""
+        self.options[name] = {'type':type_, 'default':default}
+        self.values[name] = default
 
     def load(self, fname):
-        """Load config file and check loaded values."""
-        self.parse(fname)
+        """Read config file and check loaded values."""
+        self.read(fname)
         self.check()
 
-    def parse(self, fname):
-        """Parse config file. Does not check values."""
+    def read(self, fname):
+        """Read config file. Does not check values."""
         with open(fname) as f:
-            exec(f.read(), self.args)
+            exec(f.read(), self.values)
 
     def check(self):
-        """Check loaded argument values against registered types."""
-        for key in self.args.keys():
+        """Check option values against registered types."""
+        for key in self.values.keys():
             if key == '__builtins__':
                 continue
-            if key in self.registered_args:
-                # arg registered, check type
-                type_ = self.registered_args[key]['type']
-                if not isinstance(self.args[key], type_) and not self.args[key] is None:
+            if key in self.options:
+                type_ = self.options[key]['type']
+                if not isinstance(self.values[key], type_) and not self.values[key] is None:
                     raise ConfigParserError("Bad value of config parameter '%s': type is %s but should be %s"
-                        % (key, type(self.args[key]), type_))
+                        % (key, type(self.values[key]), type_))
             else:
                 raise ConfigParserError("Unknown config parameter '%s'." % key)
 
     def __getattr__(self, name):
         """Attribute access for config parameters.
 
-        cfg.my_param <===> cfg.args['myparam']
+        cfg.my_param <===> cfg.values['myparam']
 
         """
-        if name in self.args:
-            return self.args[name]
+        if name in self.values:
+            return self.values[name]
         else:
             raise AttributeError()
 
--- a/tests/test_configparser.py	Sat Apr 06 00:07:17 2013 +0200
+++ b/tests/test_configparser.py	Sat Apr 13 14:11:26 2013 +0200
@@ -10,17 +10,17 @@
 
     def setUp(self):
         self.config = ConfigParser()
-        self.config.add_argument('arg_str')
-        self.config.add_argument('arg_int', int, 1)
+        self.config.add_option('arg_str')
+        self.config.add_option('arg_int', int, 1)
         script_path = os.path.dirname(os.path.realpath(__file__))
         self.data_path = os.path.join(script_path, 'data')
 
-    def test_add_argument(self):
+    def test_add_option(self):
         name = 'argname'
-        self.config.add_argument(name, int, 1)
-        self.assertEqual(self.config.registered_args[name]['type'], int)
-        self.assertEqual(self.config.registered_args[name]['default'], 1)
-        self.assertEqual(self.config.args[name], 1)
+        self.config.add_option(name, int, 1)
+        self.assertEqual(self.config.options[name]['type'], int)
+        self.assertEqual(self.config.options[name]['default'], 1)
+        self.assertEqual(self.config.values[name], 1)
 
     def test_load_badname(self):
         self.assertRaises(IOError, self.config.load, 'bad/file_name')