1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2000-2005 by Yasushi Saito (yasushi.saito@gmail.com)
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
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
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)
23 typeval, default_value, docstring = keys[attr][0:3]
24 val = getattr(obj, attr)
25 if val == None or typeval == pychart_types.AnyType:
27 elif isinstance(typeval, types.FunctionType):
28 # user-defined check procedure
29 error = apply(typeval, (val,))
31 raise Exception, "%s: %s for attribute '%s', but got '%s'" % (obj, error, attr, val)
34 if isinstance(val, typeval):
37 raise Exception, "%s: Expecting type %s, but got %s (attr=%s, %s)" % (obj, typeval, val, attr, keys[attr])
40 raise Exception, "%s: attribute '%s' expects type %s but found %s" % (obj, attr, typeval, val)
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])
51 # 3: defaultValue document (optional)
53 validAttrs[attr] = tuple
58 for attr, tuple in keys.items():
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)
66 for key, val in args.items():
67 setattr(self, key, val)
68 _check_attr_types(self, keys)
70 def __init__(self, **args):
74 _check_attr_types(self, self.keys)