Exemplo n.º 1
0
def override_replace(target, name, func, combiner=replacement, reverse=False):
    obj = target
    cls = target.__class__

    is_derived = False
    # The function is defined in the superclass (which we don't override)
    if not name in cls.__dict__:
        is_derived = True
        # Bind the call to the superclass function
        orig_func = new.instancemethod(lambda self, *args, **named: getattr(super(cls, self), name).__call__(*args,**named), obj, cls)
    else:
        orig_func = getattr(obj, name)
    if not hasattr(orig_func, "stacked"):
        stacked_func = stacked_function(orig_func, combiner=combiner, reverse=reverse)
        setattr(target, name, stacked_func)
    else: stacked_func = orig_func
    # Add the replacement effect along with the card name (in case of multiple effects)
    new_func = [new.instancemethod(func, obj, cls), name, False] #card.name, False]
    stacked_func.add_func(new_func)
    def restore(stacked_func=stacked_func):
        stacked_func.remove_func(new_func)
        if not stacked_func.stacking():
            setattr(target, name, stacked_func.funcs[0])
            del stacked_func
    func.expire = restore
    return restore
Exemplo n.º 2
0
def override(func, name, cls, obj=None, combiner=logical_and):
    if obj: target = obj
    else: target = cls
    is_derived = False
    if not name in target.__dict__:
        orig_func = new.instancemethod(
            lambda *args, **named: getattr(super(cls, args[0]), name).__call__(
                *args[1:], **named), obj, cls)
        is_derived = True
    else:
        orig_func = getattr(target, name)
    if not hasattr(orig_func, "stacked"):
        stacked_func = stacked_function(orig_func, combiner)
        setattr(target, name, stacked_func)
    else:
        stacked_func = orig_func
    new_func = new.instancemethod(func, obj, cls)
    stacked_func.add_func(new_func)

    def restore(stacked_func=stacked_func):
        stacked_func.remove_func(new_func)
        if not stacked_func.stacking():
            #if not is_derived: setattr(target, name, stacked_func.funcs[0])
            #else: del target.__dict__[name]
            setattr(target, name, stacked_func.funcs[0])
            del stacked_func

    return restore
Exemplo n.º 3
0
def override(func, name, cls, obj=None, combiner=logical_and):
    if obj: target = obj
    else: target = cls
    is_derived = False
    if not name in target.__dict__:
        orig_func = new.instancemethod(lambda *args, **named: getattr(super(cls, args[0]), name).__call__(*args[1:],**named), obj, cls)
        is_derived = True
    else:
        orig_func = getattr(target, name)
    if not hasattr(orig_func, "stacked"):
        stacked_func = stacked_function(orig_func, combiner)
        setattr(target, name, stacked_func)
    else: stacked_func = orig_func
    new_func = new.instancemethod(func, obj, cls) 
    stacked_func.add_func(new_func)
    def restore(stacked_func=stacked_func):
        stacked_func.remove_func(new_func)
        if not stacked_func.stacking():
            #if not is_derived: setattr(target, name, stacked_func.funcs[0])
            #else: del target.__dict__[name]
            setattr(target, name, stacked_func.funcs[0])
            del stacked_func
    return restore