2 # Shared type definitions
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
9 from PLC.Faults import *
13 Typed value wrapper. Use in accepts and returns to document method
14 parameters. Set the optional and default attributes for
15 sub-parameters (i.e., dict fields).
18 def __init__(self, typeval, doc = "",
19 min = None, max = None,
23 # Basic type of the parameter. Must be a builtin type
24 # that can be marshalled by XML-RPC.
27 # Documentation string for the parameter
30 # Basic value checking. For numeric types, the minimum and
31 # maximum possible values, inclusive. For string types, the
32 # minimum and maximum possible UTF-8 encoded byte lengths.
36 # Whether the sub-parameter is optional or not. If None,
37 # unknown whether it is optional.
38 self.optional = optional
40 # Whether the DB field is read-only.
43 # Whether the DB field can be NULL.
50 return repr(self.type)
54 A list (technically, a tuple) of types. Use in accepts and returns
55 to document method parameters that may return mixed types.
58 def __new__(cls, *types):
59 return tuple.__new__(cls, types)
63 Returns the Python type of the specified argument, which may be a
64 Python type, a typed value, or a Parameter.
67 if isinstance(arg, Parameter):
70 if isinstance(arg, type):
77 Returns the XML-RPC type of the specified argument, which may be a
78 Python type, a typed value, or a Parameter.
81 arg_type = python_type(arg)
83 if arg_type == NoneType:
85 elif arg_type == IntType or arg_type == LongType:
87 elif arg_type == bool:
89 elif arg_type == FloatType:
91 elif arg_type in StringTypes:
93 elif arg_type == ListType or arg_type == TupleType:
95 elif arg_type == DictType:
97 elif arg_type == Mixed:
98 # Not really an XML-RPC type but return "mixed" for
99 # documentation purposes.
102 raise PLCAPIError, "XML-RPC cannot marshal %s objects" % arg_type