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