#
-# Base class for all GeniAPI functions
+# Base class for all SfaAPI functions
#
#
-### $Id$
-### $URL$
-
-import xmlrpclib
-from types import *
-import textwrap
-import os
import time
-import pprint
+from types import IntType, LongType, StringTypes
+import textwrap
-from types import StringTypes
+from sfa.util.sfalogging import logger
+from sfa.util.faults import SfaFault, SfaInvalidAPIMethod, SfaInvalidArgumentCount, SfaInvalidArgument
-from sfa.util.faults import *
-from sfa.util.parameter import Parameter, Mixed, python_type, xmlrpc_type
-from sfa.util.auth import Auth
-from sfa.util.debug import profile, log
+from sfa.storage.parameter import Parameter, Mixed, python_type, xmlrpc_type
-# we inherit object because we use new-style classes for legacy methods
-class Method (object):
+class Method:
"""
- Base class for all GeniAPI functions. At a minimum, all GeniAPI
+ Base class for all SfaAPI functions. At a minimum, all SfaAPI
functions must define:
interfaces = [allowed interfaces]
def call(self, *args):
"""
- Method body for all GeniAPI functions. Must override.
-
+ Method body for all SfaAPI functions. Must override.
"""
-
- return True
+ return None
def __init__(self, api):
self.name = self.__class__.__name__
def __call__(self, *args, **kwds):
"""
- Main entry point for all GeniAPI functions. Type checks
+ Main entry point for all SFA API functions. Type checks
arguments, authenticates, and executes call().
"""
start = time.time()
methodname = self.name
if not self.api.interface or self.api.interface not in self.interfaces:
- raise GeniInvalidAPIMethod, methodname, self.api.interface
+ raise SfaInvalidAPIMethod(methodname, self.api.interface)
- # legacy code cannot be type-checked, due to the way Method.args() works
- if not hasattr(self,"skip_typecheck"):
- (min_args, max_args, defaults) = self.args()
-
- # Check that the right number of arguments were passed in
- if len(args) < len(min_args) or len(args) > len(max_args):
- raise GeniInvalidArgumentCount(len(args), len(min_args), len(max_args))
+ (min_args, max_args, defaults) = self.args()
+
+ # Check that the right number of arguments were passed in
+ if len(args) < len(min_args) or len(args) > len(max_args):
+ raise SfaInvalidArgumentCount(len(args), len(min_args), len(max_args))
- for name, value, expected in zip(max_args, args, self.accepts):
- self.type_check(name, value, expected, args)
+ for name, value, expected in zip(max_args, args, self.accepts):
+ self.type_check(name, value, expected, args)
+ logger.debug("method.__call__ [%s] : BEG %s"%(self.api.interface,methodname))
result = self.call(*args, **kwds)
- runtime = time.time() - start
- if self.api.config.GENI_API_DEBUG or hasattr(self, 'message'):
- # XX print to some log file
- # print >> log, "some output"
- pass
+ runtime = time.time() - start
+ logger.debug("method.__call__ [%s] : END %s in %02f s (%s)"%\
+ (self.api.interface,methodname,runtime,getattr(self,'message',"[no-msg]")))
return result
- except GeniFault, fault:
+ except SfaFault as fault:
caller = ""
# Prepend caller and method name to expected faults
fault.faultString = caller + ": " + self.name + ": " + fault.faultString
runtime = time.time() - start
-
- if self.api.config.GENI_API_DEBUG:
- # XX print to some log file
- #print >> log, "Some debugging output"
- pass
+ logger.log_exc("Method %s raised an exception"%self.name)
raise fault
try:
self.type_check(name, value, item, args)
return
- except GeniInvalidArgument, fault:
+ except SfaInvalidArgument as fault:
pass
raise fault
pass
elif not isinstance(value, expected_type):
- raise GeniInvalidArgument("expected %s, got %s" % \
- (xmlrpc_type(expected_type),
- xmlrpc_type(type(value))),
+ raise SfaInvalidArgument("expected %s, got %s" % \
+ (xmlrpc_type(expected_type), xmlrpc_type(type(value))),
name)
# If a minimum or maximum (length, value) has been specified
if expected_type in StringTypes:
if min is not None and \
len(value.encode(self.api.encoding)) < min:
- raise GeniInvalidArgument, "%s must be at least %d bytes long" % (name, min)
+ raise SfaInvalidArgument("%s must be at least %d bytes long" % (name, min))
if max is not None and \
len(value.encode(self.api.encoding)) > max:
- raise GeniInvalidArgument, "%s must be at most %d bytes long" % (name, max)
+ raise SfaInvalidArgument("%s must be at most %d bytes long" % (name, max))
elif expected_type in (list, tuple, set):
if min is not None and len(value) < min:
- raise GeniInvalidArgument, "%s must contain at least %d items" % (name, min)
+ raise SfaInvalidArgument("%s must contain at least %d items" % (name, min))
if max is not None and len(value) > max:
- raise GeniInvalidArgument, "%s must contain at most %d items" % (name, max)
+ raise SfaInvalidArgument("%s must contain at most %d items" % (name, max))
else:
if min is not None and value < min:
- raise GeniInvalidArgument, "%s must be > %s" % (name, str(min))
+ raise SfaInvalidArgument("%s must be > %s" % (name, str(min)))
if max is not None and value > max:
- raise GeniInvalidArgument, "%s must be < %s" % (name, str(max))
+ raise SfaInvalidArgument("%s must be < %s" % (name, str(max)))
# If a list with particular types of items is expected
if isinstance(expected, (list, tuple, set)):
if isinstance(subparam, Parameter) and \
subparam.optional is not None and \
not subparam.optional and key not in value.keys():
- raise GeniInvalidArgument("'%s' not specified" % key, name)
+ raise SfaInvalidArgument("'%s' not specified" % key, name)
#if auth is not None:
# auth.check(self, *args)