Rev 5358: Introduce a 'from_bytes' constructor for config objects. in file:///home/vila/src/bzr/bugs/525571-lock-bazaar-conf-files/

Vincent Ladeuil v.ladeuil+lp at free.fr
Tue Aug 24 09:05:12 BST 2010


At file:///home/vila/src/bzr/bugs/525571-lock-bazaar-conf-files/

------------------------------------------------------------
revno: 5358
revision-id: v.ladeuil+lp at free.fr-20100824080512-o579ni033c75lkue
parent: v.ladeuil+lp at free.fr-20100823093200-9oi6hit1hnccjyvo
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: simplify-test-config-building
timestamp: Tue 2010-08-24 10:05:12 +0200
message:
  Introduce a 'from_bytes' constructor for config objects.
-------------- next part --------------
=== modified file 'NEWS'
--- a/NEWS	2010-08-23 09:31:24 +0000
+++ b/NEWS	2010-08-24 08:05:12 +0000
@@ -133,9 +133,9 @@
 API Changes
 ***********
 
-* Configuration files should now use the ``_content`` parameter in the
-  constructor rather than the ``file`` parameter of the ``_get_parser``
-  method. The later has been deprecated. (Vincent Ladeuil)
+* Configuration files should now use the ``from_bytes`` constructor the
+  rather than the ``file`` parameter of the ``_get_parser`` method. The
+  later has been deprecated. (Vincent Ladeuil)
 
 * InventoryEntry instances now raise AttributeError if you try to assign
   to attributes that are irrelevant to that kind of entry.  e.g. setting

=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2010-08-23 09:32:00 +0000
+++ b/bzrlib/config.py	2010-08-24 08:05:12 +0000
@@ -353,13 +353,10 @@
     """A configuration policy that draws from ini files."""
 
     def __init__(self, get_filename=symbol_versioning.DEPRECATED_PARAMETER,
-                 file_name=None, _content=None):
+                 file_name=None):
         """Base class for configuration files using an ini-like syntax.
 
         :param file_name: The configuration file path.
-
-        :param _content: For tests only, a string representing the file
-            content. This will be utf-8 encoded.
         """
         super(IniBasedConfig, self).__init__()
         if symbol_versioning.deprecated_passed(get_filename):
@@ -376,12 +373,20 @@
                 self.file_name = get_filename()
         else:
             self.file_name = file_name
-        if _content is not None:
-            # wrap the content as a file-like object
-            _content = StringIO(_content.encode('utf-8'))
-        self._content = _content
+        self._content = None
         self._parser = None
 
+    @classmethod
+    def from_bytes(cls, unicode_bytes):
+        """Create a config object from bytes.
+
+        :param unicode_bytes: A string representing the file content. This will
+            be utf-8 encoded.
+        """
+        conf = cls()
+        conf._content = StringIO(unicode_bytes.encode('utf-8'))
+        return conf
+
     def _get_parser(self, file=symbol_versioning.DEPRECATED_PARAMETER):
         if self._parser is not None:
             return self._parser
@@ -524,9 +529,8 @@
 class GlobalConfig(IniBasedConfig):
     """The configuration that should be used for a specific location."""
 
-    def __init__(self, _content=None):
-        super(GlobalConfig, self).__init__(file_name=config_filename(),
-                                           _content=_content)
+    def __init__(self):
+        super(GlobalConfig, self).__init__(file_name=config_filename())
 
     def get_editor(self):
         return self._get_user_option('editor')
@@ -566,10 +570,9 @@
 class LocationConfig(IniBasedConfig):
     """A configuration object that gives the policy for a location."""
 
-    def __init__(self, location, _content=None):
+    def __init__(self, location):
         super(LocationConfig, self).__init__(
-            file_name=locations_config_filename(),
-            _content=_content)
+            file_name=locations_config_filename())
         # local file locations are looked up by local path, rather than
         # by file url. This is because the config file is a user
         # file, and we would rather not expose the user to file urls.
@@ -577,6 +580,19 @@
             location = urlutils.local_path_from_url(location)
         self.location = location
 
+    @classmethod
+    def from_bytes(cls, unicode_bytes, location):
+        """Create a config object from bytes.
+
+        :param unicode_bytes: A string representing the file content. This will
+            be utf-8 encoded.
+
+        :param location: The location url to filter the configuration.
+        """
+        conf = cls(location)
+        conf._content = StringIO(unicode_bytes.encode('utf-8'))
+        return conf
+
     def _get_matching_sections(self):
         """Return an ordered list of section names matching this location."""
         sections = self._get_parser()

=== modified file 'bzrlib/plugins/launchpad/test_account.py'
--- a/bzrlib/plugins/launchpad/test_account.py	2010-07-19 12:54:54 +0000
+++ b/bzrlib/plugins/launchpad/test_account.py	2010-08-24 08:05:12 +0000
@@ -26,7 +26,7 @@
 class LaunchpadAccountTests(TestCaseInTempDir):
 
     def setup_config(self, text):
-        my_config = config.GlobalConfig(_content=text)
+        my_config = config.GlobalConfig.from_bytes(text)
         return my_config
 
     def test_get_lp_login_unconfigured(self):

=== modified file 'bzrlib/tests/test_commands.py'
--- a/bzrlib/tests/test_commands.py	2010-08-23 09:32:00 +0000
+++ b/bzrlib/tests/test_commands.py	2010-08-24 08:05:12 +0000
@@ -95,7 +95,7 @@
 class TestGetAlias(tests.TestCase):
 
     def _get_config(self, config_text):
-        my_config = config.GlobalConfig(_content=config_text)
+        my_config = config.GlobalConfig.from_bytes(config_text)
         return my_config
 
     def test_simple(self):

=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py	2010-08-23 09:32:00 +0000
+++ b/bzrlib/tests/test_config.py	2010-08-24 08:05:12 +0000
@@ -367,7 +367,7 @@
 class TestIniConfig(tests.TestCaseInTempDir):
 
     def make_config_parser(self, s):
-        conf = config.IniBasedConfig(_content=s)
+        conf = config.IniBasedConfig.from_bytes(s)
         return conf, conf._get_parser()
 
 
@@ -377,11 +377,11 @@
         my_config = config.IniBasedConfig()
 
     def test_from_fp(self):
-        my_config = config.IniBasedConfig(_content=sample_config_text)
+        my_config = config.IniBasedConfig.from_bytes(sample_config_text)
         self.assertIsInstance(my_config._get_parser(), configobj.ConfigObj)
 
     def test_cached(self):
-        my_config = config.IniBasedConfig(_content=sample_config_text)
+        my_config = config.IniBasedConfig.from_bytes(sample_config_text)
         parser = my_config._get_parser()
         self.failUnless(my_config._get_parser() is parser)
 
@@ -409,7 +409,7 @@
 
     def test_get_parser_file_parameter_is_deprecated_(self):
         config_file = StringIO(sample_config_text.encode('utf-8'))
-        conf = config.IniBasedConfig(_content=sample_config_text)
+        conf = config.IniBasedConfig.from_bytes(sample_config_text)
         conf = self.callDeprecated([
             'IniBasedConfig._get_parser(file=xxx) was deprecated in 2.3.'
             ' Use IniBasedConfig(_content=xxx) instead.'],
@@ -607,7 +607,7 @@
 class TestGlobalConfigItems(tests.TestCase):
 
     def test_user_id(self):
-        my_config = config.GlobalConfig(_content=sample_config_text)
+        my_config = config.GlobalConfig.from_bytes(sample_config_text)
         self.assertEqual(u"Erik B\u00e5gfors <erik at bagfors.nu>",
                          my_config._get_user_id())
 
@@ -616,11 +616,11 @@
         self.assertEqual(None, my_config._get_user_id())
 
     def test_configured_editor(self):
-        my_config = config.GlobalConfig(_content=sample_config_text)
+        my_config = config.GlobalConfig.from_bytes(sample_config_text)
         self.assertEqual("vim", my_config.get_editor())
 
     def test_signatures_always(self):
-        my_config = config.GlobalConfig(_content=sample_always_signatures)
+        my_config = config.GlobalConfig.from_bytes(sample_always_signatures)
         self.assertEqual(config.CHECK_NEVER,
                          my_config.signature_checking())
         self.assertEqual(config.SIGN_ALWAYS,
@@ -628,7 +628,7 @@
         self.assertEqual(True, my_config.signature_needed())
 
     def test_signatures_if_possible(self):
-        my_config = config.GlobalConfig(_content=sample_maybe_signatures)
+        my_config = config.GlobalConfig.from_bytes(sample_maybe_signatures)
         self.assertEqual(config.CHECK_NEVER,
                          my_config.signature_checking())
         self.assertEqual(config.SIGN_WHEN_REQUIRED,
@@ -636,7 +636,7 @@
         self.assertEqual(False, my_config.signature_needed())
 
     def test_signatures_ignore(self):
-        my_config = config.GlobalConfig(_content=sample_ignore_signatures)
+        my_config = config.GlobalConfig.from_bytes(sample_ignore_signatures)
         self.assertEqual(config.CHECK_ALWAYS,
                          my_config.signature_checking())
         self.assertEqual(config.SIGN_NEVER,
@@ -644,7 +644,7 @@
         self.assertEqual(False, my_config.signature_needed())
 
     def _get_sample_config(self):
-        my_config = config.GlobalConfig(_content=sample_config_text)
+        my_config = config.GlobalConfig.from_bytes(sample_config_text)
         return my_config
 
     def test_gpg_signing_command(self):
@@ -993,10 +993,10 @@
             global_config = sample_config_text
 
         config.ensure_config_dir_exists()
-        my_global_config = config.GlobalConfig(_content=global_config)
+        my_global_config = config.GlobalConfig.from_bytes(global_config)
         my_global_config._write_config_file()
-        my_location_config = config.LocationConfig(
-            my_branch.base, _content=sample_branches_text)
+        my_location_config = config.LocationConfig.from_bytes(
+            sample_branches_text, my_branch.base)
         my_location_config._write_config_file()
 
         my_config = config.BranchConfig(my_branch)
@@ -1068,12 +1068,12 @@
                           location_config=None, branch_data_config=None):
         my_branch = FakeBranch(location)
         if global_config is not None:
-            my_global_config = config.GlobalConfig(_content=global_config)
+            my_global_config = config.GlobalConfig.from_bytes(global_config)
             config.ensure_config_dir_exists()
             my_global_config._write_config_file()
         if location_config is not None:
-            my_location_config = config.LocationConfig(my_branch.base,
-                                                       _content=location_config)
+            my_location_config = config.LocationConfig.from_bytes(
+                location_config, my_branch.base)
             config.ensure_config_dir_exists()
             my_location_config._write_config_file()
         my_config = config.BranchConfig(my_branch)

=== modified file 'bzrlib/tests/test_smtp_connection.py'
--- a/bzrlib/tests/test_smtp_connection.py	2010-07-19 12:54:54 +0000
+++ b/bzrlib/tests/test_smtp_connection.py	2010-08-24 08:05:12 +0000
@@ -91,7 +91,7 @@
 class TestSMTPConnection(tests.TestCaseInTempDir):
 
     def get_connection(self, text, smtp_factory=None):
-        my_config = config.GlobalConfig(_content=text)
+        my_config = config.GlobalConfig.from_bytes(text)
         return smtp_connection.SMTPConnection(my_config,
                                               _smtp_factory=smtp_factory)
 



More information about the bazaar-commits mailing list