/usr/share/pyshared/nevow/accessors.py is in python-nevow 0.10.0-4build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | # Copyright (c) 2004 Divmod.
# See LICENSE for details.
"""Standard IData introspection adapters.
Classes and functions in this module are responsible for resolving
data directives into the actual data to be rendered.
By default, Nevow knows how to I{look inside} Python's mapping (dict)
and sequence (tuple, list) types as well as call any functions and
methods found in the stan tree.
"""
from zope.interface import implements
import twisted.python.components as tpc
from nevow.inevow import IGettable, IContainer, IData
from nevow import util
def convertToData(data, context):
"""Recursively resolve the data until either a Twisted deferred or
something that does not implement the IGettable interface is
found.
"""
newdata = IGettable(data, None)
if newdata is not None:
olddata = newdata
newdata = olddata.get(context)
if isinstance(newdata, util.Deferred):
return newdata.addCallback(convertToData, context)
elif newdata is olddata:
return olddata
else:
return convertToData(newdata, context)
else:
return data
class NoAccessor(NotImplementedError):
pass
class DirectiveAccessor(tpc.Adapter):
implements(IGettable)
def get(self, context):
data = context.locate(IData)
container = IContainer(data, None)
if container is None:
raise NoAccessor, "%r does not implement IContainer, and there is no registered adapter." % data
child = container.child(context, self.original.name)
return child
class SlotAccessor(tpc.Adapter):
implements(IGettable)
def get(self, context):
return context.locateSlotData(self.original.name)
class FunctionAccessor(tpc.Adapter):
implements(IGettable)
def get(self, context):
return self.original(context, context.locate(IData))
class DictionaryContainer(tpc.Adapter):
implements(IContainer)
def child(self, context, name):
return self.original[name]
class ObjectContainer(tpc.Adapter):
"""Retrieve object attributes in response to a data directive; providing
easy access to your application objects' attributes.
ObjectContainer is not registered as an adapter for any objects by default.
It must be registered for each type you want to adapt.
The adapter will cowardly refuse to get any attributes that start with an
underscore.
For example:
>>> class Image:
... def __init__(self, filename, comments):
... self.filename = filename # A string
... self.comments = comments # A sequence of strings
...
>>> registerAdapter(ObjectContainer, Image, IContainer)
Registering the adapter allows Nevow to retrieve attributes from the Image
instance returned by the data_image method when rendering the following
XHTML template::
<div n:data="image">
<p n:data="filename" n:render="string">filename</p>
<ul n:data="comments" n:render="sequence">
<li n:pattern="item" n:render="string">comment</li>
</ul>
</div>
"""
implements(IContainer)
def child(self, context, name):
if name[:1] == '_':
raise ValueError("ObjectContainer does not support attribute names starting with '_', got %r"%name)
return getattr(self.original, name)
def intOrNone(s):
try:
return int(s)
except ValueError:
return None
class ListContainer(tpc.Adapter):
implements(IContainer)
def child(self, context, name):
if ':' in name:
return self.original[slice(*[intOrNone(x) for x in name.split(':')])]
return self.original[int(name)]
|