GUI editor to tame mod_security rules

⌈⌋ ⎇ branch:  modseccfg


@inject

This is a fairly basic decorator for monkeypatching:

import functools

@functools.singledispatch
def inject(*mods):
    """ decorator to override module/object functions """
    def decorator(func):
        for mod in mods:
            setattr(mod, func.__name__, func)
    return decorator

It's basically shorthand for an assignment. But allows to add or redefine object methods or module functions more easily. And with simple parameter binding avoids extra references to the previous definition:

@inject(re)
function match(*args, **kwargs, orig=re.match):
    # support \R or \K here..
    orig(*args, **kwargs)

And it can be applied to multiple objects as well, even setting dunder methods:

@inject(sg.Window, sg.Element)
function __getattr__(name):
    # smarter name matching perhaps..
    return self.__dict__.get(name)

Or just simply declare new functions:

@inject(re)
def grep(regex, list, flags=0):
    return [s for s in list if re.search(regex, s, flags)]

The @inject decorator accidentally also works from within class declarations.