def test_fetch_call_descriptor(self):
        hash = hashlib.sha1("test1").hexdigest()
        method = "test1"
        returnval = {}
        args = ()

        cd = CallDescriptor(hash=hash,
                            stack='',
                            method=method,
                            returnval=returnval,
                            args=args)
        cd.save()

        cd = fetch(hash)
        self.assertEquals(cd.hash, hash)
        self.assertEquals(cd.methodname, method)

        hash = hashlib.sha1("test1").hexdigest()
        method = "test2"
        cd.methodname = method
        cd.save()

        cd = fetch(hash)
        self.assertEquals(cd.hash, hash)
        self.assertEquals(cd.methodname, method)

        hash = hashlib.sha1("test3").hexdigest()
        method = "test3"
        cd.hash = hash
        cd.methodname = method
        cd.save()

        cd = fetch(hash)
        self.assertEquals(cd.hash, hash)
        self.assertEquals(cd.methodname, method)
예제 #2
0
    def test_fetch_call_descriptor(self):
        hash      = hashlib.sha1( "test1" ).hexdigest()
        method    = "test1"
        returnval = { }
        args      = ( )

        cd = CallDescriptor( hash=hash, stack='', method=method, returnval=returnval, args=args )
        cd.save( )

        cd = fetch( hash )
        self.assertEquals( cd.hash, hash )
        self.assertEquals( cd.methodname, method )

        hash      = hashlib.sha1( "test1" ).hexdigest()
        method    = "test2"
        cd.methodname = method
        cd.save( )

        cd = fetch( hash )
        self.assertEquals( cd.hash, hash )
        self.assertEquals( cd.methodname, method )

        hash      = hashlib.sha1( "test3" ).hexdigest()
        method    = "test3"
        cd.hash   = hash
        cd.methodname = method
        cd.save( )

        cd = fetch( hash )
        self.assertEquals( cd.hash, hash )
        self.assertEquals( cd.methodname, method )
예제 #3
0
    def test_call_descriptor(self):
        hash      = hashlib.sha1( "adsf" ).hexdigest()
        method    = "mymethod"
        returnval = {'thisis': [ 'my', 'return','val' ] }
        args      = ( 'a', 'b', 'c' )
        #self, hash='', stack='', method='', returnval='', args='', kwargs='' ):
        cd = CallDescriptor(
            hash=hash,
            stack='',
            method=method,
            returnval=returnval,
            args=args )

        cd.save()

        self.assertEqual( cd.hash, hash )
        self.assertEqual( cd.methodname, method )
        self.assertEqual( cd.returnval, returnval )
        self.assertEqual( cd.args, args )

        cd = fetch( hash )

        self.assertEqual( cd.hash, hash )
        self.assertEqual( cd.methodname, method )
        self.assertEqual( cd.returnval, returnval )
        self.assertEqual( cd.args, args )
예제 #4
0
파일: util.py 프로젝트: buzzfeed/caliendo
def recache( methodname=None, filename=None ):
    """
    Deletes entries corresponding to methodname in filename. If no arguments are passed it recaches the entire table.

    :param str methodname: The name of the method to target. This will delete ALL entries this method appears in the stack trace for.
    :param str filename: The filename the method is executed in. (include .py extension)

    :rtype int: The number of deleted entries
    """
    if not methodname and not filename:
        hashes = get_unique_hashes()
        deleted = 0
        for hash in hashes:
            delete_io(hash)
            deleted = deleted + 1
    else:
        reqd_strings = []
        if methodname:
            reqd_strings.append( methodname )
        if filename:
            reqd_strings.append( filename )
        hashes = get_unique_hashes()
        deleted = 0
        for hash in hashes:
            cd = call_descriptor.fetch( hash )
            if all( [ s in cd.stack for s in reqd_strings ] ):
                delete_io( hash )
                deleted = deleted + 1
        return deleted
    def test_call_descriptor(self):
        hash = hashlib.sha1("adsf").hexdigest()
        method = "mymethod"
        returnval = {'thisis': ['my', 'return', 'val']}
        args = ('a', 'b', 'c')
        #self, hash='', stack='', method='', returnval='', args='', kwargs='' ):
        cd = CallDescriptor(hash=hash,
                            stack='',
                            method=method,
                            returnval=returnval,
                            args=args)

        cd.save()

        self.assertEqual(cd.hash, hash)
        self.assertEqual(cd.methodname, method)
        self.assertEqual(cd.returnval, returnval)
        self.assertEqual(cd.args, args)

        cd = fetch(hash)

        self.assertEqual(cd.hash, hash)
        self.assertEqual(cd.methodname, method)
        self.assertEqual(cd.returnval, returnval)
        self.assertEqual(cd.args, args)
예제 #6
0
파일: facade.py 프로젝트: mplacona/caliendo
    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
예제 #7
0
def recache(methodname=None, filename=None):
    """
    Deletes entries corresponding to methodname in filename. If no arguments are passed it recaches the entire table.

    :param str methodname: The name of the method to target. This will delete ALL entries this method appears in the stack trace for.
    :param str filename: The filename the method is executed in. (include .py extension)

    :rtype int: The number of deleted entries
    """
    if not methodname and not filename:
        hashes = get_unique_hashes()
        deleted = 0
        for hash in hashes:
            delete_io(hash)
            deleted = deleted + 1
    else:
        reqd_strings = []
        if methodname:
            reqd_strings.append(methodname)
        if filename:
            reqd_strings.append(filename)
        hashes = get_unique_hashes()
        deleted = 0
        for hash in hashes:
            cd = call_descriptor.fetch(hash)
            if all([s in cd.stack for s in reqd_strings]):
                delete_io(hash)
                deleted = deleted + 1
        return deleted
예제 #8
0
파일: facade.py 프로젝트: pahko/caliendo
    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
예제 #9
0
    def test_call_descriptor(self):
        hash = hashlib.sha1("adsf").hexdigest()
        method = "mymethod"
        returnval = {"thisis": ["my", "return", "val"]}
        args = ("a", "b", "c")
        # self, hash='', stack='', method='', returnval='', args='', kwargs='' ):
        cd = CallDescriptor(hash=hash, stack="", method=method, returnval=returnval, args=args)

        cd.save()

        self.assertEqual(cd.hash, hash)
        self.assertEqual(cd.methodname, method)
        self.assertEqual(cd.returnval, returnval)
        self.assertEqual(cd.args, args)

        cd = fetch(hash)

        self.assertEqual(cd.hash, hash)
        self.assertEqual(cd.methodname, method)
        self.assertEqual(cd.returnval, returnval)
        self.assertEqual(cd.args, args)
예제 #10
0
    def test_recache(self):
        mtc = TestC( )
        mtc_f = Facade( mtc )

        hashes = []

        self.assertEquals( mtc.methoda( ), mtc_f.methoda( ) )
        self.assertIsNotNone(mtc_f.last_cached)
        hashes.append( mtc_f.last_cached )
        self.assertEquals( mtc.methodb( ), mtc_f.methodb( ) )
        self.assertIsNotNone(mtc_f.last_cached)
        hashes.append( mtc_f.last_cached )
        self.assertEquals( mtc_f.methoda( ), "a" )
        self.assertIsNotNone(mtc_f.last_cached)
        hashes.append( mtc_f.last_cached )

        self.assertEquals( mtc_f.increment( ), 1 )
        self.assertIsNotNone(mtc_f.last_cached)
        hashes.append( mtc_f.last_cached )
        self.assertEquals( mtc_f.increment( ), 2 )
        self.assertIsNotNone(mtc_f.last_cached)
        hashes.append( mtc_f.last_cached )
        self.assertEquals( mtc_f.increment( ), 3 )
        self.assertIsNotNone(mtc_f.last_cached)
        hashes.append( mtc_f.last_cached )
        self.assertEquals( mtc_f.increment( ), 4 )
        self.assertIsNotNone(mtc_f.last_cached)
        hashes.append( mtc_f.last_cached )

        # Ensure hashes are now in db:
        for hash in hashes:
            self.assertIsNotNone(hash, "Hash is none. whoops.")
            cd = fetch( hash )
            self.assertTrue( cd is not None, "%s was not found" % hash )
            self.assertEquals( cd.hash, hash, "%s was not found" % hash )

        # Delete some:
        caliendo.util.recache( 'methodb', 'caliendo_test.py' )
        caliendo.util.recache( 'methoda', 'caliendo_test.py' )

        # Ensure they're gone:
        methodb = hashes[0]
        methoda = hashes[1]
        cd = fetch( methodb )

        self.assertIsNone( cd, "Method b failed to recache." )
        cd = fetch( methoda )

        self.assertIsNone( cd, "Method a failed to recache." )

        # Ensure the rest are there:
        hashes = hashes[3:]
        for hash in hashes:
            cd = fetch( hash )
            self.assertEquals( cd.hash, hash )

        # Delete them ALL:
        caliendo.util.recache()

        #Ensure they're all gone:
        for hash in hashes:
            cd = fetch( hash )
            self.assertIsNone( cd )
    def test_recache(self):
        mtc = TestC()
        mtc_f = Facade(mtc)

        hashes = []

        self.assertEquals(mtc.methoda(), mtc_f.methoda())
        self.assertIsNotNone(mtc_f.last_cached)
        hashes.append(mtc_f.last_cached)
        self.assertEquals(mtc.methodb(), mtc_f.methodb())
        self.assertIsNotNone(mtc_f.last_cached)
        hashes.append(mtc_f.last_cached)
        self.assertEquals(mtc_f.methoda(), "a")
        self.assertIsNotNone(mtc_f.last_cached)
        hashes.append(mtc_f.last_cached)

        self.assertEquals(mtc_f.increment(), 1)
        self.assertIsNotNone(mtc_f.last_cached)
        hashes.append(mtc_f.last_cached)
        self.assertEquals(mtc_f.increment(), 2)
        self.assertIsNotNone(mtc_f.last_cached)
        hashes.append(mtc_f.last_cached)
        self.assertEquals(mtc_f.increment(), 3)
        self.assertIsNotNone(mtc_f.last_cached)
        hashes.append(mtc_f.last_cached)
        self.assertEquals(mtc_f.increment(), 4)
        self.assertIsNotNone(mtc_f.last_cached)
        hashes.append(mtc_f.last_cached)

        # Ensure hashes are now in db:
        for hash in hashes:
            self.assertIsNotNone(hash, "Hash is none. whoops.")
            cd = fetch(hash)
            self.assertTrue(cd is not None, "%s was not found" % hash)
            self.assertEquals(cd.hash, hash, "%s was not found" % hash)

        # Delete some:
        caliendo.util.recache('methodb', 'caliendo_test.py')
        caliendo.util.recache('methoda', 'caliendo_test.py')

        # Ensure they're gone:
        methodb = hashes[0]
        methoda = hashes[1]
        cd = fetch(methodb)

        self.assertIsNone(cd, "Method b failed to recache.")
        cd = fetch(methoda)

        self.assertIsNone(cd, "Method a failed to recache.")

        # Ensure the rest are there:
        hashes = hashes[3:]
        for hash in hashes:
            cd = fetch(hash)
            self.assertEquals(cd.hash, hash)

        # Delete them ALL:
        caliendo.util.recache()

        #Ensure they're all gone:
        for hash in hashes:
            cd = fetch(hash)
            self.assertIsNone(cd)
예제 #12
0
파일: facade.py 프로젝트: mplacona/caliendo
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
예제 #13
0
파일: facade.py 프로젝트: pahko/caliendo
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
예제 #14
0
파일: facade.py 프로젝트: buzzfeed/caliendo
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