X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FParameter.py;h=6268fcecd06d41ab6cdae7ba183b714689cc7409;hb=e81fb1f3d2c89f2e3951b3c0df975e174ed6545d;hp=5dab3639b1e65fe6dca2876b34b91eb1d611f4b2;hpb=49545a0895dd600f3323872e162d6ebbb483e3b1;p=plcapi.git diff --git a/PLC/Parameter.py b/PLC/Parameter.py index 5dab363..6268fce 100644 --- a/PLC/Parameter.py +++ b/PLC/Parameter.py @@ -4,8 +4,9 @@ # Mark Huang # Copyright (C) 2006 The Trustees of Princeton University # -# $Id: Parameter.py,v 1.1 2006/09/06 15:36:07 mlhuang Exp $ -# + +from types import * +from PLC.Faults import * class Parameter: """ @@ -14,9 +15,36 @@ class Parameter: sub-parameters (i.e., dict fields). """ - def __init__(self, type, doc = "", min = None, max = None, optional = True, default = None): - (self.type, self.doc, self.min, self.max, self.optional, self.default) = \ - (type, doc, min, max, optional, default) + 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 + + # Basic value checking. For numeric types, the minimum and + # maximum possible values, inclusive. For string types, the + # minimum and maximum possible UTF-8 encoded byte lengths. + self.min = min + self.max = max + + # Whether the sub-parameter is optional or not. If None, + # unknown whether it is optional. + self.optional = optional + + # 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) @@ -29,3 +57,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