src/reportlab/lib/attrmap.py
author rgbecker
Wed, 03 Sep 2008 16:10:51 +0000
changeset 2964 32352db0d71e
parent 2547 reportlab/lib/attrmap.py@9d13685212f2
child 3029 eded59f94021
permissions -rw-r--r--
reportlab-2.2: second stage of major re-org
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2027
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2004
946
0bda695b09ce Initial version
rgbecker
parents:
diff changeset
     2
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2027
diff changeset
     3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/lib/attrmap.py
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2027
diff changeset
     4
__version__=''' $Id$ '''
946
0bda695b09ce Initial version
rgbecker
parents:
diff changeset
     5
from UserDict import UserDict
2547
9d13685212f2 datacharts synchronised
andy
parents: 2489
diff changeset
     6
from reportlab.lib.validators import isAnything, _SequenceTypes, DerivedValue
2027
6b47f09c8d1e Add check for shapeChecking to validateSetattr
rgbecker
parents: 2025
diff changeset
     7
from reportlab import rl_config
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
2489
f0560094aae9 attrmap: fix initialization of _initial
rgbecker
parents: 2332
diff changeset
    25
        self._initial = initial
1677
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)'''
2027
6b47f09c8d1e Add check for shapeChecking to validateSetattr
rgbecker
parents: 2025
diff changeset
    74
    if rl_config.shapeChecking:
6b47f09c8d1e Add check for shapeChecking to validateSetattr
rgbecker
parents: 2025
diff changeset
    75
        map = obj._attrMap
6b47f09c8d1e Add check for shapeChecking to validateSetattr
rgbecker
parents: 2025
diff changeset
    76
        if map and name[0]!= '_':
2547
9d13685212f2 datacharts synchronised
andy
parents: 2489
diff changeset
    77
            #we always allow the inherited values; they cannot
9d13685212f2 datacharts synchronised
andy
parents: 2489
diff changeset
    78
            #be checked until draw time.
9d13685212f2 datacharts synchronised
andy
parents: 2489
diff changeset
    79
            if isinstance(value, DerivedValue):
9d13685212f2 datacharts synchronised
andy
parents: 2489
diff changeset
    80
                #let it through
9d13685212f2 datacharts synchronised
andy
parents: 2489
diff changeset
    81
                pass
9d13685212f2 datacharts synchronised
andy
parents: 2489
diff changeset
    82
            else:            
9d13685212f2 datacharts synchronised
andy
parents: 2489
diff changeset
    83
                try:
9d13685212f2 datacharts synchronised
andy
parents: 2489
diff changeset
    84
                    validate = map[name].validate
9d13685212f2 datacharts synchronised
andy
parents: 2489
diff changeset
    85
                    if not validate(value):
9d13685212f2 datacharts synchronised
andy
parents: 2489
diff changeset
    86
                        raise AttributeError, "Illegal assignment of '%s' to '%s' in class %s" % (value, name, obj.__class__.__name__)
9d13685212f2 datacharts synchronised
andy
parents: 2489
diff changeset
    87
                except KeyError:
9d13685212f2 datacharts synchronised
andy
parents: 2489
diff changeset
    88
                    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
    89
    obj.__dict__[name] = value
1549
df0706b21a94 Fixed to work with 2.2
rgbecker
parents: 948
diff changeset
    90
df0706b21a94 Fixed to work with 2.2
rgbecker
parents: 948
diff changeset
    91
def _privateAttrMap(obj,ret=0):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    92
    '''clone obj._attrMap if required'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    93
    A = obj._attrMap
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    94
    oA = getattr(obj.__class__,'_attrMap',None)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    95
    if ret:
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
            return A.clone(), oA
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    98
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    99
            return A, None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   100
    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   101
        if oA is A:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   102
            obj._attrMap = A.clone()
1549
df0706b21a94 Fixed to work with 2.2
rgbecker
parents: 948
diff changeset
   103
df0706b21a94 Fixed to work with 2.2
rgbecker
parents: 948
diff changeset
   104
def _findObjectAndAttr(src, P):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   105
    '''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
   106
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   107
    P = string.split(P, '.')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   108
    if len(P) == 0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   109
        return None, None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   110
    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   111
        for p in P[0:-1]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   112
            src = getattr(src, p)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   113
        return src, P[-1]
1549
df0706b21a94 Fixed to work with 2.2
rgbecker
parents: 948
diff changeset
   114
2025
22cff3d2ab78 Change name of hook func
rgbecker
parents: 1693
diff changeset
   115
def hook__setattr__(obj):
22cff3d2ab78 Change name of hook func
rgbecker
parents: 1693
diff changeset
   116
    if not hasattr(obj,'__attrproxy__'):
22cff3d2ab78 Change name of hook func
rgbecker
parents: 1693
diff changeset
   117
        C = obj.__class__
22cff3d2ab78 Change name of hook func
rgbecker
parents: 1693
diff changeset
   118
        import new
22cff3d2ab78 Change name of hook func
rgbecker
parents: 1693
diff changeset
   119
        obj.__class__=new.classobj(C.__name__,(C,)+C.__bases__,
22cff3d2ab78 Change name of hook func
rgbecker
parents: 1693
diff changeset
   120
            {'__attrproxy__':[],
22cff3d2ab78 Change name of hook func
rgbecker
parents: 1693
diff changeset
   121
            '__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
   122
1a71e3509e39 Allow for multiple BASEs
rgbecker
parents: 1683
diff changeset
   123
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
   124
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   125
    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
   126
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   127
    #sanity
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   128
    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
   129
    A, oA = _privateAttrMap(src,1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   130
    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
   131
    D = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   132
    DV = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   133
    for d in dst:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   134
        if type(d) in _SequenceTypes:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   135
            d, e = d[0], d[1:]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   136
        obj, attr = _findObjectAndAttr(src,d)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   137
        if obj:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   138
            dA = getattr(obj,'_attrMap',None)