/usr/share/pyshared/zope/location/traversing.py is in python-zope.location 3.9.1-2.
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | ##############################################################################
#
# Copyright (c) 2003-2009 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Classes to support implenting IContained
"""
__docformat__ = 'restructuredtext'
import zope.component
import zope.component.interfaces
import zope.interface
from zope.location.interfaces import ILocationInfo
from zope.location.interfaces import ILocation, IRoot
from zope.location.location import Location
class LocationPhysicallyLocatable(object):
"""Provide location information for location objects
>>> from zope.interface.verify import verifyObject
>>> info = LocationPhysicallyLocatable(Location())
>>> verifyObject(ILocationInfo, info)
True
"""
zope.component.adapts(ILocation)
zope.interface.implements(ILocationInfo)
def __init__(self, context):
self.context = context
def getRoot(self):
"""Get the root location for a location.
See ILocationInfo
The root location is a location that contains the given
location and that implements IContainmentRoot.
>>> root = Location()
>>> zope.interface.directlyProvides(root, IRoot)
>>> LocationPhysicallyLocatable(root).getRoot() is root
True
>>> o1 = Location(); o1.__parent__ = root
>>> LocationPhysicallyLocatable(o1).getRoot() is root
True
>>> o2 = Location(); o2.__parent__ = o1
>>> LocationPhysicallyLocatable(o2).getRoot() is root
True
We'll get a TypeError if we try to get the location fo a
rootless object:
>>> o1.__parent__ = None
>>> LocationPhysicallyLocatable(o1).getRoot()
Traceback (most recent call last):
...
TypeError: Not enough context to determine location root
>>> LocationPhysicallyLocatable(o2).getRoot()
Traceback (most recent call last):
...
TypeError: Not enough context to determine location root
If we screw up and create a location cycle, it will be caught:
>>> o1.__parent__ = o2
>>> LocationPhysicallyLocatable(o1).getRoot()
Traceback (most recent call last):
...
TypeError: Maximum location depth exceeded, """ \
"""probably due to a a location cycle.
"""
context = self.context
max = 9999
while context is not None:
if IRoot.providedBy(context):
return context
context = context.__parent__
max -= 1
if max < 1:
raise TypeError("Maximum location depth exceeded, "
"probably due to a a location cycle.")
raise TypeError("Not enough context to determine location root")
def getPath(self):
"""Get the path of a location.
See ILocationInfo
This is an "absolute path", rooted at a root object.
>>> root = Location()
>>> zope.interface.directlyProvides(root, IRoot)
>>> LocationPhysicallyLocatable(root).getPath()
u'/'
>>> o1 = Location(); o1.__parent__ = root; o1.__name__ = 'o1'
>>> LocationPhysicallyLocatable(o1).getPath()
u'/o1'
>>> o2 = Location(); o2.__parent__ = o1; o2.__name__ = u'o2'
>>> LocationPhysicallyLocatable(o2).getPath()
u'/o1/o2'
It is an error to get the path of a rootless location:
>>> o1.__parent__ = None
>>> LocationPhysicallyLocatable(o1).getPath()
Traceback (most recent call last):
...
TypeError: Not enough context to determine location root
>>> LocationPhysicallyLocatable(o2).getPath()
Traceback (most recent call last):
...
TypeError: Not enough context to determine location root
If we screw up and create a location cycle, it will be caught:
>>> o1.__parent__ = o2
>>> LocationPhysicallyLocatable(o1).getPath()
Traceback (most recent call last):
...
TypeError: Maximum location depth exceeded, """ \
"""probably due to a a location cycle.
"""
path = []
context = self.context
max = 9999
while context is not None:
if IRoot.providedBy(context):
if path:
path.append('')
path.reverse()
return u'/'.join(path)
else:
return u'/'
path.append(context.__name__)
context = context.__parent__
max -= 1
if max < 1:
raise TypeError("Maximum location depth exceeded, "
"probably due to a a location cycle.")
raise TypeError("Not enough context to determine location root")
def getParent(self):
"""Returns the container the object was traversed via.
Returns None if the object is a containment root.
Raises TypeError if the object doesn't have enough context to get the
parent.
>>> root = Location()
>>> zope.interface.directlyProvides(root, IRoot)
>>> o1 = Location()
>>> o2 = Location()
>>> LocationPhysicallyLocatable(o2).getParent() # doctest: +ELLIPSIS
Traceback (most recent call last):
TypeError: ('Not enough context information to get parent', <zope.location.location.Location object at 0x...>)
>>> o1.__parent__ = root
>>> LocationPhysicallyLocatable(o1).getParent() == root
True
>>> o2.__parent__ = o1
>>> LocationPhysicallyLocatable(o2).getParent() == o1
True
"""
parent = getattr(self.context, '__parent__', None)
if parent is not None:
return parent
raise TypeError('Not enough context information to get parent',
self.context)
def getParents(self):
"""Returns a list starting with the object's parent followed by
each of its parents.
Raises a TypeError if the object is not connected to a containment
root.
>>> root = Location()
>>> zope.interface.directlyProvides(root, IRoot)
>>> o1 = Location()
>>> o2 = Location()
>>> o1.__parent__ = root
>>> o2.__parent__ = o1
>>> LocationPhysicallyLocatable(o2).getParents() == [o1, root]
True
If the last parent is not an IRoot object, TypeError will be
raised as statet before.
>>> zope.interface.noLongerProvides(root, IRoot)
>>> LocationPhysicallyLocatable(o2).getParents()
Traceback (most recent call last):
...
TypeError: Not enough context information to get all parents
"""
# XXX Merge this implementation with getPath. This was refactored
# from zope.traversing.
parents = []
w = self.context
while 1:
w = w.__parent__
if w is None:
break
parents.append(w)
if parents and IRoot.providedBy(parents[-1]):
return parents
raise TypeError("Not enough context information to get all parents")
def getName(self):
"""Get a location name
See ILocationInfo
>>> o1 = Location(); o1.__name__ = u'o1'
>>> LocationPhysicallyLocatable(o1).getName()
u'o1'
"""
return self.context.__name__
def getNearestSite(self):
"""return the nearest site, see ILocationInfo
>>> o1 = Location()
>>> o1.__name__ = 'o1'
>>> LocationPhysicallyLocatable(o1).getNearestSite()
Traceback (most recent call last):
...
TypeError: Not enough context information to get all parents
>>> root = Location()
>>> zope.interface.directlyProvides(root, IRoot)
>>> o1 = Location()
>>> o1.__name__ = 'o1'
>>> o1.__parent__ = root
>>> LocationPhysicallyLocatable(o1).getNearestSite() is root
True
>>> zope.interface.directlyProvides(
... o1, zope.component.interfaces.ISite)
>>> LocationPhysicallyLocatable(o1).getNearestSite() is o1
True
>>> o2 = Location()
>>> o2.__parent__ = o1
>>> LocationPhysicallyLocatable(o2).getNearestSite() is o1
True
"""
if zope.component.interfaces.ISite.providedBy(self.context):
return self.context
for parent in self.getParents():
if zope.component.interfaces.ISite.providedBy(parent):
return parent
return self.getRoot()
class RootPhysicallyLocatable(object):
"""Provide location information for the root object
>>> from zope.interface.verify import verifyObject
>>> info = RootPhysicallyLocatable(None)
>>> verifyObject(ILocationInfo, info)
True
This adapter is very simple, because there's no places to search
for parents and nearest sites, so we are only working with context
object, knowing that its the root object already.
"""
zope.component.adapts(IRoot)
zope.interface.implements(ILocationInfo)
def __init__(self, context):
self.context = context
def getRoot(self):
"""See ILocationInfo
No need to search for root when our context is already root :)
>>> o1 = object()
>>> RootPhysicallyLocatable(o1).getRoot() is o1
True
"""
return self.context
def getPath(self):
"""See ILocationInfo
Root object is at the top of the tree, so always return ``/``.
>>> o1 = object()
>>> RootPhysicallyLocatable(o1).getPath()
u'/'
"""
return u'/'
def getName(self):
"""See ILocationInfo
Always return empty unicode string for the root object
>>> o1 = object()
>>> RootPhysicallyLocatable(o1).getName()
u''
"""
return u''
def getParent(self):
"""Returns the container the object was traversed via.
Returns None if the object is a containment root.
Raises TypeError if the object doesn't have enough context to get the
parent.
>>> o1 = object()
>>> RootPhysicallyLocatable(o1).getParent()
"""
return None
def getParents(self):
"""See ILocationInfo
There's no parents for the root object, return empty list.
>>> o1 = object()
>>> RootPhysicallyLocatable(o1).getParents()
[]
"""
return []
def getNearestSite(self):
"""See ILocationInfo
Return object itself as the nearest site, because there's no
other place to look for. It's also usual that the root is the
site as well.
>>> o1 = object()
>>> RootPhysicallyLocatable(o1).getNearestSite() is o1
True
"""
return self.context
|