~openerp-groupes/openobject-server/6.0-fix-setup-windows

« back to all changes in this revision

Viewing changes to bin/reportlab/lib/attrmap.py

  • Committer: pinky
  • Date: 2006-12-07 13:41:40 UTC
  • Revision ID: pinky-3f10ee12cea3c4c75cef44ab04ad33ef47432907
New trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#Copyright ReportLab Europe Ltd. 2000-2004
 
2
#see license.txt for license details
 
3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/lib/attrmap.py
 
4
__version__=''' $Id$ '''
 
5
from UserDict import UserDict
 
6
from reportlab.lib.validators import isAnything, _SequenceTypes
 
7
from reportlab import rl_config
 
8
 
 
9
class CallableValue:
 
10
    '''a class to allow callable initial values'''
 
11
    def __init__(self,func,*args,**kw):
 
12
        #assert iscallable(func)
 
13
        self.func = func
 
14
        self.args = args
 
15
        self.kw = kw
 
16
 
 
17
    def __call__(self):
 
18
        return apply(self.func,self.args,self.kw)
 
19
 
 
20
class AttrMapValue:
 
21
    '''Simple multi-value holder for attribute maps'''
 
22
    def __init__(self,validate=None,desc=None,initial=None, **kw):
 
23
        self.validate = validate or isAnything
 
24
        self.desc = desc
 
25
        self.initial = initial
 
26
        for k,v in kw.items():
 
27
            setattr(self,k,v)
 
28
 
 
29
    def __getattr__(self,name):
 
30
        #hack to allow callable initial values
 
31
        if name=='initial':
 
32
            if isinstance(self._initial,CallableValue): return self._initial()
 
33
            return self._initial
 
34
        elif name=='hidden':
 
35
            return 0
 
36
        raise AttributeError, name
 
37
 
 
38
class AttrMap(UserDict):
 
39
    def __init__(self,BASE=None,UNWANTED=[],**kw):
 
40
        data = {}
 
41
        if BASE:
 
42
            if isinstance(BASE,AttrMap):
 
43
                data = BASE.data                        #they used BASECLASS._attrMap
 
44
            else:
 
45
                if type(BASE) not in (type(()),type([])): BASE = (BASE,)
 
46
                for B in BASE:
 
47
                    if hasattr(B,'_attrMap'):
 
48
                        data.update(getattr(B._attrMap,'data',{}))
 
49
                    else:
 
50
                        raise ValueError, 'BASE=%s has wrong kind of value' % str(B)
 
51
 
 
52
        UserDict.__init__(self,data)
 
53
        self.remove(UNWANTED)
 
54
        self.data.update(kw)
 
55
 
 
56
    def update(self,kw):
 
57
        if isinstance(kw,AttrMap): kw = kw.data
 
58
        self.data.update(kw)
 
59
 
 
60
    def remove(self,unwanted):
 
61
        for k in unwanted:
 
62
            try:
 
63
                del self[k]
 
64
            except KeyError:
 
65
                pass
 
66
 
 
67
    def clone(self,UNWANTED=[],**kw):
 
68
        c = AttrMap(BASE=self,UNWANTED=UNWANTED)
 
69
        c.update(kw)
 
70
        return c
 
71
 
 
72
def validateSetattr(obj,name,value):
 
73
    '''validate setattr(obj,name,value)'''
 
74
    if rl_config.shapeChecking:
 
75
        map = obj._attrMap
 
76
        if map and name[0]!= '_':
 
77
            try:
 
78
                validate = map[name].validate
 
79
                if not validate(value):
 
80
                    raise AttributeError, "Illegal assignment of '%s' to '%s' in class %s" % (value, name, obj.__class__.__name__)
 
81
            except KeyError:
 
82
                raise AttributeError, "Illegal attribute '%s' in class %s" % (name, obj.__class__.__name__)
 
83
    obj.__dict__[name] = value
 
84
 
 
85
def _privateAttrMap(obj,ret=0):
 
86
    '''clone obj._attrMap if required'''
 
87
    A = obj._attrMap
 
88
    oA = getattr(obj.__class__,'_attrMap',None)
 
89
    if ret:
 
90
        if oA is A:
 
91
            return A.clone(), oA
 
92
        else:
 
93
            return A, None
 
94
    else:
 
95
        if oA is A:
 
96
            obj._attrMap = A.clone()
 
97
 
 
98
def _findObjectAndAttr(src, P):
 
99
    '''Locate the object src.P for P a string, return parent and name of attribute
 
100
    '''
 
101
    P = string.split(P, '.')
 
102
    if len(P) == 0:
 
103
        return None, None
 
104
    else:
 
105
        for p in P[0:-1]:
 
106
            src = getattr(src, p)
 
107
        return src, P[-1]
 
108
 
 
109
def hook__setattr__(obj):
 
110
    if not hasattr(obj,'__attrproxy__'):
 
111
        C = obj.__class__
 
112
        import new
 
113
        obj.__class__=new.classobj(C.__name__,(C,)+C.__bases__,
 
114
            {'__attrproxy__':[],
 
115
            '__setattr__':lambda self,k,v,osa=getattr(obj,'__setattr__',None),hook=hook: hook(self,k,v,osa)})
 
116
 
 
117
def addProxyAttribute(src,name,validate=None,desc=None,initial=None,dst=None):
 
118
    '''
 
119
    Add a proxy attribute 'name' to src with targets dst
 
120
    '''
 
121
    #sanity
 
122
    assert hasattr(src,'_attrMap'), 'src object has no _attrMap'
 
123
    A, oA = _privateAttrMap(src,1)
 
124
    if type(dst) not in _SequenceTypes: dst = dst,
 
125
    D = []
 
126
    DV = []
 
127
    for d in dst:
 
128
        if type(d) in _SequenceTypes:
 
129
            d, e = d[0], d[1:]
 
130
        obj, attr = _findObjectAndAttr(src,d)
 
131
        if obj:
 
132
            dA = getattr(obj,'_attrMap',None)