Rev 3354: Split TransportConfig out of TreeConfig (abentley) in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Thu Apr 10 10:14:23 BST 2008


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 3354
revision-id:pqm at pqm.ubuntu.com-20080410091416-1so11izeyk0tsbk9
parent: pqm at pqm.ubuntu.com-20080410073653-vq10gbuoes5o2c0q
parent: aaron at aaronbentley.com-20080410044559-g84t1081dbrmy5xd
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Thu 2008-04-10 10:14:16 +0100
message:
  Split TransportConfig out of TreeConfig (abentley)
modified:
  bzrlib/config.py               config.py-20051011043216-070c74f4e9e338e8
  bzrlib/tests/test_config.py    testconfig.py-20051011041908-742d0c15d8d8c8eb
    ------------------------------------------------------------
    revno: 3242.1.5
    revision-id:aaron at aaronbentley.com-20080410044559-g84t1081dbrmy5xd
    parent: aaron at aaronbentley.com-20080404180004-nsnbu8w3ke4g1k4z
    committer: Aaron Bentley <aaron at aaronbentley.com>
    branch nick: bzrdir-config
    timestamp: Thu 2008-04-10 00:45:59 -0400
    message:
      Update per review comments
    modified:
      bzrlib/config.py               config.py-20051011043216-070c74f4e9e338e8
    ------------------------------------------------------------
    revno: 3242.1.4
    revision-id:aaron at aaronbentley.com-20080404180004-nsnbu8w3ke4g1k4z
    parent: aaron at aaronbentley.com-20080404172942-joxvpyazfps3j0gu
    committer: Aaron Bentley <aaron at aaronbentley.com>
    branch nick: bzrdir-config
    timestamp: Fri 2008-04-04 14:00:04 -0400
    message:
      Clean-up
    modified:
      bzrlib/config.py               config.py-20051011043216-070c74f4e9e338e8
    ------------------------------------------------------------
    revno: 3242.1.3
    revision-id:aaron at aaronbentley.com-20080404172942-joxvpyazfps3j0gu
    parent: aaron at aaronbentley.com-20080404171106-7qsjphtlecdq8drb
    committer: Aaron Bentley <aaron at aaronbentley.com>
    branch nick: bzrdir-config
    timestamp: Fri 2008-04-04 13:29:42 -0400
    message:
      Remove BzrDirConfig-specifc code
    modified:
      bzrlib/bzrdir.py               bzrdir.py-20060131065624-156dfea39c4387cb
      bzrlib/tests/bzrdir_implementations/test_bzrdir.py test_bzrdir.py-20060131065642-0ebeca5e30e30866
    ------------------------------------------------------------
    revno: 3242.1.2
    revision-id:aaron at aaronbentley.com-20080404171106-7qsjphtlecdq8drb
    parent: aaron at aaronbentley.com-20080403193754-b84gbqm5r6bnzmzr
    committer: Aaron Bentley <aaron at aaronbentley.com>
    branch nick: bzrdir-config
    timestamp: Fri 2008-04-04 13:11:06 -0400
    message:
      Turn BzrDirConfig into TransportConfig, reduce code duplication
    modified:
      bzrlib/bzrdir.py               bzrdir.py-20060131065624-156dfea39c4387cb
      bzrlib/config.py               config.py-20051011043216-070c74f4e9e338e8
      bzrlib/tests/bzrdir_implementations/test_bzrdir.py test_bzrdir.py-20060131065642-0ebeca5e30e30866
      bzrlib/tests/test_config.py    testconfig.py-20051011041908-742d0c15d8d8c8eb
    ------------------------------------------------------------
    revno: 3242.1.1
    revision-id:aaron at aaronbentley.com-20080403193754-b84gbqm5r6bnzmzr
    parent: pqm at pqm.ubuntu.com-20080228181740-9luays3sm3jouxbj
    committer: Aaron Bentley <aaron at aaronbentley.com>
    branch nick: bzrdir-config
    timestamp: Thu 2008-04-03 15:37:54 -0400
    message:
      Implement BzrDir configuration
    modified:
      bzrlib/bzrdir.py               bzrdir.py-20060131065624-156dfea39c4387cb
      bzrlib/config.py               config.py-20051011043216-070c74f4e9e338e8
      bzrlib/tests/bzrdir_implementations/test_bzrdir.py test_bzrdir.py-20060131065642-0ebeca5e30e30866
      bzrlib/tests/test_config.py    testconfig.py-20051011041908-742d0c15d8d8c8eb
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2008-03-26 06:21:48 +0000
+++ b/bzrlib/config.py	2008-04-10 09:14:16 +0000
@@ -892,31 +892,19 @@
     """Branch configuration data associated with its contents, not location"""
 
     def __init__(self, branch):
+        transport = branch.control_files._transport
+        self._config = TransportConfig(transport, 'branch.conf')
         self.branch = branch
 
     def _get_parser(self, file=None):
         if file is not None:
             return IniBasedConfig._get_parser(file)
-        return self._get_config()
-
-    def _get_config(self):
-        try:
-            obj = ConfigObj(self.branch.control_files.get('branch.conf'),
-                            encoding='utf-8')
-        except errors.NoSuchFile:
-            obj = ConfigObj(encoding='utf=8')
-        return obj
+        return self._config._get_configobj()
 
     def get_option(self, name, section=None, default=None):
         self.branch.lock_read()
         try:
-            obj = self._get_config()
-            try:
-                if section is not None:
-                    obj = obj[section]
-                result = obj[name]
-            except KeyError:
-                result = default
+            return self._config.get_option(name, section, default)
         finally:
             self.branch.unlock()
         return result
@@ -925,20 +913,7 @@
         """Set a per-branch configuration option"""
         self.branch.lock_write()
         try:
-            cfg_obj = self._get_config()
-            if section is None:
-                obj = cfg_obj
-            else:
-                try:
-                    obj = cfg_obj[section]
-                except KeyError:
-                    cfg_obj[section] = {}
-                    obj = cfg_obj[section]
-            obj[name] = value
-            out_file = StringIO()
-            cfg_obj.write(out_file)
-            out_file.seek(0)
-            self.branch.control_files.put('branch.conf', out_file)
+            self._config.set_option(value, name, section)
         finally:
             self.branch.unlock()
 
@@ -1126,3 +1101,61 @@
 
     def decode_password(self, credentials, encoding):
         return credentials
+
+
+class TransportConfig(object):
+    """A Config that reads/writes a config file on a Transport.
+
+    It is a low-level object that considers config data to be name/value pairs
+    that may be associated with a section.  Assigning meaning to the these
+    values is done at higher levels like TreeConfig.
+    """
+
+    def __init__(self, transport, filename):
+        self._transport = transport
+        self._filename = filename
+
+    def get_option(self, name, section=None, default=None):
+        """Return the value associated with a named option.
+
+        :param name: The name of the value
+        :param section: The section the option is in (if any)
+        :param default: The value to return if the value is not set
+        :return: The value or default value
+        """
+        configobj = self._get_configobj()
+        if section is None:
+            section_obj = configobj
+        else:
+            try:
+                section_obj = configobj[section]
+            except KeyError:
+                return default
+        return section_obj.get(name, default)
+
+    def set_option(self, value, name, section=None):
+        """Set the value associated with a named option.
+
+        :param value: The value to set
+        :param name: The name of the value to set
+        :param section: The section the option is in (if any)
+        """
+        configobj = self._get_configobj()
+        if section is None:
+            configobj[name] = value
+        else:
+            configobj.setdefault(section, {})[name] = value
+        self._set_configobj(configobj)
+
+    def _get_configobj(self):
+        try:
+            return ConfigObj(self._transport.get(self._filename),
+                             encoding='utf-8')
+        except errors.NoSuchFile:
+            return ConfigObj(encoding='utf-8')
+
+    def _set_configobj(self, configobj):
+        out_file = StringIO()
+        configobj.write(out_file)
+        out_file.seek(0)
+        self._transport.put_file(self._filename, out_file)

=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py	2008-02-11 23:27:34 +0000
+++ b/bzrlib/tests/test_config.py	2008-04-04 17:11:06 +0000
@@ -33,6 +33,7 @@
     urlutils,
     tests,
     trace,
+    transport,
     )
 from bzrlib.util.configobj import configobj
 
@@ -160,6 +161,7 @@
     def __init__(self, user_id=None):
         self.email = user_id
         self.files = {}
+        self._transport = self
 
     def get_utf8(self, filename):
         if filename != 'email':
@@ -177,6 +179,9 @@
     def put(self, filename, fileobj):
         self.files[filename] = fileobj.read()
 
+    def put_file(self, filename, fileobj):
+        return self.put(filename, fileobj)
+
 
 class InstrumentedConfig(config.Config):
     """An instrumented config that supplies stubs for template methods."""
@@ -1136,6 +1141,35 @@
         self.assertEqual(value, 'value3-section')
 
 
+class TestTransportConfig(tests.TestCaseWithTransport):
+
+    def test_get_value(self):
+        """Test that retreiving a value from a section is possible"""
+        bzrdir_config = config.TransportConfig(transport.get_transport('.'),
+                                               'control.conf')
+        bzrdir_config.set_option('value', 'key', 'SECTION')
+        bzrdir_config.set_option('value2', 'key2')
+        bzrdir_config.set_option('value3-top', 'key3')
+        bzrdir_config.set_option('value3-section', 'key3', 'SECTION')
+        value = bzrdir_config.get_option('key', 'SECTION')
+        self.assertEqual(value, 'value')
+        value = bzrdir_config.get_option('key2')
+        self.assertEqual(value, 'value2')
+        self.assertEqual(bzrdir_config.get_option('non-existant'), None)
+        value = bzrdir_config.get_option('non-existant', 'SECTION')
+        self.assertEqual(value, None)
+        value = bzrdir_config.get_option('non-existant', default='default')
+        self.assertEqual(value, 'default')
+        self.assertEqual(bzrdir_config.get_option('key2', 'NOSECTION'), None)
+        value = bzrdir_config.get_option('key2', 'NOSECTION',
+                                         default='default')
+        self.assertEqual(value, 'default')
+        value = bzrdir_config.get_option('key3')
+        self.assertEqual(value, 'value3-top')
+        value = bzrdir_config.get_option('key3', 'SECTION')
+        self.assertEqual(value, 'value3-section')
+
+
 class TestAuthenticationConfigFile(tests.TestCase):
     """Test the authentication.conf file matching"""
 




More information about the bazaar-commits mailing list