Rev 4258: simplify filter registration API (Ian Clatworthy) in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Mon Apr 6 20:04:47 BST 2009


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

------------------------------------------------------------
revno: 4258
revision-id: pqm at pqm.ubuntu.com-20090406190443-d2e9bmch7wb8p7u1
parent: pqm at pqm.ubuntu.com-20090406052052-fyud2y3vlghiasig
parent: ian.clatworthy at canonical.com-20090406174308-7r4v0x05becjlhgd
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Mon 2009-04-06 20:04:43 +0100
message:
  simplify filter registration API (Ian Clatworthy)
modified:
  bzrlib/filters/__init__.py     __init__.py-20080416080515-mkxl29amuwrf6uir-2
  bzrlib/filters/eol.py          eol.py-20090327060429-todzdjmqt3bpv5r8-1
  bzrlib/tests/test_filters.py   test_filters.py-20080417120614-tc3zok0vvvprsc99-1
    ------------------------------------------------------------
    revno: 4257.2.1
    revision-id: ian.clatworthy at canonical.com-20090406174308-7r4v0x05becjlhgd
    parent: pqm at pqm.ubuntu.com-20090406052052-fyud2y3vlghiasig
    parent: ian.clatworthy at canonical.com-20090406110601-6qxo99uw6cr542q2
    committer: Ian Clatworthy <ian.clatworthy at canonical.com>
    branch nick: ianc-integration
    timestamp: Tue 2009-04-07 03:43:08 +1000
    message:
      simplify filter registration API (Ian Clatworthy)
    modified:
      bzrlib/filters/__init__.py     __init__.py-20080416080515-mkxl29amuwrf6uir-2
      bzrlib/filters/eol.py          eol.py-20090327060429-todzdjmqt3bpv5r8-1
      bzrlib/tests/test_filters.py   test_filters.py-20080417120614-tc3zok0vvvprsc99-1
    ------------------------------------------------------------
    revno: 4257.1.1
    revision-id: ian.clatworthy at canonical.com-20090406110601-6qxo99uw6cr542q2
    parent: pqm at pqm.ubuntu.com-20090406052052-fyud2y3vlghiasig
    committer: Ian Clatworthy <ian.clatworthy at canonical.com>
    branch nick: bzr.filter-registration-api
    timestamp: Mon 2009-04-06 21:06:01 +1000
    message:
      register content filters using a callable, not a dict+callable
    modified:
      bzrlib/filters/__init__.py     __init__.py-20080416080515-mkxl29amuwrf6uir-2
      bzrlib/filters/eol.py          eol.py-20090327060429-todzdjmqt3bpv5r8-1
      bzrlib/tests/test_filters.py   test_filters.py-20080417120614-tc3zok0vvvprsc99-1
=== modified file 'bzrlib/filters/__init__.py'
--- a/bzrlib/filters/__init__.py	2009-03-29 03:05:23 +0000
+++ b/bzrlib/filters/__init__.py	2009-04-06 11:06:01 +0000
@@ -196,23 +196,19 @@
 _stack_cache = {}
 
 
-def register_filter_stack_map(name, stack_map, fallback=None):
+def register_filter_stack_map(name, stack_map_lookup):
     """Register the filter stacks to use for various preference values.
 
     :param name: the preference/filter-stack name
-    :param stack_map: a dictionary where
-      the keys are preference values to match and
-      the values are the matching stack of filters for each
-    :param fallback: if non-None, a callable that will be
-      invoked if a preference value is found that doesn't
-      match a key in the stack_map. The callable is expected
-      to take the value as a parameter and either return
-      the matching stack of filters or None if none.
+    :param stack_map_lookup: a callable where
+      the parameter is the preference value to match and
+      the result is the matching stack of filters to use,
+      or None if none.
     """
     if name in _filter_stacks_registry:
         raise errors.BzrError(
             "filter stack for %s already installed" % name)
-    _filter_stacks_registry.register(name, (stack_map, fallback))
+    _filter_stacks_registry.register(name, stack_map_lookup)
 
 
 def lazy_register_filter_stack_map(name, module_name, member_name):
@@ -220,7 +216,7 @@
 
     :param name: the preference/filter-stack name
     :param module_name: The python path to the module of the filter stack map.
-    :param member_name: The name of the (filter stack map, fallback) tuple
+    :param member_name: The name of the stack_map_lookup callable
       in the module.
     """
     if name in _filter_stacks_registry:
@@ -252,13 +248,11 @@
     stack = []
     for k, v in preferences:
         try:
-            stacks_by_values, fallback = _filter_stacks_registry.get(k)
+            stack_map_lookup = _filter_stacks_registry.get(k)
         except KeyError:
             # Some preferences may not have associated filters
             continue
-        items = stacks_by_values.get(v)
-        if items is None and fallback is not None:
-            items = fallback(v)
+        items = stack_map_lookup(v)
         if items:
             stack.extend(items)
     _stack_cache[preferences] = stack

=== modified file 'bzrlib/filters/eol.py'
--- a/bzrlib/filters/eol.py	2009-04-01 04:51:46 +0000
+++ b/bzrlib/filters/eol.py	2009-04-06 11:06:01 +0000
@@ -64,4 +64,10 @@
     'crlf-with-crlf-in-repo':
         [filters.ContentFilter(_to_crlf_converter, _to_crlf_converter)],
     }
-filters.register_filter_stack_map('eol', _eol_filter_stack_map)
+
+
+def _eol_filter_stack_map_lookup(key):
+    return _eol_filter_stack_map.get(key)
+
+
+filters.register_filter_stack_map('eol', _eol_filter_stack_map_lookup)

=== modified file 'bzrlib/tests/test_filters.py'
--- a/bzrlib/tests/test_filters.py	2009-03-28 02:49:14 +0000
+++ b/bzrlib/tests/test_filters.py	2009-04-06 11:06:01 +0000
@@ -111,9 +111,10 @@
 
 class TestFilterStackMaps(TestCase):
 
-    def _register_map(self, pref, stk1, stk2, fallback=None):
-        register_filter_stack_map(pref, {'v1': stk1, 'v2': stk2},
-            fallback=fallback)
+    def _register_map(self, pref, stk1, stk2):
+        def stk_lookup(key):
+            return {'v1': stk1, 'v2': stk2}.get(key)
+        register_filter_stack_map(pref, stk_lookup)
 
     def test_filter_stack_maps(self):
         # Save the current registry
@@ -158,25 +159,3 @@
         finally:
             # Restore the real registry
             filters._reset_registry(original_registry)
-
-    def test_filter_stack_map_with_callback(self):
-        a_stack = [ContentFilter('b', 'c')]
-        d_stack = [ContentFilter('d', 'D')]
-        z_stack = [ContentFilter('y', 'x'), ContentFilter('w', 'v')]
-        def my_fallback(value):
-            if value == 'v3':
-                return d_stack
-            else:
-                return None
-        # Save the current registry
-        original_registry = filters._reset_registry()
-        try:
-            # Test registration
-            self._register_map('foo', a_stack, z_stack, my_fallback)
-            self.assertEqual(['foo'], _get_registered_names())
-            # Test lookup of a value only handled by the fallback
-            prefs = (('foo','v3'),)
-            self.assertEqual(d_stack, _get_filter_stack_for(prefs))
-        finally:
-            # Restore the real registry
-            filters._reset_registry(original_registry)




More information about the bazaar-commits mailing list