X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Futil%2Fmethod.py;h=b9cd05e7152ea3c687d7c7056b737bb0b36c6603;hb=b8596ce95578bf77158db2a5dacbaab36bdf6b16;hp=9683bc54198552d11094f9b65705b4e8797a4277;hpb=e0ffcf4ed82441080a2e1afb6cbfbdc0a57b3c3e;p=sfa.git diff --git a/sfa/util/method.py b/sfa/util/method.py index 9683bc54..b9cd05e7 100644 --- a/sfa/util/method.py +++ b/sfa/util/method.py @@ -1,29 +1,20 @@ # -# 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.trust.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] @@ -48,11 +39,9 @@ class Method (object): 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__ @@ -67,7 +56,7 @@ class Method (object): def __call__(self, *args, **kwds): """ - Main entry point for all GeniAPI functions. Type checks + Main entry point for all SfaAPI functions. Type checks arguments, authenticates, and executes call(). """ @@ -75,41 +64,34 @@ class Method (object): 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.SFA_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, 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.SFA_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 @@ -221,7 +203,7 @@ class Method (object): try: self.type_check(name, value, item, args) return - except GeniInvalidArgument, fault: + except SfaInvalidArgument, fault: pass raise fault @@ -260,29 +242,28 @@ class Method (object): 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)): @@ -303,7 +284,7 @@ class Method (object): 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)