2
# Copyright (C) 2000-2005 by Yasushi Saito (yasushi.saito@gmail.com)
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
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
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)
22
typeval, default_value, docstring = keys[attr][0:3]
23
val = getattr(obj, attr)
24
if val == None or typeval == pychart_types.AnyType:
26
elif isinstance(typeval, types.FunctionType):
27
# user-defined check procedure
28
error = apply(typeval, (val,))
30
raise Exception, "%s: %s for attribute '%s', but got '%s'" % (obj, error, attr, val)
33
if isinstance(val, typeval):
36
raise Exception, "%s: Expecting type %s, but got %s (attr=%s, %s)" % (obj, typeval, val, attr, keys[attr])
39
raise Exception, "%s: attribute '%s' expects type %s but found %s" % (obj, attr, typeval, val)
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])
50
# 3: defaultValue document (optional)
52
validAttrs[attr] = tuple
57
for attr, tuple in keys.items():
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)
65
for key, val in args.items():
66
setattr(self, key, val)
67
_check_attr_types(self, keys)
69
def __init__(self, **args):
73
_check_attr_types(self, self.keys)