john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

oxygen skip decorator

# 2013-04-08 johnpfeiffer

from functools import wraps
import o2lib
from ssl import SSLError

def oxygen_skip_decorator( logger = None ):
    """Skip and Log if one of the Exceptions provided is raised
    :ExceptionsToCheck: tuple of exceptions to check
    :logger: logger to use. If None, print
    """
    def deco_skip( f ):

        @wraps( f )
        def f_skip( *args , **kwargs ):
            try:

                return f( *args , **kwargs )

            except o2lib.ApiSystemsException as error:
                if error.errorCode == 2 or error.errorCode == 4:
                    error_detail = o2lib.ApiSystemsErrorCode._VALUES_TO_NAMES[ error.errorCode ]
                    message = u"SKIPPING: {} due to {} {}".format( error_detail , str( error ) , repr( args ) )
                    if logger:
                        logger.warning( message )
                    print message
                    return      # skip continuing the function

            except SSLError:
                message = u"SKIPPING: SSLError when trying: {}".format( repr( args ) )
                if logger:
                    logger.error( message )
                print message
                return      # skip continuing the function


            raise error # if reached then propogate the error back up

        return f_skip  # true decorator

    return deco_skip

  • « retry decorator
  • dictionary iteration iteritems viewkeys »

Published

Apr 9, 2013

Category

python-oxygencloud-snapshot

~125 words

Tags

  • decorator 8
  • oxygen 14
  • python 180
  • skip 5
  • snapshot 12