Rev 5686: (jelmer) Factor out common code from BranchFormatRegistry, in file:///home/pqm/archives/thelove/bzr/%2Btrunk/
Canonical.com Patch Queue Manager
pqm at pqm.ubuntu.com
Fri Feb 25 00:47:50 UTC 2011
At file:///home/pqm/archives/thelove/bzr/%2Btrunk/
------------------------------------------------------------
revno: 5686 [merge]
revision-id: pqm at pqm.ubuntu.com-20110225004747-jct2ae1o9clrbjqn
parent: pqm at pqm.ubuntu.com-20110224173928-bj55mr0wx9k1zl6u
parent: jelmer at samba.org-20110224234221-byoonswaxvgjg077
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Fri 2011-02-25 00:47:47 +0000
message:
(jelmer) Factor out common code from BranchFormatRegistry,
RepositoryFormatRegistry and WorkingTreeFormatRegistry into
ControlComponentFormatRegistry. (Jelmer Vernooij)
added:
bzrlib/tests/test_controldir.py test_controldir.py-20110224120830-peu3bobygfcfsilp-1
modified:
bzrlib/branch.py branch.py-20050309040759-e4baf4e0d046576e
bzrlib/controldir.py controldir.py-20100802102926-hvtvh0uae5epuibp-1
bzrlib/repository.py rev_storage.py-20051111201905-119e9401e46257e3
bzrlib/tests/__init__.py selftest.py-20050531073622-8d0e3c8845c97a64
bzrlib/tests/test_bzrdir.py test_bzrdir.py-20060131065654-deba40eef51cf220
bzrlib/workingtree.py workingtree.py-20050511021032-29b6ec0a681e02e3
=== modified file 'bzrlib/branch.py'
--- a/bzrlib/branch.py 2011-02-23 13:15:21 +0000
+++ b/bzrlib/branch.py 2011-02-24 23:42:21 +0000
@@ -1532,7 +1532,7 @@
raise AssertionError("invalid heads: %r" % (heads,))
-class BranchFormat(object):
+class BranchFormat(controldir.ControlComponentFormat):
"""An encapsulation of the initialization and open routines for a format.
Formats provide three things:
@@ -2356,56 +2356,12 @@
return result
-class BranchFormatRegistry(registry.FormatRegistry):
+class BranchFormatRegistry(controldir.ControlComponentFormatRegistry):
"""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
@@ -2427,6 +2383,7 @@
# formats which have no format string are not discoverable
# and not independently creatable, so are not registered.
+__format4 = BzrBranchFormat4()
__format5 = BzrBranchFormat5()
__format6 = BzrBranchFormat6()
__format7 = BzrBranchFormat7()
@@ -2437,7 +2394,8 @@
format_registry.register(__format7)
format_registry.register(__format8)
format_registry.set_default(__format7)
-format_registry.register_extra(BzrBranchFormat4())
+format_registry.register_extra(__format4)
+network_format_registry.register(__format4.network_name(), __format4)
class BranchWriteLockResult(LogicalLockResult):
=== modified file 'bzrlib/controldir.py'
--- a/bzrlib/controldir.py 2011-02-23 12:40:56 +0000
+++ b/bzrlib/controldir.py 2011-02-24 12:17:19 +0000
@@ -622,6 +622,76 @@
raise NotImplementedError(self.clone_on_transport)
+class ControlComponentFormat(object):
+ """A component that can live inside of a .bzr meta directory."""
+
+ def get_format_string(self):
+ """Return the format of this format, if usable in meta directories."""
+ raise NotImplementedError(self.get_format_string)
+
+ def get_format_description(self):
+ """Return the short description for this format."""
+ raise NotImplementedError(self.get_format_description)
+
+
+class ControlComponentFormatRegistry(registry.FormatRegistry):
+ """A registry for control components (branch, workingtree, repository)."""
+
+ def __init__(self, other_registry=None):
+ super(ControlComponentFormatRegistry, self).__init__(other_registry)
+ self._extra_formats = []
+
+ def register(self, format):
+ """Register a new format."""
+ super(ControlComponentFormatRegistry, self).register(
+ format.get_format_string(), format)
+
+ def remove(self, format):
+ """Remove a registered format."""
+ super(ControlComponentFormatRegistry, self).remove(
+ format.get_format_string())
+
+ def register_extra(self, format):
+ """Register a format that can not be used in a metadir.
+
+ This is mainly useful to allow custom repository formats, such as older
+ Bazaar formats and foreign formats, to be tested.
+ """
+ self._extra_formats.append(registry._ObjectGetter(format))
+
+ def remove_extra(self, format):
+ """Remove an extra format.
+ """
+ self._extra_formats.remove(registry._ObjectGetter(format))
+
+ def register_extra_lazy(self, module_name, member_name):
+ """Register a format lazily.
+ """
+ self._extra_formats.append(
+ registry._LazyObjectGetter(module_name, member_name))
+
+ def _get_extra(self):
+ """Return all "extra" formats, not usable in meta directories."""
+ result = []
+ for getter in self._extra_formats:
+ f = getter.get_obj()
+ if callable(f):
+ f = f()
+ result.append(f)
+ return result
+
+ def _get_all(self):
+ """Return all formats, even those not usable in metadirs.
+ """
+ result = []
+ for name in self.keys():
+ fmt = self.get(name)
+ if callable(fmt):
+ fmt = fmt()
+ result.append(fmt)
+ return result + self._get_extra()
+
+
class ControlDirFormat(object):
"""An encapsulation of the initialization and open routines for a format.
@@ -879,7 +949,7 @@
"""Registry of user-selectable ControlDir subformats.
Differs from ControlDirFormat._formats in that it provides sub-formats,
- e.g. ControlDirMeta1 with weave repository. Also, it's more user-oriented.
+ e.g. BzrDirMeta1 with weave repository. Also, it's more user-oriented.
"""
def __init__(self):
=== modified file 'bzrlib/repository.py'
--- a/bzrlib/repository.py 2011-02-22 16:53:15 +0000
+++ b/bzrlib/repository.py 2011-02-24 12:19:13 +0000
@@ -2917,61 +2917,14 @@
control_files)
-class RepositoryFormatRegistry(registry.FormatRegistry):
+class RepositoryFormatRegistry(controldir.ControlComponentFormatRegistry):
"""Repository format registry."""
- def __init__(self, other_registry=None):
- super(RepositoryFormatRegistry, self).__init__(other_registry)
- self._extra_formats = []
-
- def register(self, format):
- """Register a new repository format."""
- super(RepositoryFormatRegistry, self).register(
- format.get_format_string(), format)
-
- def remove(self, format):
- """Remove a registered repository format."""
- super(RepositoryFormatRegistry, self).remove(
- format.get_format_string())
-
- def register_extra(self, format):
- """Register a repository format that can not be used in a metadir.
-
- This is mainly useful to allow custom repository formats, such as older
- Bazaar formats and foreign formats, to be tested.
- """
- self._extra_formats.append(registry._ObjectGetter(format))
-
- def remove_extra(self, format):
- """Remove an extra repository format.
- """
- self._extra_formats.remove(registry._ObjectGetter(format))
-
- def register_extra_lazy(self, module_name, member_name):
- """Register a repository format lazily.
- """
- self._extra_formats.append(
- registry._LazyObjectGetter(module_name, member_name))
-
def get_default(self):
"""Return the current default format."""
from bzrlib import bzrdir
return bzrdir.format_registry.make_bzrdir('default').repository_format
- def _get_extra(self):
- result = []
- for getter in self._extra_formats:
- f = getter.get_obj()
- if callable(f):
- f = f()
- result.append(f)
- return result
-
- def _get_all(self):
- """Return all repository formats, even those not usable in metadirs.
- """
- return [self.get(k) for k in self.keys()] + self._get_extra()
-
network_format_registry = registry.FormatRegistry()
"""Registry of formats indexed by their network name.
@@ -2993,7 +2946,7 @@
#####################################################################
# Repository Formats
-class RepositoryFormat(object):
+class RepositoryFormat(controldir.ControlComponentFormat):
"""A repository format.
Formats provide four things:
=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py 2011-02-20 11:07:56 +0000
+++ b/bzrlib/tests/__init__.py 2011-02-24 16:58:20 +0000
@@ -3750,6 +3750,7 @@
'bzrlib.tests.test_commit_merge',
'bzrlib.tests.test_config',
'bzrlib.tests.test_conflicts',
+ 'bzrlib.tests.test_controldir',
'bzrlib.tests.test_counted_lock',
'bzrlib.tests.test_crash',
'bzrlib.tests.test_decorators',
=== modified file 'bzrlib/tests/test_bzrdir.py'
--- a/bzrlib/tests/test_bzrdir.py 2011-02-19 12:38:04 +0000
+++ b/bzrlib/tests/test_bzrdir.py 2011-02-24 12:08:37 +0000
@@ -1484,3 +1484,4 @@
def test_exiting(self):
self._transport.put_bytes("a.~1~", "some content")
self.assertEqual("a.~2~", self._bzrdir._available_backup_name("a"))
+
=== added file 'bzrlib/tests/test_controldir.py'
--- a/bzrlib/tests/test_controldir.py 1970-01-01 00:00:00 +0000
+++ b/bzrlib/tests/test_controldir.py 2011-02-24 13:44:47 +0000
@@ -0,0 +1,71 @@
+# Copyright (C) 2011 Canonical Ltd
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+"""Tests for the ControlDir facility.
+
+For interface contract tests, see tests/per_control_dir.
+"""
+
+from bzrlib import (
+ controldir,
+ tests,
+ )
+
+
+class SampleComponentFormat(controldir.ControlComponentFormat):
+
+ def get_format_string(self):
+ return "Example component format."
+
+
+class SampleExtraComponentFormat(controldir.ControlComponentFormat):
+ """Extra format, no format string."""
+
+
+class TestMetaComponentFormatRegistry(tests.TestCase):
+
+ def setUp(self):
+ super(TestMetaComponentFormatRegistry, self).setUp()
+ self.registry = controldir.ControlComponentFormatRegistry()
+
+ def test_register_unregister_format(self):
+ format = SampleComponentFormat()
+ self.registry.register(format)
+ self.assertEquals(format,
+ self.registry.get("Example component format."))
+ self.registry.remove(format)
+ self.assertRaises(KeyError, self.registry.get,
+ "Example component format.")
+
+ def test_get_all(self):
+ format = SampleComponentFormat()
+ self.assertEquals([], self.registry._get_all())
+ self.registry.register(format)
+ self.assertEquals([format], self.registry._get_all())
+
+ def test_register_extra(self):
+ format = SampleExtraComponentFormat()
+ 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_controldir",
+ "SampleExtraComponentFormat")
+ formats = self.registry._get_all()
+ self.assertEquals(1, len(formats))
+ self.assertIsInstance(formats[0], SampleExtraComponentFormat)
=== modified file 'bzrlib/workingtree.py'
--- a/bzrlib/workingtree.py 2011-02-24 11:46:57 +0000
+++ b/bzrlib/workingtree.py 2011-02-25 00:47:47 +0000
@@ -2859,42 +2859,13 @@
return path[:-len(suffix)]
-class WorkingTreeFormatRegistry(registry.FormatRegistry):
+class WorkingTreeFormatRegistry(controldir.ControlComponentFormatRegistry):
"""Registry for working tree formats."""
def __init__(self, other_registry=None):
super(WorkingTreeFormatRegistry, self).__init__(other_registry)
- self._extra_formats = []
self._default_format = None
- def register(self, format):
- """Register a new repository format."""
- super(WorkingTreeFormatRegistry, self).register(
- format.get_format_string(), format)
-
- def remove(self, format):
- """Remove a registered repository format."""
- super(WorkingTreeFormatRegistry, self).remove(format.get_format_string())
-
- def register_extra(self, format):
- """Register a repository format that can not be used in a metadir.
-
- This is mainly useful to allow custom repository formats, such as older
- Bazaar formats and foreign formats, to be tested.
- """
- self._extra_formats.append(registry._ObjectGetter(format))
-
- def remove_extra(self, format):
- """Remove an extra repository format.
- """
- self._extra_formats.remove(registry._ObjectGetter(format))
-
- def register_extra_lazy(self, module_name, member_name):
- """Register a repository format lazily.
- """
- self._extra_formats.append(
- registry._LazyObjectGetter(module_name, member_name))
-
def get_default(self):
"""Return the current default format."""
return self._default_format
@@ -2902,25 +2873,11 @@
def set_default(self, format):
self._default_format = format
- def _get_extra(self):
- result = []
- for getter in self._extra_formats:
- f = getter.get_obj()
- if callable(f):
- f = f()
- result.append(f)
- return result
-
- def _get_all(self):
- """Return all repository formats, even those not usable in metadirs.
- """
- return [self.get(k) for k in self.keys()] + self._get_extra()
-
format_registry = WorkingTreeFormatRegistry()
-class WorkingTreeFormat(object):
+class WorkingTreeFormat(controldir.ControlComponentFormat):
"""An encapsulation of the initialization and open routines for a format.
Formats provide three things:
More information about the bazaar-commits
mailing list