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