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