~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/filters/__init__.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-04-06 19:04:43 UTC
  • mfrom: (4257.2.1 ianc-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20090406190443-d2e9bmch7wb8p7u1
simplify filter registration API (Ian Clatworthy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
196
196
_stack_cache = {}
197
197
 
198
198
 
199
 
def register_filter_stack_map(name, stack_map, fallback=None):
 
199
def register_filter_stack_map(name, stack_map_lookup):
200
200
    """Register the filter stacks to use for various preference values.
201
201
 
202
202
    :param name: the preference/filter-stack name
203
 
    :param stack_map: a dictionary where
204
 
      the keys are preference values to match and
205
 
      the values are the matching stack of filters for each
206
 
    :param fallback: if non-None, a callable that will be
207
 
      invoked if a preference value is found that doesn't
208
 
      match a key in the stack_map. The callable is expected
209
 
      to take the value as a parameter and either return
210
 
      the matching stack of filters or None if none.
 
203
    :param stack_map_lookup: a callable where
 
204
      the parameter is the preference value to match and
 
205
      the result is the matching stack of filters to use,
 
206
      or None if none.
211
207
    """
212
208
    if name in _filter_stacks_registry:
213
209
        raise errors.BzrError(
214
210
            "filter stack for %s already installed" % name)
215
 
    _filter_stacks_registry.register(name, (stack_map, fallback))
 
211
    _filter_stacks_registry.register(name, stack_map_lookup)
216
212
 
217
213
 
218
214
def lazy_register_filter_stack_map(name, module_name, member_name):
220
216
 
221
217
    :param name: the preference/filter-stack name
222
218
    :param module_name: The python path to the module of the filter stack map.
223
 
    :param member_name: The name of the (filter stack map, fallback) tuple
 
219
    :param member_name: The name of the stack_map_lookup callable
224
220
      in the module.
225
221
    """
226
222
    if name in _filter_stacks_registry:
252
248
    stack = []
253
249
    for k, v in preferences:
254
250
        try:
255
 
            stacks_by_values, fallback = _filter_stacks_registry.get(k)
 
251
            stack_map_lookup = _filter_stacks_registry.get(k)
256
252
        except KeyError:
257
253
            # Some preferences may not have associated filters
258
254
            continue
259
 
        items = stacks_by_values.get(v)
260
 
        if items is None and fallback is not None:
261
 
            items = fallback(v)
 
255
        items = stack_map_lookup(v)
262
256
        if items:
263
257
            stack.extend(items)
264
258
    _stack_cache[preferences] = stack