Exemple #1
0
    def __cache( self, method_name, *args, **kwargs ):
        """
        Store a call descriptor

        """
        trace_string           = util.get_stack(method_name)
        call_hash              = self.__get_hash(args, trace_string, kwargs)
        cd                     = call_descriptor.fetch( call_hash )
        if not cd:
            c  = self.__store__['callables'][method_name]
            if hasattr( c, '__class__' ) and c.__class__ == LazyBones:
                c = c.init()
            returnval = c(*args, **kwargs)
            cd = call_descriptor.CallDescriptor( hash      = call_hash,
                                                 stack     = trace_string,
                                                 method    = method_name,
                                                 returnval = returnval,
                                                 args      = args,
                                                 kwargs    = kwargs )
            cd.save()
            if not call_hash:
                raise Exception("CALL HASH IS NONE")

            util.last_hash = call_hash
            self.last_cached = call_hash
        else:
            returnval = cd.returnval

        if inspect.isclass(returnval):
            returnval = LazyBones( c, args, kwargs )

        return returnval
Exemple #2
0
def get_or_store(observed_value):
    caller = inspect.stack()[2][3]
    trace_string = util.get_stack(caller)
    counter_value = counter.get_from_trace_for_ev(trace_string)
    call_hash = sha1("%s.%s" % (trace_string,
                                counter_value)).hexdigest()

    ev = fetch(call_hash)
    if not ev or (config.CALIENDO_PROMPT and prompt.should_modify_expected_value(caller)):
        ev = ExpectedValue(call_hash=call_hash,
                           expected_value=prompt.modify_expected_value(observed_value, caller))
        ev.save()

    return ev.expected_value
def cache(handle=lambda *args, **kwargs: None,
          args=UNDEFINED,
          kwargs=UNDEFINED,
          ignore=UNDEFINED,
          call_stack=UNDEFINED,
          callback=UNDEFINED,
          subsequent_rvalue=UNDEFINED):
    """
    Store a call descriptor

    :param lambda handle: Any callable will work here. The method to cache.
    :param tuple args: The arguments to the method.
    :param dict kwargs: The keyword arguments to the method.
    :param tuple(list(int), list(str)) ignore: A tuple of arguments to ignore. The first element should be a list of positional arguments. The second should be a list of keys for keyword arguments.
    :param caliendo.hooks.CallStack call_stack: The stack of calls thus far for this patch.
    :param function callback: The callback function to execute each time there is a cache hit for 'handle' (actually mechanism is more complicated, but this is what it boils down to)
    :param mixed subsequent_rvalue: If passed; this will be the return value each time this method is run regardless of what is returned when it is initially cached. Caching for this method will be skipped. This is useful when the method returns something unpickleable but we still need to stub it out.

    :returns: The value of handle(*args, **kwargs)
    """
    if args == UNDEFINED:
        args = tuple()
    if kwargs == UNDEFINED:
        kwargs = {}
    if not USE_CALIENDO:
        return handle(*args, **kwargs)

    filtered_args = ignore.filter_args(
        args) if ignore is not UNDEFINED else args
    filtered_kwargs = ignore.filter_kwargs(
        kwargs) if ignore is not UNDEFINED else args

    trace_string = util.get_stack(handle.__name__)
    call_hash = get_hash(filtered_args, trace_string, filtered_kwargs, ignore)
    cd = call_descriptor.fetch(call_hash)
    modify_or_replace = 'no'

    util.set_current_hash(call_hash)

    if config.CALIENDO_PROMPT:
        display_name = (
            "(test %s): " %
            caliendo.util.current_test) if caliendo.util.current_test else ''
        if hasattr(handle, '__module__') and hasattr(handle, '__name__'):
            display_name += "%s.%s" % (handle.__module__, handle.__name__)
        else:
            display_name += handle

        if cd:
            modify_or_replace = prompt.should_modify_or_replace_cached(
                display_name)

    if not cd or modify_or_replace == 'replace':
        returnval = handle(*args, **kwargs)
    elif cd and modify_or_replace == 'modify':
        returnval = prompt.modify_cached_value(cd.returnval,
                                               calling_method=display_name,
                                               calling_test='')

    if cd and subsequent_rvalue != UNDEFINED:
        return subsequent_rvalue
    elif subsequent_rvalue != UNDEFINED:
        original_rvalue = returnval
        returnval = subsequent_rvalue

    if not cd or modify_or_replace != 'no':
        if isinstance(handle, types.MethodType):
            filtered_args = list(filtered_args)
            filtered_args[0] = util.serialize_item(filtered_args[0])
            filtered_args = tuple(filtered_args)

        cd = call_descriptor.CallDescriptor(hash=call_hash,
                                            stack=trace_string,
                                            method=handle.__name__,
                                            returnval=returnval,
                                            args=filtered_args,
                                            kwargs=filtered_kwargs)

        cd.save()

    util.set_last_hash(cd.hash)

    if call_stack != UNDEFINED:
        call_stack.add(cd)
        if callback != UNDEFINED:
            call_stack.add_hook(
                Hook(call_descriptor_hash=cd.hash, callback=callback))

    if subsequent_rvalue == UNDEFINED:
        return cd.returnval
    else:
        return original_rvalue
Exemple #4
0
def cache(handle=lambda *args, **kwargs: None, args=UNDEFINED, kwargs=UNDEFINED, ignore=UNDEFINED, call_stack=UNDEFINED, callback=UNDEFINED):
    """
    Store a call descriptor

    :param lambda handle: Any callable will work here. The method to cache.
    :param tuple args: The arguments to the method.
    :param dict kwargs: The keyword arguments to the method.
    :param tuple(list(int), list(str)) ignore: A tuple of arguments to ignore. The first element should be a list of positional arguments. The second should be a list of keys for keyword arguments.
    :param caliendo.hooks.CallStack call_stack: The stack of calls thus far for this patch.
    :param function callback: The callback function to execute each time there is a cache hit for 'handle' (actually mechanism is more complicated, but this is what it boils down to)

    :returns: The value of handle(*args, **kwargs)
    """
    if args == UNDEFINED:
        args = tuple()
    if kwargs == UNDEFINED:
        kwargs = {}
    if not USE_CALIENDO:
        return handle(*args, **kwargs)

    trace_string      = util.get_stack(handle.__name__)
    call_hash         = get_hash(args, trace_string, kwargs, ignore)
    cd                = call_descriptor.fetch(call_hash)
    modify_or_replace = 'no'

    util.set_current_hash(call_hash)

    if config.CALIENDO_PROMPT:
        display_name = ("(test %s): " % caliendo.util.current_test) if caliendo.util.current_test else ''
        if hasattr(handle, '__module__') and hasattr(handle, '__name__'):
            display_name += "%s.%s" % (handle.__module__, handle.__name__)
        else:
            display_name += handle

        if cd:
            modify_or_replace = prompt.should_modify_or_replace_cached(display_name)

    if not cd or modify_or_replace == 'replace':
        returnval = handle(*args, **kwargs)
    elif cd and modify_or_replace == 'modify':
        returnval = prompt.modify_cached_value(cd.returnval,
                                               calling_method=display_name,
                                               calling_test='')
    if not cd or modify_or_replace != 'no':
        if isinstance(handle, types.MethodType):
            args = list(args)
            args[0] = util.serialize_item(args[0])
            args = tuple(args)


        cd = call_descriptor.CallDescriptor( hash      = call_hash,
                                             stack     = trace_string,
                                             method    = handle.__name__,
                                             returnval = returnval,
                                             args      = args,
                                             kwargs    = kwargs )

        cd.save()

    util.set_last_hash(cd.hash)

    if call_stack != UNDEFINED:
        call_stack.add(cd)
        if callback != UNDEFINED:
            call_stack.add_hook(Hook(call_descriptor_hash=cd.hash,
                                     callback=callback))


    return cd.returnval
Exemple #5
0
def cache(handle=lambda *args, **kwargs: None, args=UNDEFINED, kwargs=UNDEFINED, ignore=UNDEFINED, call_stack=UNDEFINED, callback=UNDEFINED, subsequent_rvalue=UNDEFINED):
    """
    Store a call descriptor

    :param lambda handle: Any callable will work here. The method to cache.
    :param tuple args: The arguments to the method.
    :param dict kwargs: The keyword arguments to the method.
    :param tuple(list(int), list(str)) ignore: A tuple of arguments to ignore. The first element should be a list of positional arguments. The second should be a list of keys for keyword arguments.
    :param caliendo.hooks.CallStack call_stack: The stack of calls thus far for this patch.
    :param function callback: The callback function to execute each time there is a cache hit for 'handle' (actually mechanism is more complicated, but this is what it boils down to)
    :param mixed subsequent_rvalue: If passed; this will be the return value each time this method is run regardless of what is returned when it is initially cached. Caching for this method will be skipped. This is useful when the method returns something unpickleable but we still need to stub it out.

    :returns: The value of handle(*args, **kwargs)
    """
    if args == UNDEFINED:
        args = tuple()
    if kwargs == UNDEFINED:
        kwargs = {}
    if not USE_CALIENDO:
        return handle(*args, **kwargs)

     
    filtered_args = ignore.filter_args(args) if ignore is not UNDEFINED else args
    filtered_kwargs = ignore.filter_kwargs(kwargs) if ignore is not UNDEFINED else args

    trace_string      = util.get_stack(handle.__name__)
    call_hash         = get_hash(filtered_args, trace_string, filtered_kwargs, ignore)
    cd                = call_descriptor.fetch(call_hash)
    modify_or_replace = 'no'

    util.set_current_hash(call_hash)

    if config.CALIENDO_PROMPT:
        display_name = ("(test %s): " % caliendo.util.current_test) if caliendo.util.current_test else ''
        if hasattr(handle, '__module__') and hasattr(handle, '__name__'):
            display_name += "%s.%s" % (handle.__module__, handle.__name__)
        else:
            display_name += handle

        if cd:
            modify_or_replace = prompt.should_modify_or_replace_cached(display_name)

    if not cd or modify_or_replace == 'replace':
        returnval = handle(*args, **kwargs)
    elif cd and modify_or_replace == 'modify':
        returnval = prompt.modify_cached_value(cd.returnval,
                                               calling_method=display_name,
                                               calling_test='')

    if cd and subsequent_rvalue != UNDEFINED:
        return subsequent_rvalue
    elif subsequent_rvalue != UNDEFINED:
        original_rvalue = returnval
        returnval = subsequent_rvalue

    if not cd or modify_or_replace != 'no':
        if isinstance(handle, types.MethodType):
            filtered_args = list(filtered_args)
            filtered_args[0] = util.serialize_item(filtered_args[0])
            filtered_args = tuple(filtered_args)

        cd = call_descriptor.CallDescriptor( hash      = call_hash,
                                             stack     = trace_string,
                                             method    = handle.__name__,
                                             returnval = returnval,
                                             args      = filtered_args,
                                             kwargs    = filtered_kwargs )

        cd.save()

    util.set_last_hash(cd.hash)

    if call_stack != UNDEFINED:
        call_stack.add(cd)
        if callback != UNDEFINED:
            call_stack.add_hook(Hook(call_descriptor_hash=cd.hash,
                                     callback=callback))

    if subsequent_rvalue == UNDEFINED:
        return cd.returnval
    else:
        return original_rvalue