Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/pyramid/location.py : 15%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1##############################################################################
2#
3# Copyright (c) 2003 Zope Corporation and Contributors.
4# All Rights Reserved.
5#
6# This software is subject to the provisions of the Zope Public License,
7# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
8# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11# FOR A PARTICULAR PURPOSE.
12#
13##############################################################################
16def inside(resource1, resource2):
17 """Is ``resource1`` 'inside' ``resource2``? Return ``True`` if so, else
18 ``False``.
20 ``resource1`` is 'inside' ``resource2`` if ``resource2`` is a
21 :term:`lineage` ancestor of ``resource1``. It is a lineage ancestor
22 if its parent (or one of its parent's parents, etc.) is an
23 ancestor.
24 """
25 while resource1 is not None:
26 if resource1 is resource2:
27 return True
28 resource1 = resource1.__parent__
30 return False
33def lineage(resource):
34 """
35 Return a generator representing the :term:`lineage` of the
36 :term:`resource` object implied by the ``resource`` argument. The
37 generator first returns ``resource`` unconditionally. Then, if
38 ``resource`` supplies a ``__parent__`` attribute, return the resource
39 represented by ``resource.__parent__``. If *that* resource has a
40 ``__parent__`` attribute, return that resource's parent, and so on,
41 until the resource being inspected either has no ``__parent__``
42 attribute or which has a ``__parent__`` attribute of ``None``.
43 For example, if the resource tree is::
45 thing1 = Thing()
46 thing2 = Thing()
47 thing2.__parent__ = thing1
49 Calling ``lineage(thing2)`` will return a generator. When we turn
50 it into a list, we will get::
52 list(lineage(thing2))
53 [ <Thing object at thing2>, <Thing object at thing1> ]
54 """
55 while resource is not None:
56 yield resource
57 # The common case is that the AttributeError exception below
58 # is exceptional as long as the developer is a "good citizen"
59 # who has a root object with a __parent__ of None. Using an
60 # exception here instead of a getattr with a default is an
61 # important micro-optimization, because this function is
62 # called in any non-trivial application over and over again to
63 # generate URLs and paths.
64 try:
65 resource = resource.__parent__
66 except AttributeError:
67 resource = None