X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FParameter.py;h=3f0d42a1c1409f223ce6652185e4218795d48ea0;hb=fe81b2c91b436b1882f63023413c7f51b29538ed;hp=9ae648692ef6bef6da70af201b551de0b67d34f4;hpb=bcc4d54cff6df0e6298620444a2bc75b1e39c909;p=plcapi.git diff --git a/PLC/Parameter.py b/PLC/Parameter.py index 9ae6486..3f0d42a 100644 --- a/PLC/Parameter.py +++ b/PLC/Parameter.py @@ -4,9 +4,13 @@ # Mark Huang # Copyright (C) 2006 The Trustees of Princeton University # -# $Id: Parameter.py,v 1.3 2006/10/02 18:32:31 mlhuang Exp $ +# $Id$ +# $URL$ # +from types import * +from PLC.Faults import * + class Parameter: """ Typed value wrapper. Use in accepts and returns to document method @@ -14,12 +18,14 @@ class Parameter: sub-parameters (i.e., dict fields). """ - def __init__(self, type, doc = "", + def __init__(self, typeval, doc = "", min = None, max = None, optional = None, - ro = False): - # Basic type of the parameter. May be a builtin type or Mixed. - self.type = type + ro = False, + nullok = False): + # Basic type of the parameter. Must be a builtin type + # that can be marshalled by XML-RPC. + self.type = typeval # Documentation string for the parameter self.doc = doc @@ -37,6 +43,12 @@ class Parameter: # Whether the DB field is read-only. self.ro = ro + # Whether the DB field can be NULL. + self.nullok = nullok + + def type(self): + return self.type + def __repr__(self): return repr(self.type) @@ -48,3 +60,46 @@ class Mixed(tuple): def __new__(cls, *types): return tuple.__new__(cls, types) + +def python_type(arg): + """ + Returns the Python type of the specified argument, which may be a + Python type, a typed value, or a Parameter. + """ + + if isinstance(arg, Parameter): + arg = arg.type + + if isinstance(arg, type): + return arg + else: + return type(arg) + +def xmlrpc_type(arg): + """ + Returns the XML-RPC type of the specified argument, which may be a + Python type, a typed value, or a Parameter. + """ + + arg_type = python_type(arg) + + if arg_type == NoneType: + return "nil" + elif arg_type == IntType or arg_type == LongType: + return "int" + elif arg_type == bool: + return "boolean" + elif arg_type == FloatType: + return "double" + elif arg_type in StringTypes: + return "string" + elif arg_type == ListType or arg_type == TupleType: + return "array" + elif arg_type == DictType: + return "struct" + elif arg_type == Mixed: + # Not really an XML-RPC type but return "mixed" for + # documentation purposes. + return "mixed" + else: + raise PLCAPIError, "XML-RPC cannot marshal %s objects" % arg_type