Exemple #1
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 #2
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)

    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 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):
            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))

    if subsequent_rvalue == UNDEFINED:
        return cd.returnval
    else:
        return original_rvalue
Exemple #3
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