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