Rev 2242: Change RepositoryFormat to use a Registry rather than ad-hoc dictionary in file:///home/mbp/bzr/Work/repoformats/

Martin Pool mbp at sourcefrog.net
Wed Jan 24 07:12:11 GMT 2007


------------------------------------------------------------
revno: 2242
revision-id: mbp at sourcefrog.net-20070124071209-yqiths20n6wxqaqr
parent: pqm at pqm.ubuntu.com-20070120020104-a192555a9165b259
committer: Martin Pool <mbp at sourcefrog.net>
branch nick: repoformats
timestamp: Wed 2007-01-24 18:12:09 +1100
message:
  Change RepositoryFormat to use a Registry rather than ad-hoc dictionary
added:
  bzrlib/repofmt/                repofmt-20070124052833-mplndq0aywl30b35-1
modified:
  bzrlib/registry.py             lazy_factory.py-20060809213415-2gfvqadtvdn0phtg-1
  bzrlib/repository.py           rev_storage.py-20051111201905-119e9401e46257e3
  bzrlib/tests/repository_implementations/__init__.py __init__.py-20060131092037-9564957a7d4a841b
  bzrlib/tests/test_repository.py test_repository.py-20060131075918-65c555b881612f4d
=== added directory 'bzrlib/repofmt'
=== modified file 'bzrlib/registry.py'
--- a/bzrlib/registry.py	2006-10-16 01:50:48 +0000
+++ b/bzrlib/registry.py	2007-01-24 07:12:09 +0000
@@ -200,6 +200,14 @@
         for key, getter in self._dict.iteritems():
             yield key, getter.get_obj()
 
+    def itervalues(self):
+        """Iterate all registry values in key order.
+
+        This imports any lazily registered entries.
+        """
+        for key in self.keys():
+            yield self.get(key)
+
     def _set_default_key(self, key):
         if not self._dict.has_key(key):
             raise KeyError('No object registered under key %s.' % key)

=== modified file 'bzrlib/repository.py'
--- a/bzrlib/repository.py	2007-01-17 15:37:08 +0000
+++ b/bzrlib/repository.py	2007-01-24 07:12:09 +0000
@@ -1,4 +1,4 @@
-# Copyright (C) 2005, 2006 Canonical Ltd
+# Copyright (C) 2005, 2006, 2007 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
@@ -37,6 +37,7 @@
     lockable_files,
     lockdir,
     osutils,
+    registry,
     revision as _mod_revision,
     symbol_versioning,
     transactions,
@@ -1159,35 +1160,58 @@
     parameterisation.
     """
 
-    _default_format = None
-    """The default format used for new repositories."""
-
-    _formats = {}
-    """The known formats."""
+    _registry = registry.Registry()
+    """Registry of formats, indexed by their identifying format string."""
 
     def __str__(self):
         return "<%s>" % self.__class__.__name__
 
     @classmethod
     def find_format(klass, a_bzrdir):
-        """Return the format for the repository object in a_bzrdir."""
+        """Return the format for the repository object in a_bzrdir.
+        
+        This is used by bzr native formats that have a "format" file in
+        the repository.  Other methods may be used by different types of 
+        control directory.
+        """
         try:
             transport = a_bzrdir.get_repository_transport(None)
             format_string = transport.get("format").read()
-            return klass._formats[format_string]
+            return klass._registry.get(format_string)
         except errors.NoSuchFile:
             raise errors.NoRepositoryPresent(a_bzrdir)
         except KeyError:
             raise errors.UnknownFormatError(format=format_string)
 
-    def _get_control_store(self, repo_transport, control_files):
-        """Return the control store for this repository."""
-        raise NotImplementedError(self._get_control_store)
+    @classmethod
+    def register_format(klass, format):
+        klass._registry.register(format.get_format_string(), format)
+
+    @classmethod
+    @deprecated_method(symbol_versioning.zero_fourteen)
+    def set_default_format(klass, format):
+        klass._set_default_format(format)
+
+    @classmethod
+    def _set_default_format(klass, format):
+        """Set the default format for new Repository creation.
+
+        The format must already be registered.
+        """
+        klass._registry.default_key = format.get_format_string()
+
+    @classmethod
+    def unregister_format(klass, format):
+        klass._registry.remove(format.get_format_string())
     
     @classmethod
     def get_default_format(klass):
         """Return the current default format."""
-        return klass._default_format
+        return klass._registry.get(klass._registry.default_key)
+
+    def _get_control_store(self, repo_transport, control_files):
+        """Return the control store for this repository."""
+        raise NotImplementedError(self._get_control_store)
 
     def get_format_string(self):
         """Return the ASCII format string that identifies this format.
@@ -1275,24 +1299,6 @@
         """
         raise NotImplementedError(self.open)
 
-    @classmethod
-    def register_format(klass, format):
-        klass._formats[format.get_format_string()] = format
-
-    @classmethod
-    @deprecated_method(symbol_versioning.zero_fourteen)
-    def set_default_format(klass, format):
-        klass._set_default_format(format)
-
-    @classmethod
-    def _set_default_format(klass, format):
-        klass._default_format = format
-
-    @classmethod
-    def unregister_format(klass, format):
-        assert klass._formats[format.get_format_string()] is format
-        del klass._formats[format.get_format_string()]
-
 
 class PreSplitOutRepositoryFormat(RepositoryFormat):
     """Base class for the pre split out repository formats."""
@@ -1807,7 +1813,8 @@
 # formats which have no format string are not discoverable
 # and not independently creatable, so are not registered.
 RepositoryFormat.register_format(RepositoryFormat7())
-# KEEP in sync with bzrdir.format_registry default
+# KEEP in sync with bzrdir.format_registry default, which controls the overall
+# default control directory format
 _default_format = RepositoryFormatKnit1()
 RepositoryFormat.register_format(_default_format)
 RepositoryFormat.register_format(RepositoryFormatKnit2())

=== modified file 'bzrlib/tests/repository_implementations/__init__.py'
--- a/bzrlib/tests/repository_implementations/__init__.py	2006-10-11 23:08:27 +0000
+++ b/bzrlib/tests/repository_implementations/__init__.py	2007-01-24 07:12:09 +0000
@@ -1,6 +1,6 @@
-# Copyright (C) 2006 Canonical Ltd
+# Copyright (C) 2006, 2007 Canonical Ltd
 # Authors: Robert Collins <robert.collins at canonical.com>
-# -*- coding: utf-8 -*-
+#          and others
 #
 # 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
@@ -47,13 +47,14 @@
         'bzrlib.tests.repository_implementations.test_repository',
         'bzrlib.tests.repository_implementations.test_revision',
         ]
+    all_formats = list(RepositoryFormat._registry.itervalues()) \
+        + _legacy_formats
     adapter = RepositoryTestProviderAdapter(
         default_transport,
         # None here will cause a readonly decorator to be created
         # by the TestCaseWithTransport.get_readonly_transport method.
         None,
-        [(format, format._matchingbzrdir) for format in 
-         RepositoryFormat._formats.values() + _legacy_formats])
+        [(format, format._matchingbzrdir) for format in all_formats])
     loader = TestLoader()
     adapt_modules(test_repository_implementations, adapter, loader, result)
     return result

=== modified file 'bzrlib/tests/test_repository.py'
--- a/bzrlib/tests/test_repository.py	2007-01-05 06:32:57 +0000
+++ b/bzrlib/tests/test_repository.py	2007-01-24 07:12:09 +0000
@@ -34,33 +34,40 @@
                            UnknownFormatError,
                            UnsupportedFormatError,
                            )
-import bzrlib.repository as repository
+from bzrlib.repository import RepositoryFormat
 from bzrlib.tests import TestCase, TestCaseWithTransport
 from bzrlib.transport import get_transport
 from bzrlib.transport.memory import MemoryServer
-from bzrlib import upgrade, workingtree
+from bzrlib import (
+    repository,
+    upgrade,
+    workingtree,
+    )
 
 
 class TestDefaultFormat(TestCase):
 
     def test_get_set_default_format(self):
-        private_default = repository._default_format.__class__
-        old_format = repository.RepositoryFormat.get_default_format()
-        self.assertTrue(isinstance(old_format, private_default))
-        self.applyDeprecated(symbol_versioning.zero_fourteen, 
-            repository.RepositoryFormat.set_default_format, 
-            SampleRepositoryFormat())
-        # creating a repository should now create an instrumented dir.
+        old_format = RepositoryFormat.get_default_format()
+        test_format = SampleRepositoryFormat()
+        RepositoryFormat.register_format(test_format)
         try:
-            # the default branch format is used by the meta dir format
-            # which is not the default bzrdir format at this point
-            dir = bzrdir.BzrDirMetaFormat1().initialize('memory:///')
-            result = dir.create_repository()
-            self.assertEqual(result, 'A bzr repository dir')
-        finally:
             self.applyDeprecated(symbol_versioning.zero_fourteen, 
-                repository.RepositoryFormat.set_default_format, old_format)
-        self.assertEqual(old_format, repository.RepositoryFormat.get_default_format())
+                RepositoryFormat.set_default_format, 
+                test_format)
+            # creating a repository should now create an instrumented dir.
+            try:
+                # the default branch format is used by the meta dir format
+                # which is not the default bzrdir format at this point
+                dir = bzrdir.BzrDirMetaFormat1().initialize('memory:///')
+                result = dir.create_repository()
+                self.assertEqual(result, 'A bzr repository dir')
+            finally:
+                self.applyDeprecated(symbol_versioning.zero_fourteen,
+                    RepositoryFormat.set_default_format, old_format)
+        finally:
+            RepositoryFormat.unregister_format(test_format)
+        self.assertEqual(old_format, RepositoryFormat.get_default_format())
 
 
 class SampleRepositoryFormat(repository.RepositoryFormat):




More information about the bazaar-commits mailing list