Rev 5758: Implement get_mutable_section. in file:///home/vila/src/bzr/experimental/config/
Vincent Ladeuil
v.ladeuil+lp at free.fr
Tue Apr 5 17:39:35 UTC 2011
At file:///home/vila/src/bzr/experimental/config/
------------------------------------------------------------
revno: 5758
revision-id: v.ladeuil+lp at free.fr-20110405173934-bdp7ct4kvvytw420
parent: v.ladeuil+lp at free.fr-20110405172734-imy4dkxu6yix8k7f
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: config-abstract-store
timestamp: Tue 2011-04-05 19:39:34 +0200
message:
Implement get_mutable_section.
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py 2011-04-05 17:27:34 +0000
+++ b/bzrlib/config.py 2011-04-05 17:39:34 +0000
@@ -2090,16 +2090,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
@@ -2134,7 +2145,7 @@
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:
=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py 2011-04-05 17:27:34 +0000
+++ b/bzrlib/tests/test_config.py 2011-04-05 17:39:34 +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,6 +2002,13 @@
('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', '')
section = store.get_mutable_section(None)
More information about the bazaar-commits
mailing list