1
# Copyright 2009 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Metaclass to automatically decorate methods."""
6
from types import FunctionType
10
__all__ = ['AutoDecorate']
13
def AutoDecorate(*decorators):
14
"""Factory to generate metaclasses that automatically apply decorators."""
16
class AutoDecorateMetaClass(type):
17
def __new__(cls, class_name, bases, class_dict):
19
for name, value in class_dict.items():
20
if type(value) == FunctionType:
21
for decorator in decorators:
22
value = decorator(value)
23
assert callable(value), (
24
"Decorator %s didn't return a callable."
26
new_class_dict[name] = value
27
return type.__new__(cls, class_name, bases, new_class_dict)
29
return AutoDecorateMetaClass