2 # Shared type definitions
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
14 from sfa.util.faults import *
18 Typed value wrapper. Use in accepts and returns to document method
19 parameters. Set the optional and default attributes for
20 sub-parameters (i.e., dict fields).
23 def __init__(self, type, doc = "",
24 min = None, max = None,
28 # Basic type of the parameter. Must be a builtin type
29 # that can be marshalled by XML-RPC.
32 # Documentation string for the parameter
35 # Basic value checking. For numeric types, the minimum and
36 # maximum possible values, inclusive. For string types, the
37 # minimum and maximum possible UTF-8 encoded byte lengths.
41 # Whether the sub-parameter is optional or not. If None,
42 # unknown whether it is optional.
43 self.optional = optional
45 # Whether the DB field is read-only.
48 # Whether the DB field can be NULL.
55 return repr(self.type)
59 A list (technically, a tuple) of types. Use in accepts and returns
60 to document method parameters that may return mixed types.
63 def __new__(cls, *types):
64 return tuple.__new__(cls, types)
69 Returns the Python type of the specified argument, which may be a
70 Python type, a typed value, or a Parameter.
73 if isinstance(arg, Parameter):
76 if isinstance(arg, type):
83 Returns the XML-RPC type of the specified argument, which may be a
84 Python type, a typed value, or a Parameter.
87 arg_type = python_type(arg)
89 if arg_type == NoneType:
91 elif arg_type == IntType or arg_type == LongType:
93 elif arg_type == bool:
95 elif arg_type == FloatType:
97 elif arg_type in StringTypes:
99 elif arg_type == ListType or arg_type == TupleType:
101 elif arg_type == DictType:
103 elif arg_type == Mixed:
104 # Not really an XML-RPC type but return "mixed" for
105 # documentation purposes.
108 raise GeniAPIError, "XML-RPC cannot marshal %s objects" % arg_type