Rev 5753: Merge config-concrete-stores into config-stack in file:///home/vila/src/bzr/experimental/config/

Vincent Ladeuil v.ladeuil+lp at free.fr
Tue Apr 5 17:39:53 UTC 2011


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

------------------------------------------------------------
revno: 5753 [merge]
revision-id: v.ladeuil+lp at free.fr-20110405173953-n5rsmhxkytx0nk1d
parent: v.ladeuil+lp at free.fr-20110405164634-tq2gnd52yl7gclh5
parent: v.ladeuil+lp at free.fr-20110405173951-k8dsvb43pyearpw3
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: config-stack
timestamp: Tue 2011-04-05 19:39:53 +0200
message:
  Merge config-concrete-stores into config-stack
modified:
  bzrlib/config.py               config.py-20051011043216-070c74f4e9e338e8
  bzrlib/tests/test_config.py    testconfig.py-20051011041908-742d0c15d8d8c8eb
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2011-04-05 16:46:34 +0000
+++ b/bzrlib/config.py	2011-04-05 17:39:53 +0000
@@ -2049,8 +2049,12 @@
         """
         raise NotImplementedError(self.get_sections)
 
-    def set_option(self, name, value, section_name=None):
-        raise NotImplementedError(self.set_option)
+    def get_mutable_section(self, section_name=None):
+        """Returns the specified mutable section.
+
+        :param section_name: The section identifier
+        """
+        raise NotImplementedError(self.get_mutable_section)
 
 
 class ConfigObjStore(Store):
@@ -2087,16 +2091,27 @@
         # We just keep the content waiting for load() to be called when needed
         self._content = StringIO(str_or_unicode.encode('utf-8'))
 
-    def load(self):
-        """Load the store from the associated file."""
+    def load(self, allow_no_such_file=False):
+        """Load the store from the associated file.
+
+        :param allow_no_such_file: Swallow the NoSuchFile exception if True.
+            This allows delayed loading when creating the first option ever.
+        """
         if self.loaded:
             return
         if self._content is not None:
             co_input = self._content
         else:
+            try:
+                content = self.transport.get_bytes(self.file_name)
+            except errors.NoSuchFile:
+                if allow_no_such_file:
+                    content = ''
+                else:
+                    raise
+            co_input =  StringIO(content)
+        try:
             # The config files are always stored utf8-encoded
-            co_input =  StringIO(self.transport.get_bytes(self.file_name))
-        try:
             self._config_obj = ConfigObj(co_input, encoding='utf-8')
         except configobj.ConfigObjError, e:
             # FIXME: external_url should really accepts an optional relpath
@@ -2129,14 +2144,14 @@
         for section_name in cobj.sections:
             yield ReadOnlySection(section_name, cobj[section_name])
 
-    def set_option(self, name, value, section_name=None):
+    def get_mutable_section(self, section_name=None):
         # We need a loaded store
-        self.load()
+        self.load(allow_no_such_file=True)
         if section_name is None:
             section = self._config_obj
         else:
             section = self._config_obj.setdefault(section_name, {})
-        section[name] = value
+        return MutableSection(section_name, section)
 
 
 # FIXME: global, bazaar, shouldn't that be 'user' instead or even

=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py	2011-04-05 16:46:34 +0000
+++ b/bzrlib/tests/test_config.py	2011-04-05 17:39:53 +0000
@@ -1918,6 +1918,10 @@
         # We use from_string and don't save, so the file shouldn't be created
         self.failIfExists('foo.conf')
 
+    def test_loading_unknown_file_fails(self):
+        store = config.ConfigObjStore(self.get_transport(), 'I-do-not-exist')
+        self.assertRaises(errors.NoSuchFile, store.load)
+
     def test_invalid_content(self):
         store = self.get_store('foo.conf', 'this is invalid !')
         self.assertEquals(False, store.loaded)
@@ -1998,15 +2002,24 @@
             ('baz', {'foo_in_baz': 'barbaz', 'qux': {'foo_in_qux': 'quux'}}),
             sections[3])
 
+    def test_set_option_in_empty_file(self):
+        store = self.get_store('foo.conf')
+        section = store.get_mutable_section(None)
+        section.set('foo', 'bar')
+        store.save()
+        self.assertFileEqual('foo = bar\n', 'foo.conf')
+
     def test_set_option_in_default_section(self):
         store = self.get_store('foo.conf', '')
-        store.set_option('foo', 'bar')
+        section = store.get_mutable_section(None)
+        section.set('foo', 'bar')
         store.save()
         self.assertFileEqual('foo = bar\n', 'foo.conf')
 
     def test_set_option_in_named_section(self):
         store = self.get_store('foo.conf', '')
-        store.set_option('foo', 'bar', 'baz')
+        section = store.get_mutable_section('baz')
+        section.set('foo', 'bar')
         store.save()
         self.assertFileEqual('[baz]\nfoo = bar\n', 'foo.conf')
 



More information about the bazaar-commits mailing list