Exemple #1
0
def validateAfter(func, validator=None):
    '''
    Function wrapper for post-invocation-validation
     
    @param func: a target function
    @param validator: default=None, checks getSafe and returns the getSafe(may be transformed) is the value if this is None or not-function type, or invokes the value with 'getSafe' if this is a function
    @return: a wrapped function 
    '''
    @wraps(func)
    def wrapped(*Args, **kwArgs):
        getSafe = func(*Args, **kwArgs)
        if validator is None and getSafe is not None:
            raise ValidationException("getSafe=%s expect=None")
        elif isinstance(validator, types.FunctionType):
            getSafe = validator(func, getSafe, *Args, **kwArgs)
        return getSafe

    return inheritOriginalArgSpec(wrapped, func)
Exemple #2
0
    def adapter(func):
        @wraps(func)
        def wrapped(*args, **kwargs):
            try:
                return func(*args, **kwargs)
            except BaseException as e:
                if excepts.isValid(e):
                    raise
                else:
                    raise ValidationException("%s is not a checked error" %
                                              str(type(e)))
            except:
                excInfo = sys.exc_info()
                if excepts.isValid(excInfo[0]):
                    raise
                else:
                    raise ValidationException(
                        "%s is not a checked error(exc_info=%s)" %
                        toStrTuple(excInfo[0], excInfo))

        return inheritOriginalArgSpec(wrapped, func)
Exemple #3
0
def validateBefore(func, validator=None):
    '''
    Function wrapper for pre-invocation-validation
    
    @param func: a target function
    @param validator: default=None, no validation is if this is None, or validate before calling the function if this is a function
    @return: a function which is wrapped by validator
    '''
    if validator is None:
        return func
    else:
        iaa = InvocableArgumentAccessor(func)

        @wraps(func)
        def wrapped(*Args, **kwArgs):
            if isinstance(validator, types.FunctionType):
                checkArguments_ArgumentManipulator = validator(
                    func, iaa, *Args, **kwArgs)
                return func(*checkArguments_ArgumentManipulator.fullArgs,
                            **checkArguments_ArgumentManipulator.kwArgs)
            else:
                raise ValidationException("Cannot Value-validation")

        return inheritOriginalArgSpec(wrapped, func)
Exemple #4
0
    def adapter(func):
        @wraps(func)
        def wrapped(*args, **kwargs):
            return validateBefore(func, _instanceOf)(*args, **kwargs)

        return inheritOriginalArgSpec(wrapped, func)
Exemple #5
0
    def adapter(func):
        @wraps(func)
        def wrapped(*args, **kwargs):
            return validateAfter(func, _resultIs)(*args, **kwargs)

        return inheritOriginalArgSpec(wrapped, func)