X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FParameter.py;h=1144ec8f198d617704f1dc8c844ee30192a9e2fb;hb=c3fc031363ac794e6b1245c6ed1a05329cba69c9;hp=53c32196487ca3f41292927193e11a58ae6edb3f;hpb=bc48d5148c2d0b65f90e393184a213dc202ecc70;p=plcapi.git diff --git a/PLC/Parameter.py b/PLC/Parameter.py index 53c3219..1144ec8 100644 --- a/PLC/Parameter.py +++ b/PLC/Parameter.py @@ -4,8 +4,8 @@ # Mark Huang # Copyright (C) 2006 The Trustees of Princeton University # -# $Id: Parameter.py,v 1.4 2006/10/25 14:27:12 mlhuang Exp $ -# + +from PLC.Faults import * class Parameter: """ @@ -14,13 +14,14 @@ class Parameter: sub-parameters (i.e., dict fields). """ - def __init__(self, type, doc = "", - min = None, max = None, - optional = None, - ro = False, - nullok = False): - # Basic type of the parameter. May be a builtin type or Mixed. - self.type = type + def __init__(self, typeval, doc="", + min=None, max=None, + optional=None, + 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 @@ -41,6 +42,9 @@ class Parameter: # Whether the DB field can be NULL. self.nullok = nullok + def type(self): + return self.type + def __repr__(self): return repr(self.type) @@ -52,3 +56,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 is type(None): + return "nil" + elif arg_type is int: + return "int" + elif arg_type is bool: + return "boolean" + elif arg_type is float: + return "double" + elif arg_type is str: + return "string" + elif arg_type in (list, tuple): + return "array" + elif arg_type is dict: + 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)