Rev 5753: Implement store.get_sections() as an iterator and provides the configobj implementation. in file:///home/vila/src/bzr/experimental/config/

Vincent Ladeuil v.ladeuil+lp at free.fr
Mon Apr 4 15:31:16 UTC 2011


At file:///home/vila/src/bzr/experimental/config/

------------------------------------------------------------
revno: 5753
revision-id: v.ladeuil+lp at free.fr-20110404153116-86oshhkw9nboow6z
parent: v.ladeuil+lp at free.fr-20110404131427-b22d3elqg8jmy9l6
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: config-store
timestamp: Mon 2011-04-04 17:31:16 +0200
message:
  Implement store.get_sections() as an iterator and provides the configobj implementation.
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2011-04-04 13:14:27 +0000
+++ b/bzrlib/config.py	2011-04-04 15:31:16 +0000
@@ -2038,6 +2038,16 @@
     def load(self):
         raise NotImplementedError(self.load)
 
+    def save(self):
+        raise NotImplementedError(self.load)
+
+    def get_sections(self):
+        """Returns an ordered iterable of existing sections.
+
+        :returns: An iterable of (name, dict).
+        """
+        raise NotImplementedError(self.get_sections)
+
 
 class ConfigObjStore(Store):
 
@@ -2100,6 +2110,19 @@
         self._config_obj.write(out)
         self.transport.put_bytes(self.file_name, out.getvalue())
 
+    def get_sections(self):
+        """Get the configobj section in the file order.
+
+        :returns: An iterable of (name, dict).
+        """
+        # We need a loaded store
+        self.load()
+        cobj = self._config_obj
+        if cobj.scalars:
+            yield None, dict([(k, cobj[k]) for k in cobj.scalars])
+        for section_name in cobj.sections:
+            yield section_name, dict(cobj[section_name])
+
 
 class cmd_config(commands.Command):
     __doc__ = """Display, set or remove a configuration option.

=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py	2011-04-04 13:14:27 +0000
+++ b/bzrlib/tests/test_config.py	2011-04-04 15:31:16 +0000
@@ -1893,7 +1893,7 @@
 
     # FIXME: parametrize against all valid (store, transport) combinations
 
-    def get_store(self, content=None, name=None):
+    def get_store(self, name=None, content=None):
         if name is None:
             name = 'foo.conf'
         if content is None:
@@ -1905,13 +1905,13 @@
 
     def test_delayed_load(self):
         self.build_tree_contents([('foo.conf', '')])
-        store = self.get_store(None, 'foo.conf')
+        store = self.get_store('foo.conf')
         self.assertEquals(False, store.loaded)
         store.load()
         self.assertEquals(True, store.loaded)
 
     def test_from_string_delayed_load(self):
-        store = self.get_store('')
+        store = self.get_store('foo.conf', '')
         self.assertEquals(False, store.loaded)
         store.load()
         self.assertEquals(True, store.loaded)
@@ -1919,7 +1919,7 @@
         self.failIfExists('foo.conf')
 
     def test_invalid_content(self):
-        store = self.get_store('this is invalid !', 'foo.conf')
+        store = self.get_store('foo.conf', 'this is invalid !')
         self.assertEquals(False, store.loaded)
         exc = self.assertRaises(errors.ParseConfigError, store.load)
         self.assertEndsWith(exc.filename, 'foo.conf')
@@ -1927,14 +1927,14 @@
         self.assertEquals(False, store.loaded)
 
     def test_save_empty_succeeds(self):
-        store = self.get_store('', 'foo.conf')
+        store = self.get_store('foo.conf', '')
         store.load()
         self.failIfExists('foo.conf')
         store.save()
         self.failUnlessExists('foo.conf')
 
     def test_save_with_content_succeeds(self):
-        store = self.get_store('foo=bar\n', 'foo.conf')
+        store = self.get_store('foo.conf', 'foo=bar\n')
         store.load()
         self.failIfExists('foo.conf')
         store.save()
@@ -1942,6 +1942,49 @@
         # FIXME: Far too ConfigObj specific
         self.assertFileEqual('foo = bar\n', 'foo.conf')
 
+    def test_get_no_sections_for_empty(self):
+        store = self.get_store('foo.conf', '')
+        store.load()
+        self.assertEquals([], list(store.get_sections()))
+
+    def test_get_default_section(self):
+        store = self.get_store('foo.conf', 'foo=bar')
+        sections = list(store.get_sections())
+        self.assertLength(1, sections)
+        self.assertEquals((None, {'foo': 'bar'}), sections[0])
+
+    def test_get_named_section(self):
+        store = self.get_store('foo.conf', '[baz]\nfoo=bar')
+        sections = list(store.get_sections())
+        self.assertLength(1, sections)
+        self.assertEquals(('baz', {'foo': 'bar'}), sections[0])
+
+    def test_get_embedded_sections(self):
+        store = self.get_store('foo.conf', '''
+foo=bar
+l=1,2
+[DEFAULT]
+foo_in_DEFAULT=foo_DEFAULT
+[bar]
+foo_in_bar=barbar
+[baz]
+foo_in_baz=barbaz
+[[qux]]
+foo_in_qux=quux
+''')
+        sections = list(store.get_sections())
+        self.assertLength(4, sections)
+        # The default section has no name.
+        # List values are provided as lists
+        self.assertEquals((None, {'foo': 'bar', 'l': ['1', '2']}), sections[0])
+        self.assertEquals(('DEFAULT', {'foo_in_DEFAULT': 'foo_DEFAULT'}),
+                          sections[1])
+        self.assertEquals(('bar', {'foo_in_bar': 'barbar'}), sections[2])
+        # sub sections are provided as embedded dicts.
+        self.assertEquals(('baz', {'foo_in_baz': 'barbaz',
+                                   'qux': {'foo_in_qux': 'quux'}}),
+                          sections[3])
+
 
 class TestConfigGetOptions(tests.TestCaseWithTransport, TestOptionsMixin):
 



More information about the bazaar-commits mailing list