reportlab/lib/attrmap.py
author rgbecker
Tue, 26 Aug 2003 17:09:32 +0000
changeset 2027 6b47f09c8d1e
parent 2025 22cff3d2ab78
child 2332 2a7ab4405e18
permissions -rw-r--r--
Add check for shapeChecking to validateSetattr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
946
0bda695b09ce Initial version
rgbecker
parents:
diff changeset
     1
#copyright ReportLab Inc. 2000
0bda695b09ce Initial version
rgbecker
parents:
diff changeset
     2
#see license.txt for license details
0bda695b09ce Initial version
rgbecker
parents:
diff changeset
     3
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/lib/attrmap.py?cvsroot=reportlab
2027
6b47f09c8d1e Add check for shapeChecking to validateSetattr
rgbecker
parents: 2025
diff changeset
     4
#$Header: /tmp/reportlab/reportlab/lib/attrmap.py,v 1.9 2003/08/26 17:09:32 rgbecker Exp $
6b47f09c8d1e Add check for shapeChecking to validateSetattr
rgbecker
parents: 2025
diff changeset
     5
__version__=''' $Id: attrmap.py,v 1.9 2003/08/26 17:09:32 rgbecker Exp $ '''
946
0bda695b09ce Initial version
rgbecker
parents:
diff changeset
     6
from UserDict import UserDict
1549
df0706b21a94 Fixed to work with 2.2
rgbecker
parents: 948
diff changeset
     7
from reportlab.lib.validators import isAnything, _SequenceTypes
2027
6b47f09c8d1e Add check for shapeChecking to validateSetattr
rgbecker
parents: 2025
diff changeset
     8
from reportlab import rl_config
946
0bda695b09ce Initial version
rgbecker
parents:
diff changeset
     9
0bda695b09ce Initial version
rgbecker
parents:
diff changeset
    10
class CallableValue:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    11
    '''a class to allow callable initial values'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    12
    def __init__(self,func,*args,**kw):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    13
        #assert iscallable(func)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    14
        self.func = func
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    15
        self.args = args
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    16
        self.kw = kw
946
0bda695b09ce Initial version
rgbecker
parents:
diff changeset
    17
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    18
    def __call__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    19
        return apply(self.func,self.args,self.kw)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
    20
946
0bda695b09ce Initial version
rgbecker
parents:
diff changeset
    21
class AttrMapValue:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    22
    '''Simple multi-value holder for attribute maps'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    23
    def __init__(self,validate=None,desc=None,initial=None, **kw):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    24
        self.validate = validate or isAnything
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    25
        self.desc = desc
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    26
        self.initial = initial
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    27
        for k,v in kw.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    28
            setattr(self,k,v)
946
0bda695b09ce Initial version
rgbecker
parents:
diff changeset
    29
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    30
    def __getattr__(self,name):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    31
        #hack to allow callable initial values
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    32
        if name=='initial':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    33
            if isinstance(self._initial,CallableValue): return self._initial()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    34
            return self._initial
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    35
        elif name=='hidden':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    36
            return 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    37
        raise AttributeError, name
946
0bda695b09ce Initial version
rgbecker
parents:
diff changeset
    38
0bda695b09ce Initial version
rgbecker
parents:
diff changeset
    39
class AttrMap(UserDict):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    40
    def __init__(self,BASE=None,UNWANTED=[],**kw):
1693
1a71e3509e39 Allow for multiple BASEs
rgbecker
parents: 1683
diff changeset
    41
        data = {}
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    42
        if BASE:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    43
            if isinstance(BASE,AttrMap):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    44
                data = BASE.data                        #they used BASECLASS._attrMap
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    45
            else:
1693
1a71e3509e39 Allow for multiple BASEs
rgbecker
parents: 1683
diff changeset
    46
                if type(BASE) not in (type(()),type([])): BASE = (BASE,)
1a71e3509e39 Allow for multiple BASEs
rgbecker
parents: 1683
diff changeset
    47
                for B in BASE:
1a71e3509e39 Allow for multiple BASEs
rgbecker
parents: 1683
diff changeset
    48
                    if hasattr(B,'_attrMap'):
1a71e3509e39 Allow for multiple BASEs
rgbecker
parents: 1683
diff changeset
    49
                        data.update(getattr(B._attrMap,'data',{}))
1a71e3509e39 Allow for multiple BASEs
rgbecker
parents: 1683
diff changeset
    50
                    else:
1a71e3509e39 Allow for multiple BASEs
rgbecker
parents: 1683
diff changeset
    51
                        raise ValueError, 'BASE=%s has wrong kind of value' % str(B)
946
0bda695b09ce Initial version
rgbecker
parents:
diff changeset
    52
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    53
        UserDict.__init__(self,data)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    54
        self.remove(UNWANTED)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    55
        self.data.update(kw)
948
2531b35adf85 New AttrMap emplacement
rgbecker
parents: 946
diff changeset
    56
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    57
    def update(self,kw):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    58
        if isinstance(kw,AttrMap): kw = kw.data
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    59
        self.data.update(kw)
946
0bda695b09ce Initial version
rgbecker
parents:
diff changeset
    60
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    61
    def remove(self,unwanted):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    62
        for k in unwanted:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    63
            try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    64
                del self[k]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    65
            except KeyError:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    66
                pass
946
0bda695b09ce Initial version
rgbecker
parents:
diff changeset
    67
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    68
    def clone(self,UNWANTED=[],**kw):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    69
        c = AttrMap(BASE=self,UNWANTED=UNWANTED)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    70
        c.update(kw)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    71
        return c
948
2531b35adf85 New AttrMap emplacement
rgbecker
parents: 946
diff changeset
    72
2531b35adf85 New AttrMap emplacement
rgbecker
parents: 946
diff changeset
    73
def validateSetattr(obj,name,value):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    74
    '''validate setattr(obj,name,value)'''
2027
6b47f09c8d1e Add check for shapeChecking to validateSetattr
rgbecker
parents: 2025
diff changeset
    75
    if rl_config.shapeChecking:
6b47f09c8d1e Add check for shapeChecking to validateSetattr
rgbecker
parents: 2025
diff changeset
    76
        map = obj._attrMap
6b47f09c8d1e Add check for shapeChecking to validateSetattr
rgbecker
parents: 2025
diff changeset
    77
        if map and name[0]!= '_':
6b47f09c8d1e Add check for shapeChecking to validateSetattr
rgbecker
parents: 2025
diff changeset
    78
            try:
6b47f09c8d1e Add check for shapeChecking to validateSetattr
rgbecker
parents: 2025
diff changeset
    79
                validate = map[name].validate
6b47f09c8d1e Add check for shapeChecking to validateSetattr
rgbecker
parents: 2025
diff changeset
    80
                if not validate(value):
6b47f09c8d1e Add check for shapeChecking to validateSetattr
rgbecker
parents: 2025
diff changeset
    81
                    raise AttributeError, "Illegal assignment of '%s' to '%s' in class %s" % (value, name, obj.__class__.__name__)
6b47f09c8d1e Add check for shapeChecking to validateSetattr
rgbecker
parents: 2025
diff changeset
    82
            except KeyError:
6b47f09c8d1e Add check for shapeChecking to validateSetattr
rgbecker
parents: 2025
diff changeset
    83
                raise AttributeError, "Illegal attribute '%s' in class %s" % (name, obj.__class__.__name__)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    84
    obj.__dict__[name] = value
1549
df0706b21a94 Fixed to work with 2.2
rgbecker
parents: 948
diff changeset
    85
df0706b21a94 Fixed to work with 2.2
rgbecker
parents: 948
diff changeset
    86
def _privateAttrMap(obj,ret=0):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    87
    '''clone obj._attrMap if required'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    88
    A = obj._attrMap
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    89
    oA = getattr(obj.__class__,'_attrMap',None)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    90
    if ret:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    91
        if oA is A:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    92
            return A.clone(), oA
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    93
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    94
            return A, None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    95
    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    96
        if oA is A:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    97
            obj._attrMap = A.clone()
1549
df0706b21a94 Fixed to work with 2.2
rgbecker
parents: 948
diff changeset
    98
df0706b21a94 Fixed to work with 2.2
rgbecker
parents: 948
diff changeset
    99
def _findObjectAndAttr(src, P):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   100
    '''Locate the object src.P for P a string, return parent and name of attribute
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   101
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   102
    P = string.split(P, '.')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   103
    if len(P) == 0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   104
        return None, None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   105
    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   106
        for p in P[0:-1]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   107
            src = getattr(src, p)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   108
        return src, P[-1]
1549
df0706b21a94 Fixed to work with 2.2
rgbecker
parents: 948
diff changeset
   109
2025
22cff3d2ab78 Change name of hook func
rgbecker
parents: 1693
diff changeset
   110
def hook__setattr__(obj):
22cff3d2ab78 Change name of hook func
rgbecker
parents: 1693
diff changeset
   111
    if not hasattr(obj,'__attrproxy__'):
22cff3d2ab78 Change name of hook func
rgbecker
parents: 1693
diff changeset
   112
        C = obj.__class__
22cff3d2ab78 Change name of hook func
rgbecker
parents: 1693
diff changeset
   113
        import new
22cff3d2ab78 Change name of hook func
rgbecker
parents: 1693
diff changeset
   114
        obj.__class__=new.classobj(C.__name__,(C,)+C.__bases__,
22cff3d2ab78 Change name of hook func
rgbecker
parents: 1693
diff changeset
   115
            {'__attrproxy__':[],
22cff3d2ab78 Change name of hook func
rgbecker
parents: 1693
diff changeset
   116
            '__setattr__':lambda self,k,v,osa=getattr(obj,'__setattr__',None),hook=hook: hook(self,k,v,osa)})
1693
1a71e3509e39 Allow for multiple BASEs
rgbecker
parents: 1683
diff changeset
   117
1a71e3509e39 Allow for multiple BASEs
rgbecker
parents: 1683
diff changeset
   118
def addProxyAttribute(src,name,validate=None,desc=None,initial=None,dst=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   119
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   120
    Add a proxy attribute 'name' to src with targets dst
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   121
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   122
    #sanity
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   123
    assert hasattr(src,'_attrMap'), 'src object has no _attrMap'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   124
    A, oA = _privateAttrMap(src,1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   125
    if type(dst) not in _SequenceTypes: dst = dst,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   126
    D = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   127
    DV = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   128
    for d in dst:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   129
        if type(d) in _SequenceTypes:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   130
            d, e = d[0], d[1:]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   131
        obj, attr = _findObjectAndAttr(src,d)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   132
        if obj:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   133
            dA = getattr(obj,'_attrMap',None)