# Mark Huang <mlhuang@cs.princeton.edu>
# Copyright (C) 2006 The Trustees of Princeton University
#
-# $Id: Parameter.py,v 1.2 2006/09/08 19:45:04 mlhuang Exp $
-#
+
+from PLC.Faults import *
class Parameter:
"""
sub-parameters (i.e., dict fields).
"""
- def __init__(self, type, doc = "",
- min = None, max = None,
- optional = True, default = None,
- ro = 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
self.min = min
self.max = max
- # Whether the sub-parameter is optional or not. If optional,
- # the default for the sub-parameter if not specified.
+ # Whether the sub-parameter is optional or not. If None,
+ # unknown whether it is optional.
self.optional = optional
- self.default = default
# 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)
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)