Setting tag plcapi-7.0-0
[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 PLC.Faults import *
9
10 class Parameter:
11     """
12     Typed value wrapper. Use in accepts and returns to document method
13     parameters. Set the optional and default attributes for
14     sub-parameters (i.e., dict fields).
15     """
16
17     def __init__(self, typeval, doc="",
18                  min=None, max=None,
19                  optional=None,
20                  ro=False,
21                  nullok=False):
22         # Basic type of the parameter. Must be a builtin type
23         # that can be marshalled by XML-RPC.
24         self.type = typeval
25
26         # Documentation string for the parameter
27         self.doc = doc
28
29         # Basic value checking. For numeric types, the minimum and
30         # maximum possible values, inclusive. For string types, the
31         # minimum and maximum possible UTF-8 encoded byte lengths.
32         self.min = min
33         self.max = max
34
35         # Whether the sub-parameter is optional or not. If None,
36         # unknown whether it is optional.
37         self.optional = optional
38
39         # Whether the DB field is read-only.
40         self.ro = ro
41
42         # Whether the DB field can be NULL.
43         self.nullok = nullok
44
45     def type(self):
46         return self.type
47
48     def __repr__(self):
49         return repr(self.type)
50
51 class Mixed(tuple):
52     """
53     A list (technically, a tuple) of types. Use in accepts and returns
54     to document method parameters that may return mixed types.
55     """
56
57     def __new__(cls, *types):
58         return tuple.__new__(cls, types)
59
60 def python_type(arg):
61     """
62     Returns the Python type of the specified argument, which may be a
63     Python type, a typed value, or a Parameter.
64     """
65
66     if isinstance(arg, Parameter):
67         arg = arg.type
68
69     if isinstance(arg, type):
70         return arg
71     else:
72         return type(arg)
73
74 def xmlrpc_type(arg):
75     """
76     Returns the XML-RPC type of the specified argument, which may be a
77     Python type, a typed value, or a Parameter.
78     """
79
80     arg_type = python_type(arg)
81
82     if arg_type is type(None):
83         return "nil"
84     elif arg_type is int:
85         return "int"
86     elif arg_type is bool:
87         return "boolean"
88     elif arg_type is float:
89         return "double"
90     elif arg_type is str:
91         return "string"
92     elif arg_type in (list, tuple):
93         return "array"
94     elif arg_type is dict:
95         return "struct"
96     elif arg_type == Mixed:
97         # Not really an XML-RPC type but return "mixed" for
98         # documentation purposes.
99         return "mixed"
100     else:
101         raise PLCAPIError("XML-RPC cannot marshal %s objects" % arg_type)