2 # Shared type definitions
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
8 from types import NoneType, IntType, LongType, FloatType, DictType, TupleType, ListType
9 from sfa.util.faults import SfaAPIError
14 Typed value wrapper. Use in accepts and returns to document method
15 parameters. Set the optional and default attributes for
16 sub-parameters (i.e., dict fields).
19 def __init__(self, type, doc="",
24 # Basic type of the parameter. Must be a builtin type
25 # that can be marshalled by XML-RPC.
28 # Documentation string for the parameter
31 # Basic value checking. For numeric types, the minimum and
32 # maximum possible values, inclusive. For string types, the
33 # minimum and maximum possible UTF-8 encoded byte lengths.
37 # Whether the sub-parameter is optional or not. If None,
38 # unknown whether it is optional.
39 self.optional = optional
41 # Whether the DB field is read-only.
44 # Whether the DB field can be NULL.
51 return repr(self.type)
56 A list (technically, a tuple) of types. Use in accepts and returns
57 to document method parameters that may return mixed types.
60 def __new__(cls, *types):
61 return tuple.__new__(cls, types)
66 Returns the Python type of the specified argument, which may be a
67 Python type, a typed value, or a Parameter.
70 if isinstance(arg, Parameter):
73 if isinstance(arg, type):
81 Returns the XML-RPC type of the specified argument, which may be a
82 Python type, a typed value, or a Parameter.
85 arg_type = python_type(arg)
87 if arg_type == NoneType:
89 elif arg_type == IntType or arg_type == LongType:
91 elif arg_type == bool:
93 elif arg_type == FloatType:
95 elif issubclass(arg_type, str):
97 elif arg_type == ListType or arg_type == TupleType:
99 elif arg_type == DictType:
101 elif arg_type == Mixed:
102 # Not really an XML-RPC type but return "mixed" for
103 # documentation purposes.
106 raise SfaAPIError("XML-RPC cannot marshal %s objects" % arg_type)