2to3 -f raise
[sfa.git] / sfa / storage / parameter.py
1 #
2 # Shared type definitions
3 #
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
6 #
7
8 from types import NoneType, IntType, LongType, FloatType, StringTypes, DictType, TupleType, ListType
9 from sfa.util.faults import SfaAPIError
10
11 class Parameter:
12     """
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).
16     """
17
18     def __init__(self, type, doc = "",
19                  min = None, max = None,
20                  optional = None,
21                  ro = False,
22                  nullok = False):
23         # Basic type of the parameter. Must be a builtin type
24         # that can be marshalled by XML-RPC.
25         self.type = type
26
27         # Documentation string for the parameter
28         self.doc = doc
29
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.
33         self.min = min
34         self.max = max
35
36         # Whether the sub-parameter is optional or not. If None,
37         # unknown whether it is optional.
38         self.optional = optional
39
40         # Whether the DB field is read-only.
41         self.ro = ro
42
43         # Whether the DB field can be NULL.
44         self.nullok = nullok
45
46     def type(self):
47         return self.type
48
49     def __repr__(self):
50         return repr(self.type)
51
52 class Mixed(tuple):
53     """
54     A list (technically, a tuple) of types. Use in accepts and returns
55     to document method parameters that may return mixed types.
56     """
57
58     def __new__(cls, *types):
59         return tuple.__new__(cls, types)
60
61
62 def python_type(arg):
63     """
64     Returns the Python type of the specified argument, which may be a
65     Python type, a typed value, or a Parameter.
66     """
67
68     if isinstance(arg, Parameter):
69         arg = arg.type
70
71     if isinstance(arg, type):
72         return arg
73     else:
74         return type(arg)
75
76 def xmlrpc_type(arg):
77     """
78     Returns the XML-RPC type of the specified argument, which may be a
79     Python type, a typed value, or a Parameter.
80     """
81
82     arg_type = python_type(arg)
83
84     if arg_type == NoneType:
85         return "nil"
86     elif arg_type == IntType or arg_type == LongType:
87         return "int"
88     elif arg_type == bool:
89         return "boolean"
90     elif arg_type == FloatType:
91         return "double"
92     elif arg_type in StringTypes:
93         return "string"
94     elif arg_type == ListType or arg_type == TupleType:
95         return "array"
96     elif arg_type == DictType:
97         return "struct"
98     elif arg_type == Mixed:
99         # Not really an XML-RPC type but return "mixed" for
100         # documentation purposes.
101         return "mixed"
102     else:
103         raise SfaAPIError("XML-RPC cannot marshal %s objects" % arg_type)