~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/canonical/autodecorate.py

  • Committer: Jonathan Lange
  • Date: 2011-02-17 15:28:00 UTC
  • mto: This revision was merged to the branch mainline in revision 12413.
  • Revision ID: jml@canonical.com-20110217152800-r4zbyox5fo0qn3s7
Remove AutoDecorate from canonical, as well as its doctest.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
 
# GNU Affero General Public License version 3 (see the file LICENSE).
3
 
 
4
 
"""Metaclass to automatically decorate methods."""
5
 
 
6
 
from types import FunctionType
7
 
 
8
 
 
9
 
__metaclass__ = type
10
 
__all__ = ['AutoDecorate']
11
 
 
12
 
 
13
 
def AutoDecorate(*decorators):
14
 
    """Factory to generate metaclasses that automatically apply decorators."""
15
 
 
16
 
    class AutoDecorateMetaClass(type):
17
 
        def __new__(cls, class_name, bases, class_dict):
18
 
            new_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."
25
 
                            % repr(decorator))
26
 
                new_class_dict[name] = value
27
 
            return type.__new__(cls, class_name, bases, new_class_dict)
28
 
 
29
 
    return AutoDecorateMetaClass