Is there a way to get decorator function arguments at runtime? The project I'm working on is huge, and it would be a huge effort to rewrite the affected code. I need a dynamic solution to list all categories assigned to a function list. To do this, I want to avoid super hacking solutions, such as reusing all of my modules. Can this be done by checking frames from the call stack?
In our environment, functions are object methods, and we also use chained decorators. For ease of understanding, I have compiled this bit of code.
If this is not possible, I can build another decorator for parts of the project, although it will add much more complexity. But any suggestion to solve my problem is welcome.
def check(*args):
return True
def decorate(*categories):
def wrap(f):
def wrap_check_categories(*args, **kwargs):
if check(*categories):
return f(*args, **kwargs)
else:
raise Exception
return wrap_check_categories
return wrap
def get_categories(f):
'''Returns decorator parameters'''
raise NotImplementedError
@decorate('foo', 'bar')
def fancy_func(*args, **kwargs):
return args, kwargs
def main():
print get_categories(fancy_func)
if __name__ == '__main__':
main()