create() calls get_table()
[plcapi.git] / PLC / 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 *
9 from PLC.Faults import *
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, typeval, doc = "",
19                  min = None, max = None,
20                  optional = None,
21                  ro = False,
22                  nullok = False, 
23                  primary_key = False, 
24                  indexed = False,
25                  joined=False):
26         # Basic type of the parameter. Must be a builtin type
27         # that can be marshalled by XML-RPC.
28         self.type = typeval
29
30         # Documentation string for the parameter
31         self.doc = doc
32
33         # Basic value checking. For numeric types, the minimum and
34         # maximum possible values, inclusive. For string types, the
35         # minimum and maximum possible UTF-8 encoded byte lengths.
36         self.min = min
37         self.max = max
38
39         # Whether the sub-parameter is optional or not. If None,
40         # unknown whether it is optional.
41         self.optional = optional
42
43         # Whether the DB field is read-only.
44         self.ro = ro
45
46         # Whether the DB field can be NULL.
47         self.nullok = nullok
48
49         self.primary_key = primary_key
50
51         # Whether the DB field is indexed
52         self.indexed = indexed
53         
54         # Whether the DB field lives in another table  
55         self.joined = joined
56
57     def type(self):
58         return self.type
59
60     def __repr__(self):
61         return repr(self.type)
62
63 class Mixed(tuple):
64     """
65     A list (technically, a tuple) of types. Use in accepts and returns
66     to document method parameters that may return mixed types.
67     """
68
69     def __new__(cls, *types):
70         return tuple.__new__(cls, types)
71
72 def python_type(arg):
73     """
74     Returns the Python type of the specified argument, which may be a
75     Python type, a typed value, or a Parameter.
76     """
77
78     if isinstance(arg, Parameter):
79         arg = arg.type
80
81     if isinstance(arg, type):
82         return arg
83     else:
84         return type(arg)
85
86 def xmlrpc_type(arg):
87     """
88     Returns the XML-RPC type of the specified argument, which may be a
89     Python type, a typed value, or a Parameter.
90     """
91
92     arg_type = python_type(arg)
93
94     if arg_type == NoneType:
95         return "nil"
96     elif arg_type == IntType or arg_type == LongType:
97         return "int"
98     elif arg_type == bool:
99         return "boolean"
100     elif arg_type == FloatType:
101         return "double"
102     elif arg_type in StringTypes:
103         return "string"
104     elif arg_type == ListType or arg_type == TupleType:
105         return "array"
106     elif arg_type == DictType:
107         return "struct"
108     elif arg_type == Mixed:
109         # Not really an XML-RPC type but return "mixed" for
110         # documentation purposes.
111         return "mixed"
112     else:
113         raise PLCAPIError, "XML-RPC cannot marshal %s objects" % arg_type