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

« back to all changes in this revision

Viewing changes to bin/pychart/chart_object.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
#
 
2
# Copyright (C) 2000-2005 by Yasushi Saito (yasushi.saito@gmail.com)
 
3
 
4
# Jockey is free software; you can redistribute it and/or modify it
 
5
# under the terms of the GNU General Public License as published by the
 
6
# Free Software Foundation; either version 2, or (at your option) any
 
7
# later version.
 
8
#
 
9
# Jockey is distributed in the hope that it will be useful, but WITHOUT
 
10
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
11
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
12
# for more details.
 
13
#
 
14
import pychart_types
 
15
import types
 
16
 
 
17
def _check_attr_types(obj, keys):
 
18
    for attr in obj.__dict__.keys():
 
19
        if not keys.has_key(attr):
 
20
            raise Exception, "%s: unknown attribute '%s'" % (obj, attr)
 
21
        
 
22
        typeval, default_value, docstring = keys[attr][0:3]
 
23
        val = getattr(obj, attr)
 
24
        if val == None or typeval == pychart_types.AnyType:
 
25
            pass
 
26
        elif isinstance(typeval, types.FunctionType):
 
27
            # user-defined check procedure
 
28
            error = apply(typeval, (val,))
 
29
            if error != None:
 
30
                raise Exception, "%s: %s for attribute '%s', but got '%s'" % (obj, error, attr, val)
 
31
        elif 1:
 
32
            try:
 
33
                if isinstance(val, typeval):
 
34
                    pass
 
35
            except:
 
36
                raise Exception, "%s: Expecting type %s, but got %s (attr=%s, %s)"  % (obj, typeval, val, attr, keys[attr])
 
37
 
 
38
        else:
 
39
            raise Exception, "%s: attribute '%s' expects type %s but found %s" % (obj, attr, typeval, val)
 
40
 
 
41
def set_defaults(cls, **dict):
 
42
    validAttrs = getattr(cls, "keys")
 
43
    for attr, val in dict.items():
 
44
        if not validAttrs.has_key(attr):
 
45
            raise Exception, "%s: unknown attribute %s." % (cls, attr)
 
46
        tuple = list(validAttrs[attr])
 
47
        # 0 : type
 
48
        # 1: defaultValue
 
49
        # 2: document
 
50
        # 3: defaultValue document (optional)
 
51
        tuple[1] = val
 
52
        validAttrs[attr] = tuple
 
53
        
 
54
class T(object):
 
55
    def init(self, args):
 
56
        keys = self.keys
 
57
        for attr, tuple in keys.items():
 
58
            defaultVal = tuple[1]
 
59
            if isinstance(defaultVal, types.FunctionType):
 
60
                # if the value is procedure, use the result of the proc call
 
61
                # as the default value
 
62
                defaultVal = apply(defaultVal, ())
 
63
            setattr(self, attr, defaultVal)
 
64
            
 
65
        for key, val in args.items():
 
66
            setattr(self, key, val)
 
67
        _check_attr_types(self, keys)
 
68
        
 
69
    def __init__(self, **args):
 
70
        self.init(args)
 
71
 
 
72
    def type_check(self):
 
73
        _check_attr_types(self, self.keys)