Rev 5680: (jelmer) Add a BranchFormatRegistry. (Jelmer Vernooij) in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Wed Feb 23 15:03:29 UTC 2011


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

------------------------------------------------------------
revno: 5680 [merge]
revision-id: pqm at pqm.ubuntu.com-20110223150327-d9k3rvhhy8ij3t4j
parent: pqm at pqm.ubuntu.com-20110223135938-hcq8y5a4qdybu9t0
parent: jelmer at samba.org-20110223131521-vyvd00j75qaeb9cl
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Wed 2011-02-23 15:03:27 +0000
message:
  (jelmer) Add a BranchFormatRegistry. (Jelmer Vernooij)
modified:
  bzrlib/branch.py               branch.py-20050309040759-e4baf4e0d046576e
  bzrlib/bzrdir.py               bzrdir.py-20060131065624-156dfea39c4387cb
  bzrlib/registry.py             lazy_factory.py-20060809213415-2gfvqadtvdn0phtg-1
  bzrlib/tests/per_branch/__init__.py __init__.py-20060123013057-b12a52c3f361daf4
  bzrlib/tests/test_branch.py    test_branch.py-20060116013032-97819aa07b8ab3b5
  bzrlib/tests/test_remote.py    test_remote.py-20060720103555-yeeg2x51vn0rbtdp-2
  doc/en/release-notes/bzr-2.4.txt bzr2.4.txt-20110114053217-k7ym9jfz243fddjm-1
=== modified file 'bzrlib/branch.py'
--- a/bzrlib/branch.py	2011-02-21 22:43:53 +0000
+++ b/bzrlib/branch.py	2011-02-23 13:15:21 +0000
@@ -1550,15 +1550,6 @@
     object will be created every time regardless.
     """
 
-    _default_format = None
-    """The default format used for new branches."""
-
-    _formats = {}
-    """The known formats."""
-
-    _extra_formats = []
-    """Extra formats that can not be part of a metadir."""
-
     can_set_append_revisions_only = True
 
     def __eq__(self, other):
@@ -1573,33 +1564,27 @@
         try:
             transport = a_bzrdir.get_branch_transport(None, name=name)
             format_string = transport.get_bytes("format")
-            format = klass._formats[format_string]
-            if isinstance(format, MetaDirBranchFormatFactory):
-                return format()
-            return format
+            return format_registry.get(format_string)
         except errors.NoSuchFile:
             raise errors.NotBranchError(path=transport.base, bzrdir=a_bzrdir)
         except KeyError:
             raise errors.UnknownFormatError(format=format_string, kind='branch')
 
     @classmethod
+    @deprecated_method(deprecated_in((2, 4, 0)))
     def get_default_format(klass):
         """Return the current default format."""
-        return klass._default_format
+        return format_registry.get_default()
 
     @classmethod
+    @deprecated_method(deprecated_in((2, 4, 0)))
     def get_formats(klass):
         """Get all the known formats.
 
         Warning: This triggers a load of all lazy registered formats: do not
         use except when that is desireed.
         """
-        result = []
-        for fmt in klass._formats.values():
-            if isinstance(fmt, MetaDirBranchFormatFactory):
-                fmt = fmt()
-            result.append(fmt)
-        return result + klass._extra_formats
+        return format_registry._get_all()
 
     def get_reference(self, a_bzrdir, name=None):
         """Get the target reference of the branch in a_bzrdir.
@@ -1745,35 +1730,19 @@
         raise NotImplementedError(self.open)
 
     @classmethod
-    def register_extra_format(klass, format):
-        """Register a branch format that can not be part of a metadir.
-
-        This is mainly useful to allow custom branch formats, such as
-        older Bazaar formats and foreign formats, to be tested
-        """
-        klass._extra_formats.append(format)
-        network_format_registry.register(
-            format.network_name(), format.__class__)
-
-    @classmethod
+    @deprecated_method(deprecated_in((2, 4, 0)))
     def register_format(klass, format):
         """Register a metadir format.
-        
+
         See MetaDirBranchFormatFactory for the ability to register a format
         without loading the code the format needs until it is actually used.
         """
-        klass._formats[format.get_format_string()] = format
-        # Metadir formats have a network name of their format string, and get
-        # registered as factories.
-        if isinstance(format, MetaDirBranchFormatFactory):
-            network_format_registry.register(format.get_format_string(), format)
-        else:
-            network_format_registry.register(format.get_format_string(),
-                format.__class__)
+        format_registry.register(format)
 
     @classmethod
+    @deprecated_method(deprecated_in((2, 4, 0)))
     def set_default_format(klass, format):
-        klass._default_format = format
+        format_registry.set_default(format)
 
     def supports_set_append_revisions_only(self):
         """True if this format supports set_append_revisions_only."""
@@ -1788,12 +1757,9 @@
         return False # by default
 
     @classmethod
+    @deprecated_method(deprecated_in((2, 4, 0)))
     def unregister_format(klass, format):
-        del klass._formats[format.get_format_string()]
-
-    @classmethod
-    def unregister_extra_format(klass, format):
-        klass._extra_formats.remove(format)
+        format_registry.remove(format)
 
     def __str__(self):
         return self.get_format_description().rstrip()
@@ -2390,6 +2356,64 @@
         return result
 
 
+class BranchFormatRegistry(registry.FormatRegistry):
+    """Branch format registry."""
+
+    def __init__(self, other_registry=None):
+        super(BranchFormatRegistry, self).__init__(other_registry)
+        self._default_format = None
+        self._extra_formats = []
+
+    def register(self, format):
+        """Register a new branch format."""
+        super(BranchFormatRegistry, self).register(
+            format.get_format_string(), format)
+
+    def remove(self, format):
+        """Remove a registered branch format."""
+        super(BranchFormatRegistry, self).remove(
+            format.get_format_string())
+
+    def register_extra(self, format):
+        """Register a branch format that can not be part of a metadir.
+
+        This is mainly useful to allow custom branch formats, such as
+        older Bazaar formats and foreign formats, to be tested
+        """
+        self._extra_formats.append(registry._ObjectGetter(format))
+        network_format_registry.register(
+            format.network_name(), format.__class__)
+
+    def register_extra_lazy(self, module_name, member_name):
+        """Register a branch format lazily.
+        """
+        self._extra_formats.append(
+            registry._LazyObjectGetter(module_name, member_name))
+
+    @classmethod
+    def unregister_extra(self, format):
+        self._extra_formats.remove(registry._ObjectGetter(format))
+
+    def _get_all(self):
+        result = []
+        for name, fmt in self.iteritems():
+            if callable(fmt):
+                fmt = fmt()
+            result.append(fmt)
+        for objgetter in self._extra_formats:
+            fmt = objgetter.get_obj()
+            if callable(fmt):
+                fmt = fmt()
+            result.append(fmt)
+        return result
+
+    def set_default(self, format):
+        self._default_format = format
+
+    def get_default(self):
+        return self._default_format
+
+
 network_format_registry = registry.FormatRegistry()
 """Registry of formats indexed by their network name.
 
@@ -2398,6 +2422,8 @@
 BranchFormat.network_name() for more detail.
 """
 
+format_registry = BranchFormatRegistry(network_format_registry)
+
 
 # formats which have no format string are not discoverable
 # and not independently creatable, so are not registered.
@@ -2405,13 +2431,13 @@
 __format6 = BzrBranchFormat6()
 __format7 = BzrBranchFormat7()
 __format8 = BzrBranchFormat8()
-BranchFormat.register_format(__format5)
-BranchFormat.register_format(BranchReferenceFormat())
-BranchFormat.register_format(__format6)
-BranchFormat.register_format(__format7)
-BranchFormat.register_format(__format8)
-BranchFormat.set_default_format(__format7)
-BranchFormat.register_extra_format(BzrBranchFormat4())
+format_registry.register(__format5)
+format_registry.register(BranchReferenceFormat())
+format_registry.register(__format6)
+format_registry.register(__format7)
+format_registry.register(__format8)
+format_registry.set_default(__format7)
+format_registry.register_extra(BzrBranchFormat4())
 
 
 class BranchWriteLockResult(LogicalLockResult):
@@ -3410,7 +3436,7 @@
 
     @classmethod
     def _get_branch_formats_to_test(klass):
-        return [(BranchFormat._default_format, BranchFormat._default_format)]
+        return [(format_registry.get_default(), format_registry.get_default())]
 
     @classmethod
     def unwrap_format(klass, format):
@@ -3602,6 +3628,8 @@
         :param run_hooks: Private parameter - if false, this branch
             is being called because it's the master of the primary branch,
             so it should not run its hooks.
+            is being called because it's the master of the primary branch,
+            so it should not run its hooks.
         :param _override_hook_target: Private parameter - set the branch to be
             supplied as the target_branch to pull hooks.
         :param local: Only update the local branch, and not the bound branch.

=== modified file 'bzrlib/bzrdir.py'
--- a/bzrlib/bzrdir.py	2011-02-19 17:37:45 +0000
+++ b/bzrlib/bzrdir.py	2011-02-23 13:15:21 +0000
@@ -1957,8 +1957,8 @@
 
     def get_branch_format(self):
         if self._branch_format is None:
-            from bzrlib.branch import BranchFormat
-            self._branch_format = BranchFormat.get_default_format()
+            from bzrlib.branch import format_registry as branch_format_registry
+            self._branch_format = branch_format_registry.get_default()
         return self._branch_format
 
     def set_branch_format(self, format):

=== modified file 'bzrlib/registry.py'
--- a/bzrlib/registry.py	2010-11-05 20:54:32 +0000
+++ b/bzrlib/registry.py	2011-02-19 15:26:45 +0000
@@ -239,6 +239,14 @@
         Registry.__init__(self)
         self._other_registry = other_registry
 
+    def register(self, key, obj, help=None, info=None,
+                 override_existing=False):
+        Registry.register(self, key, obj, help=help, info=info,
+            override_existing=override_existing)
+        if self._other_registry is not None:
+            self._other_registry.register(key, obj, help=help,
+                info=info, override_existing=override_existing)
+
     def register_lazy(self, key, module_name, member_name,
                       help=None, info=None,
                       override_existing=False):
@@ -255,5 +263,3 @@
         if callable(r):
             r = r()
         return r
-
-

=== modified file 'bzrlib/tests/per_branch/__init__.py'
--- a/bzrlib/tests/per_branch/__init__.py	2011-02-04 13:15:13 +0000
+++ b/bzrlib/tests/per_branch/__init__.py	2011-02-19 13:23:40 +0000
@@ -28,9 +28,8 @@
     errors,
     tests,
     )
-from bzrlib.branch import (BranchFormat,
-                           )
-from bzrlib.remote import RemoteBranchFormat, RemoteBzrDirFormat
+from bzrlib.branch import format_registry
+from bzrlib.remote import RemoteBranchFormat
 from bzrlib.tests import test_server
 from bzrlib.tests.per_controldir.test_controldir import TestCaseWithControlDir
 from bzrlib.transport import memory
@@ -131,7 +130,7 @@
     # Generate a list of branch formats and their associated bzrdir formats to
     # use.
     combinations = [(format, format._matchingbzrdir) for format in
-         BranchFormat.get_formats()]
+         format_registry._get_all()]
     scenarios = make_scenarios(
         # None here will cause the default vfs transport server to be used.
         None,

=== modified file 'bzrlib/tests/test_branch.py'
--- a/bzrlib/tests/test_branch.py	2011-02-07 03:24:37 +0000
+++ b/bzrlib/tests/test_branch.py	2011-02-19 16:42:16 +0000
@@ -32,7 +32,6 @@
     symbol_versioning,
     tests,
     trace,
-    transport,
     urlutils,
     )
 
@@ -41,7 +40,7 @@
 
     def test_default_format(self):
         # update this if you change the default branch format
-        self.assertIsInstance(_mod_branch.BranchFormat.get_default_format(),
+        self.assertIsInstance(_mod_branch.format_registry.get_default(),
                 _mod_branch.BzrBranchFormat7)
 
     def test_default_format_is_same_as_bzrdir_default(self):
@@ -49,13 +48,13 @@
         # set, but at the moment that's not true -- mbp 20070814 --
         # https://bugs.launchpad.net/bzr/+bug/132376
         self.assertEqual(
-            _mod_branch.BranchFormat.get_default_format(),
+            _mod_branch.format_registry.get_default(),
             bzrdir.BzrDirFormat.get_default_format().get_branch_format())
 
     def test_get_set_default_format(self):
         # set the format and then set it back again
-        old_format = _mod_branch.BranchFormat.get_default_format()
-        _mod_branch.BranchFormat.set_default_format(SampleBranchFormat())
+        old_format = _mod_branch.format_registry.get_default()
+        _mod_branch.format_registry.set_default(SampleBranchFormat())
         try:
             # the default branch format is used by the meta dir format
             # which is not the default bzrdir format at this point
@@ -63,9 +62,9 @@
             result = dir.create_branch()
             self.assertEqual(result, 'A branch')
         finally:
-            _mod_branch.BranchFormat.set_default_format(old_format)
+            _mod_branch.format_registry.set_default(old_format)
         self.assertEqual(old_format,
-                         _mod_branch.BranchFormat.get_default_format())
+                         _mod_branch.format_registry.get_default())
 
 
 class TestBranchFormat5(tests.TestCaseWithTransport):
@@ -181,25 +180,14 @@
             self.failUnless(isinstance(found_format, format.__class__))
         check_format(_mod_branch.BzrBranchFormat5(), "bar")
 
-    def test_extra_format(self):
-        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
-        SampleSupportedBranchFormat().initialize(dir)
-        format = SampleExtraBranchFormat()
-        _mod_branch.BranchFormat.register_extra_format(format)
-        self.addCleanup(_mod_branch.BranchFormat.unregister_extra_format,
-            format)
-        self.assertTrue(format in _mod_branch.BranchFormat.get_formats())
-        self.assertEquals(format,
-            _mod_branch.network_format_registry.get("extra"))
-
     def test_find_format_factory(self):
         dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
         SampleSupportedBranchFormat().initialize(dir)
         factory = _mod_branch.MetaDirBranchFormatFactory(
             SampleSupportedBranchFormatString,
             "bzrlib.tests.test_branch", "SampleSupportedBranchFormat")
-        _mod_branch.BranchFormat.register_format(factory)
-        self.addCleanup(_mod_branch.BranchFormat.unregister_format, factory)
+        _mod_branch.format_registry.register(factory)
+        self.addCleanup(_mod_branch.format_registry.remove, factory)
         b = _mod_branch.Branch.open(self.get_url())
         self.assertEqual(b, "opened supported branch.")
 
@@ -217,13 +205,15 @@
                           dir)
 
     def test_register_unregister_format(self):
+        # Test the deprecated format registration functions
         format = SampleBranchFormat()
         # make a control dir
         dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
         # make a branch
         format.initialize(dir)
         # register a format for it.
-        _mod_branch.BranchFormat.register_format(format)
+        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
+            _mod_branch.BranchFormat.register_format, format)
         # which branch.Open will refuse (not supported)
         self.assertRaises(errors.UnsupportedFormatError,
                           _mod_branch.Branch.open, self.get_url())
@@ -233,10 +223,53 @@
             format.open(dir),
             bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
         # unregister the format
-        _mod_branch.BranchFormat.unregister_format(format)
+        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
+            _mod_branch.BranchFormat.unregister_format, format)
         self.make_branch_and_tree('bar')
 
 
+class TestBranchFormatRegistry(tests.TestCase):
+
+    def setUp(self):
+        super(TestBranchFormatRegistry, self).setUp()
+        self.registry = _mod_branch.BranchFormatRegistry()
+
+    def test_default(self):
+        self.assertIs(None, self.registry.get_default())
+        format = SampleBranchFormat()
+        self.registry.set_default(format)
+        self.assertEquals(format, self.registry.get_default())
+
+    def test_register_unregister_format(self):
+        format = SampleBranchFormat()
+        self.registry.register(format)
+        self.assertEquals(format,
+            self.registry.get("Sample branch format."))
+        self.registry.remove(format)
+        self.assertRaises(KeyError, self.registry.get,
+            "Sample branch format.")
+
+    def test_get_all(self):
+        format = SampleBranchFormat()
+        self.assertEquals([], self.registry._get_all())
+        self.registry.register(format)
+        self.assertEquals([format], self.registry._get_all())
+
+    def test_register_extra(self):
+        format = SampleExtraBranchFormat()
+        self.assertEquals([], self.registry._get_all())
+        self.registry.register_extra(format)
+        self.assertEquals([format], self.registry._get_all())
+
+    def test_register_extra_lazy(self):
+        self.assertEquals([], self.registry._get_all())
+        self.registry.register_extra_lazy("bzrlib.tests.test_branch",
+            "SampleExtraBranchFormat")
+        formats = self.registry._get_all()
+        self.assertEquals(1, len(formats))
+        self.assertIsInstance(formats[0], SampleExtraBranchFormat)
+
+
 #Used by TestMetaDirBranchFormatFactory 
 FakeLazyFormat = None
 
@@ -742,5 +775,3 @@
                           _mod_branch._run_with_write_locked_target,
                           lockable, self.func_that_raises)
         self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
-
-

=== modified file 'bzrlib/tests/test_remote.py'
--- a/bzrlib/tests/test_remote.py	2011-02-10 15:13:32 +0000
+++ b/bzrlib/tests/test_remote.py	2011-02-19 13:23:40 +0000
@@ -1859,7 +1859,7 @@
 
     def test_get_format_description(self):
         remote_format = RemoteBranchFormat()
-        real_format = branch.BranchFormat.get_default_format()
+        real_format = branch.format_registry.get_default()
         remote_format._network_name = real_format.network_name()
         self.assertEqual(remoted_description(real_format),
             remote_format.get_format_description())

=== modified file 'doc/en/release-notes/bzr-2.4.txt'
--- a/doc/en/release-notes/bzr-2.4.txt	2011-02-22 11:59:59 +0000
+++ b/doc/en/release-notes/bzr-2.4.txt	2011-02-23 13:15:21 +0000
@@ -120,6 +120,10 @@
 * All methods and arguments that were deprecated before 2.0
   have been removed. (Jelmer Vernooij)
 
+* Branch formats should now be registered on the format registry
+  (``bzrlib.branch.format_registry``) rather than using the class
+  methods on ``BranchFormat``. (Jelmer Vernooij, #714729)
+
 * ``BranchFormat.supports_leaving_lock()`` and
   ``RepositoryFormat.supports_leaving_lock`` flags have been added.
   (Jelmer Vernooij)




More information about the bazaar-commits mailing list