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