| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
"""Decorator helpers that simplify class composition.""" |
| 18 |
|
| 19 |
__all__ = [ |
| 20 |
"Passthrough", |
| 21 |
"delegate_to", |
| 22 |
] |
| 23 |
|
| 24 |
from zope.interface import classImplements |
| 25 |
|
| 26 |
|
| 27 |
def delegate_to(*interfaces, context="context"): |
| 28 |
"""Make an adapter into a decorator. |
| 29 |
|
| 30 |
Use like: |
| 31 |
|
| 32 |
@implementer(IRosettaProject) |
| 33 |
@delegate_to(IProject) |
| 34 |
class RosettaProject: |
| 35 |
def __init__(self, context): |
| 36 |
self.context = context |
| 37 |
|
| 38 |
def methodFromRosettaProject(self): |
| 39 |
return self.context.methodFromIProject() |
| 40 |
|
| 41 |
If you want to use a different name than "context" then you can explicitly |
| 42 |
say so: |
| 43 |
|
| 44 |
@implementer(IRosettaProject) |
| 45 |
@delegate_to(IProject, context='project') |
| 46 |
class RosettaProject: |
| 47 |
def __init__(self, project): |
| 48 |
self.project = project |
| 49 |
|
| 50 |
def methodFromRosettaProject(self): |
| 51 |
return self.project.methodFromIProject() |
| 52 |
|
| 53 |
The adapter class will implement the interface it is decorating. |
| 54 |
|
| 55 |
The minimal decorator looks like this: |
| 56 |
|
| 57 |
@delegate_to(IProject) |
| 58 |
class RosettaProject: |
| 59 |
def __init__(self, context): |
| 60 |
self.context = context |
| 61 |
""" |
| 62 |
if len(interfaces) == 0: |
| 63 |
raise TypeError("At least one interface is required") |
| 64 |
|
| 65 |
def _decorator(cls): |
| 66 |
missing = object() |
| 67 |
for interface in interfaces: |
| 68 |
classImplements(cls, interface) |
| 69 |
for name in interface: |
| 70 |
if getattr(cls, name, missing) is missing: |
| 71 |
setattr(cls, name, Passthrough(name, context)) |
| 72 |
return cls |
| 73 |
|
| 74 |
return _decorator |
| 75 |
|
| 76 |
|
| 77 |
class Passthrough: |
| 78 |
"""Call the delegated class for the decorator class. |
| 79 |
|
| 80 |
If the ``adaptation`` argument is not None, it should be a callable. It |
| 81 |
will be called with the context, and should return an object that will |
| 82 |
have the delegated attribute. The ``adaptation`` argument is expected to |
| 83 |
be used with an interface, to adapt the context. |
| 84 |
""" |
| 85 |
|
| 86 |
def __init__(self, name, contextvar, adaptation=None): |
| 87 |
self.name = name |
| 88 |
self.contextvar = contextvar |
| 89 |
self.adaptation = adaptation |
| 90 |
|
| 91 |
def __get__(self, inst, cls=None): |
| 92 |
if inst is None: |
| 93 |
return self |
| 94 |
else: |
| 95 |
context = getattr(inst, self.contextvar) |
| 96 |
if self.adaptation is not None: |
| 97 |
context = self.adaptation(context) |
| 98 |
return getattr(context, self.name) |
| 99 |
|
| 100 |
def __set__(self, inst, value): |
| 101 |
context = getattr(inst, self.contextvar) |
| 102 |
if self.adaptation is not None: |
| 103 |
context = self.adaptation(context) |
| 104 |
setattr(context, self.name, value) |
| 105 |
|
| 106 |
def __delete__(self, inst): |
| 107 |
raise NotImplementedError |