X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Futil%2Fmethod.py;h=1d3de5754c5987ff360bbfb7da17652b72abbb6f;hb=a0ef6e7c91c3c8fa376943d28fec8c5c204a78cd;hp=ec9440e5bfb481473045a71b37c98d081c8b49fe;hpb=eababa96fb603cdd552bc03091813544b099befd;p=sfa.git diff --git a/sfa/util/method.py b/sfa/util/method.py index ec9440e5..1d3de575 100644 --- a/sfa/util/method.py +++ b/sfa/util/method.py @@ -3,24 +3,16 @@ # # -### $Id$ -### $URL$ - -import os, time -from types import * -from types import StringTypes -import traceback +import time +from types import IntType, LongType, StringTypes import textwrap -import xmlrpclib +from sfa.util.sfalogging import logger +from sfa.util.faults import SfaFault, SfaInvalidAPIMethod, SfaInvalidArgumentCount, SfaInvalidArgument -from sfa.util.sfalogging import sfa_logger -from sfa.util.faults import * -from sfa.util.parameter import Parameter, Mixed, python_type, xmlrpc_type -from sfa.trust.auth import Auth +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 SfaAPI functions. At a minimum, all SfaAPI functions must define: @@ -48,10 +40,8 @@ class Method (object): def call(self, *args): """ Method body for all SfaAPI functions. Must override. - """ - - return True + return None def __init__(self, api): self.name = self.__class__.__name__ @@ -66,7 +56,7 @@ class Method (object): def __call__(self, *args, **kwds): """ - Main entry point for all SfaAPI functions. Type checks + Main entry point for all SFA API functions. Type checks arguments, authenticates, and executes call(). """ @@ -76,34 +66,32 @@ class Method (object): if not self.api.interface or self.api.interface not in self.interfaces: 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 SfaInvalidArgumentCount(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'): - msg=getattr(self,'message',"method %s completed in %02f s"%(methodname,runtime)) - sfa_logger.info(msg) + 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 SfaFault, 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 - sfa_logger.log_exc("Method %s raised an exception"%self.name) + logger.log_exc("Method %s raised an exception"%self.name) raise fault @@ -215,7 +203,7 @@ class Method (object): try: self.type_check(name, value, item, args) return - except SfaInvalidArgument, fault: + except SfaInvalidArgument as fault: pass raise fault @@ -255,28 +243,27 @@ class Method (object): elif not isinstance(value, expected_type): raise SfaInvalidArgument("expected %s, got %s" % \ - (xmlrpc_type(expected_type), - xmlrpc_type(type(value))), + (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 SfaInvalidArgument, "%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 SfaInvalidArgument, "%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 SfaInvalidArgument, "%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 SfaInvalidArgument, "%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 SfaInvalidArgument, "%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 SfaInvalidArgument, "%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)):