~sidnei/zope3/reduce-deps

« back to all changes in this revision

Viewing changes to src/zc/sourcefactory/mapping.py

  • Committer: Sidnei da Silva
  • Date: 2010-07-05 21:07:01 UTC
  • Revision ID: sidnei.da.silva@canonical.com-20100705210701-zmqhqrbzad1mhzsl
- Reduce deps

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
##############################################################################
2
 
#
3
 
# Copyright (c) 2006-2007 Zope Corporation and Contributors.
4
 
# All Rights Reserved.
5
 
#
6
 
# This software is subject to the provisions of the Zope Public License,
7
 
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8
 
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
 
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
 
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
 
# FOR A PARTICULAR PURPOSE.
12
 
#
13
 
##############################################################################
14
 
"""A source proxy providing a mapping between values
15
 
 
16
 
"""
17
 
__docformat__ = "reStructuredText"
18
 
 
19
 
import zope.interface
20
 
import zope.schema.interfaces
21
 
 
22
 
 
23
 
class ValueMappingSourceContextBinder(object):
24
 
 
25
 
    zope.interface.implements(zope.schema.interfaces.IContextSourceBinder)
26
 
 
27
 
    def __init__(self, base, map):
28
 
        self.base = base
29
 
        self.map = map
30
 
 
31
 
    def __call__(self, context):
32
 
        source = self.base(context)
33
 
        return ValueMappingSource(source, self.map)
34
 
 
35
 
 
36
 
class ValueMappingSource(object):
37
 
 
38
 
    zope.interface.implements(zope.schema.interfaces.IIterableSource)
39
 
 
40
 
    def __init__(self, base, map):
41
 
        self.base = base
42
 
        self._mapping_cache = {}
43
 
        self.map = map
44
 
 
45
 
    def mapReverse(self, mapped_value):
46
 
        if mapped_value in self._mapping_cache:
47
 
            return self._mapping_cache[mapped_value]
48
 
 
49
 
        # Not found in cache, continue to look for the mapped value in
50
 
        # the rest of the iterator
51
 
        if not hasattr(self, '_cache_iterator'):
52
 
            self._cache_iterator = iter(self.base)
53
 
        for original_value in self._cache_iterator:
54
 
            original_mapped_value = self.map(original_value)
55
 
            self._mapping_cache[original_mapped_value] = original_value
56
 
            if mapped_value == original_mapped_value:
57
 
                return original_value
58
 
        raise KeyError(mapped_value)
59
 
 
60
 
    def __contains__(self, value):
61
 
        try:
62
 
            self.mapReverse(value)
63
 
        except KeyError:
64
 
            return False
65
 
        else:
66
 
            return True
67
 
 
68
 
    def __iter__(self):
69
 
        for item in self.base:
70
 
            yield self.map(item)
71
 
 
72
 
    def __len__(self):
73
 
        return len(self.base)
74
 
 
75
 
    def __nonzero__(self):
76
 
        for dummy in self.base:
77
 
            return True
78
 
        return False