Kelsey Hightower avatar Kelsey Hightower committed 8a519ec

Fixing Pylint and pep8 errors. Adding install-requires to setup.py; insure pip installs PyYAML

Comments (0)

Files changed (5)

 include README
 include yamlconfig.py
-recursive-include test *
+recursive-include tests *
 
 setup(
     name='yamlconfig',
-    version='0.3',
+    version='0.3.1',
     description='YAML configuration file reader with basic variable interpolation.',
     author='Kelsey Hightower',
     author_email='kelsey.hightower@gmail.com',
         'Programming Language :: Python :: 3.2',
         'Topic :: Software Development :: Libraries :: Python Modules',
         ],
-    requires=['pyyaml (>=3.0)']
+    install_requires=['PyYAML >= 3.0']
     )

test/test_yamlconfig.py

-import os
-import unittest2
-import yamlconfig
-
-dummy_yaml = """
-main:
-    name: 'Napoleon'
-    age: 15
-    skills: ['Ninja', 'Dancing', 'Nunchucks', $default_skills]
-    net_worth: $10
-"""
-
-def create_dummy_config():
-    with open('dummy_config.yaml', 'w') as config:
-        config.write(dummy_yaml)
-
-class TestYamlConfig(unittest2.TestCase):
-
-    def setUp(self):
-        create_dummy_config()
-        self.infile = 'dummy_config.yaml'
-        self.defaults = {'default_skills': 'Kickball'} 
-        self.config = yamlconfig.YamlConfig(self.infile, self.defaults)
-
-    def tearDown(self):
-        os.remove('dummy_config.yaml')
-
-    def test_get_config(self):
-        expected_config = {
-            'main': {
-                'name': 'Napoleon',
-                'age': 15,
-                'skills': ['Ninja', 'Dancing', 'Nunchucks', 'Kickball'],
-                'net_worth': '$10'
-                }
-        }
-        self.assertDictEqual(expected_config, self.config)
-
-    def test_list_type(self):
-        self.assertIsInstance(self.config['main']['skills'], list)
-
-    def test_int_type(self):
-        self.assertIsInstance(self.config['main']['age'], int)
-
-    def test_str_type(self):
-        self.assertIsInstance(self.config['main']['name'], str)
-
-    def test_keyerror(self):
-        def getoption(option):
-            return self.config['main'][option]
-        self.assertRaises(KeyError, getoption, 'girlfriend')
-
-    def test_safe_substitute(self):
-        self.assertEqual(self.config['main']['net_worth'], '$10')
-
-    def test_with_no_defaults(self):
-        create_dummy_config()
-        infile = 'dummy_config.yaml'
-        config = yamlconfig.YamlConfig(infile)
-        expected_config = {
-            'main': {
-                'name': 'Napoleon',
-                'age': 15,
-                'skills': ['Ninja', 'Dancing', 'Nunchucks', '$default_skills'],
-                'net_worth': '$10'
-                }
-        }
-        self.assertDictEqual(expected_config, config)
-
-if __name__ == '__main__':
-    unittest2.main()

tests/test_yamlconfig.py

+import os
+import unittest2
+import yamlconfig
+
+dummy_yaml = """
+main:
+    name: 'Napoleon'
+    age: 15
+    skills: ['Ninja', 'Dancing', 'Nunchucks', $default_skills]
+    net_worth: $10
+"""
+
+def create_dummy_config():
+    with open('dummy_config.yaml', 'w') as config:
+        config.write(dummy_yaml)
+
+class TestYamlConfig(unittest2.TestCase):
+
+    def setUp(self):
+        create_dummy_config()
+        self.infile = 'dummy_config.yaml'
+        self.defaults = {'default_skills': 'Kickball'} 
+        self.config = yamlconfig.YamlConfig(self.infile, self.defaults)
+
+    def tearDown(self):
+        os.remove('dummy_config.yaml')
+
+    def test_get_config(self):
+        expected_config = {
+            'main': {
+                'name': 'Napoleon',
+                'age': 15,
+                'skills': ['Ninja', 'Dancing', 'Nunchucks', 'Kickball'],
+                'net_worth': '$10'
+                }
+        }
+        self.assertDictEqual(expected_config, self.config)
+
+    def test_list_type(self):
+        self.assertIsInstance(self.config['main']['skills'], list)
+
+    def test_int_type(self):
+        self.assertIsInstance(self.config['main']['age'], int)
+
+    def test_str_type(self):
+        self.assertIsInstance(self.config['main']['name'], str)
+
+    def test_keyerror(self):
+        def getoption(option):
+            return self.config['main'][option]
+        self.assertRaises(KeyError, getoption, 'girlfriend')
+
+    def test_safe_substitute(self):
+        self.assertEqual(self.config['main']['net_worth'], '$10')
+
+    def test_with_no_defaults(self):
+        create_dummy_config()
+        infile = 'dummy_config.yaml'
+        config = yamlconfig.YamlConfig(infile)
+        expected_config = {
+            'main': {
+                'name': 'Napoleon',
+                'age': 15,
+                'skills': ['Ninja', 'Dancing', 'Nunchucks', '$default_skills'],
+                'net_worth': '$10'
+                }
+        }
+        self.assertDictEqual(expected_config, config)
+
+if __name__ == '__main__':
+    unittest2.main()
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 02110-1301  USA
 """
-import string
 import yaml
 
+from string import Template
+
+
 class YamlConfig(dict):
     """
     YAML configuration file reader with basic variable interpolation.
         for entry in indict:
             self[entry] = indict[entry]
 
-    def _interpolate(self, infile, defaults):
+    def _interpolate(self):
         """
         Interpolate variables.
         """
-        with open(infile, 'r') as config:
-            template = string.Template(config.read())
-            interpolated_config = template.safe_substitute(defaults)
+        with open(self.infile, 'r') as config:
+            template = Template(config.read())
+            interpolated_config = template.safe_substitute(self.defaults)
         return interpolated_config
 
-    def _deserialize(self, infile):
+    def _deserialize(self):
         """
         Deserialize YAML string.
         """
-        config = yaml.load(infile)
+        config = yaml.load(self.infile)
         return config
 
     def get_config(self):
-        self.infile = self._interpolate(self.infile, self.defaults)
-        config = self._deserialize(self.infile)
+        """
+        Parse and return the configuration object.
+        """
+        self.infile = self._interpolate()
+        config = self._deserialize()
         return config
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.