Rev 6133: Provide config.IdMatcher for config files defining secion names as unique ids in file:///home/vila/src/bzr/bugs/843638-id-matcher/

Vincent Ladeuil v.ladeuil+lp at free.fr
Wed Sep 7 08:23:49 UTC 2011


At file:///home/vila/src/bzr/bugs/843638-id-matcher/

------------------------------------------------------------
revno: 6133
revision-id: v.ladeuil+lp at free.fr-20110907082349-1ly8p12duy1exzn3
parent: pqm at pqm.ubuntu.com-20110906133507-jxpb08khpc90jvvo
fixes bug(s): https://launchpad.net/bugs/843638
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: 843638-id-matcher
timestamp: Wed 2011-09-07 10:23:49 +0200
message:
  Provide config.IdMatcher for config files defining secion names as unique ids
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2011-09-06 11:53:48 +0000
+++ b/bzrlib/config.py	2011-09-07 08:23:49 +0000
@@ -2891,8 +2891,8 @@
 class SectionMatcher(object):
     """Select sections into a given Store.
 
-    This intended to be used to postpone getting an iterable of sections from a
-    store.
+    This is intended to be used to postpone getting an iterable of sections
+    from a store.
     """
 
     def __init__(self, store):
@@ -2907,10 +2907,26 @@
             if self.match(s):
                 yield s
 
-    def match(self, secion):
+    def match(self, section):
+        """Does the proposed section match.
+
+        :param section: A Section object.
+
+        :returns: True if the section matches, False otherwise.
+        """
         raise NotImplementedError(self.match)
 
 
+class IdMatcher(SectionMatcher):
+
+    def __init__(self, store, section_id):
+        super(IdMatcher, self).__init__(store)
+        self.section_id = section_id
+
+    def match(self, section):
+        return section.id == self.section_id
+
+
 class LocationSection(Section):
 
     def __init__(self, section, length, extra_path):

=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py	2011-09-06 11:53:48 +0000
+++ b/bzrlib/tests/test_config.py	2011-09-07 08:23:49 +0000
@@ -2624,9 +2624,6 @@
     scenarios = [(key, {'get_store': builder}) for key, builder
                  in config.test_store_builder_registry.iteritems()]
 
-    def setUp(self):
-        super(TestReadonlyStore, self).setUp()
-
     def test_building_delays_load(self):
         store = self.get_store(self)
         self.assertEquals(False, store.is_loaded())
@@ -3061,12 +3058,13 @@
     # FIXME: It may be worth looking into removing the lock dir when it's not
     # needed anymore and look at possible fallouts for concurrent lockers. This
     # will matter if/when we use config files outside of bazaar directories
-    # (.bazaar or .bzr) -- vila 20110-04-11
+    # (.bazaar or .bzr) -- vila 20110-04-111
 
 
 class TestSectionMatcher(TestStore):
 
-    scenarios = [('location', {'matcher': config.LocationMatcher})]
+    scenarios = [('location', {'matcher': config.LocationMatcher}),
+                 ('id', {'matcher': config.IdMatcher}),]
 
     def get_store(self, file_name):
         return config.IniFileStore(self.get_readonly_transport(), file_name)
@@ -3181,6 +3179,33 @@
         self.assertEquals(expected_location, matcher.location)
 
 
+class TestIdMatcher(TestStore):
+
+    def setUp(self):
+        super(TestIdMatcher, self).setUp()
+        self.store = config.IniFileStore(self.get_readonly_transport(),
+                                         'foo.conf')
+        self.store._load_from_string('''
+[foo]
+option=foo
+[foo/baz]
+option=foo/baz
+[bar]
+option=bar
+''')
+
+    def test_matching(self):
+        matcher = config.IdMatcher(self.store, 'foo')
+        sections = list(matcher.get_sections())
+        self.assertLength(1, sections)
+        self.assertSectionContent(('foo', {'option': 'foo'}), sections[0])
+
+    def test_not_matching(self):
+        matcher = config.IdMatcher(self.store, 'baz')
+        sections = list(matcher.get_sections())
+        self.assertLength(0, sections)
+
+
 class TestStackGet(tests.TestCase):
 
     # FIXME: This should be parametrized for all known Stack or dedicated

=== modified file 'doc/developers/configuration.txt'
--- a/doc/developers/configuration.txt	2011-08-19 14:10:32 +0000
+++ b/doc/developers/configuration.txt	2011-09-07 08:23:49 +0000
@@ -86,13 +86,25 @@
 all data needed for the selection and uses it while processing the sections in
 ``get_sections``.
 
-Only ``ReadOnlySection`` objects are manipulated here but a ``SectionMatcher``
-can return dedicated ``Section`` to provide additional context (the
-``LocationSection`` add an ``extra_path`` attribute to implement the
-``appendpath`` policy for example).
+Only ``ReadOnlySection`` objects are manipulated here but a
+``SectionMatcher`` can return dedicated ``Section`` objects to provide
+additional context (the ``LocationSection`` add an ``extra_path`` attribute
+to implement the ``appendpath`` policy for example). If no sections match,
+an empty list is returned.
 
 .. FIXME: Replace the appendpath example if/when it's deprecated ;)
 
+Specific section matchers can be implemented by overriding ``get_sections``
+or just ``match``.
+
+``bzrlib`` provides:
+
+* ``LocationMatcher(store, location)``: To select all sections that match
+  ``location``.
+
+* ``IdMatcher(store, unique_id)``: To select a single section matching
+  ``unique_id``.
+
 Stacks
 ------
 

=== modified file 'doc/en/release-notes/bzr-2.5.txt'
--- a/doc/en/release-notes/bzr-2.5.txt	2011-09-06 13:35:07 +0000
+++ b/doc/en/release-notes/bzr-2.5.txt	2011-09-07 08:23:49 +0000
@@ -75,6 +75,10 @@
 * ``config.Option`` can now declare ``default_from_env``, a list of
   environment variables to get a default value from. (Vincent Ladeuil)
 
+* ``config.IdMatcher`` can be used to implement config stores and stacks
+  that need to provide specific option values for arbitrary unique IDs (svn
+  repository UUIDs, etc).  (Vincent Ladeuil, #843638)
+
 * New builtin ``bzr branches`` command, which lists all colocated branches
   in a directory. (Jelmer Vernooij, #826820)
 



More information about the bazaar-commits mailing list