← Back to team overview

configglue team mailing list archive

[Merge] lp:~ricardokirkner/configglue/avoid-re-inclusion into lp:configglue

 

Ricardo Kirkner has proposed merging lp:~ricardokirkner/configglue/avoid-re-inclusion into lp:configglue.

Commit message:
avoid re-including same files over again

Requested reviews:
  Configglue developers (configglue)

For more details, see:
https://code.launchpad.net/~ricardokirkner/configglue/avoid-re-inclusion/+merge/177426
-- 
https://code.launchpad.net/~ricardokirkner/configglue/avoid-re-inclusion/+merge/177426
Your team Configglue developers is requested to review the proposed merge of lp:~ricardokirkner/configglue/avoid-re-inclusion into lp:configglue.
=== modified file 'configglue/parser.py'
--- configglue/parser.py	2013-07-16 20:00:31 +0000
+++ configglue/parser.py	2013-07-29 16:44:32 +0000
@@ -257,7 +257,21 @@
                 logger.warn(
                     'File {0} could not be read. Skipping.'.format(path))
                 continue
-            self._read(fp, path, already_read=already_read)
+            # parse file
+            sub_parser = self.__class__(self.schema)
+            sub_parser._basedir = self._basedir
+            sub_parser._location = self._location
+            sub_parser._read(fp, path, already_read=already_read)
+            # update current parser with those values
+            for section, options in sub_parser._sections.items():
+                if section == '__main__':
+                    # skip copying includes to avoid including same files twice
+                    options.pop('includes', None)
+                if section in self._sections:
+                    self._sections[section].update(options)
+                else:
+                    self._sections[section] = options
+
             fp.close()
             read_ok.append(path)
             self._last_location = filename
@@ -290,6 +304,9 @@
             sub_parser.read(filenames)
             # update current parser with those values
             for section, options in sub_parser._sections.items():
+                if section == '__main__':
+                    # skip copying includes to avoid including same files twice
+                    options.pop('includes', None)
                 if section in self._sections:
                     self._sections[section].update(options)
                 else:

=== modified file 'configglue/tests/test_parser.py'
--- configglue/tests/test_parser.py	2013-07-16 20:20:21 +0000
+++ configglue/tests/test_parser.py	2013-07-29 16:44:32 +0000
@@ -194,26 +194,47 @@
 
             f = codecs.open("%s/first.cfg" % folder, 'w',
                             encoding=CONFIG_FILE_ENCODING)
-            f.write("[__main__]\nfoo=1\nbar=1")
+            f.write(textwrap.dedent(
+                """
+                [__noschema__]
+                baz = 1
+                [__main__]
+                includes = {folder}/second.cfg
+                foo = 1
+                bar = 1
+                """.format(folder=folder)))
             f.close()
 
             f = codecs.open("%s/second.cfg" % folder, 'w',
                             encoding=CONFIG_FILE_ENCODING)
-            f.write("[__main__]\nfoo=2\nbar=2")
+            f.write(textwrap.dedent(
+                """
+                [__noschema__]
+                baz = 2
+                [__main__]
+                foo = 2
+                bar = 2
+                """))
             f.close()
 
             f = codecs.open("%s/third.cfg" % folder, 'w',
                             encoding=CONFIG_FILE_ENCODING)
-            f.write("[__main__]\nfoo=3\nbar=3")
+            f.write(textwrap.dedent(
+                """
+                [__main__]
+                foo = 3
+                bar = 3
+                """))
             f.close()
 
-            config = textwrap.dedent("""
+            config = textwrap.dedent(
+                """
                 [__main__]
                 includes =
                     {folder}/first.cfg
-                    {folder}/second.cfg
                     {folder}/third.cfg
                 foo = 4
+                blam = %(baz)s
                 """.format(folder=folder))
             config = BytesIO(config.encode(CONFIG_FILE_ENCODING))
             return config, folder
@@ -221,9 +242,10 @@
         class MySchema(Schema):
             foo = IntOption()
             bar = IntOption()
+            blam = IntOption()
 
         config, folder = setup_config()
-        expected_values = {'__main__': {'foo': 4, 'bar': 3}}
+        expected_values = {'__main__': {'foo': 4, 'bar': 3, 'blam': 1}}
         parser = SchemaConfigParser(MySchema())
         # make sure we start on a clean basedir
         self.assertEqual(parser._basedir, '')


Follow ups