/usr/share/pyshared/zope/app/generations/utility.py is in python-zope.app.generations 3.6.1-0ubuntu1.
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 | ##############################################################################
#
# Copyright (c) 2004 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.
#
##############################################################################
"""Utility functions for evolving database generations.
$Id: utility.py 124134 2012-01-23 15:20:44Z menesis $
"""
__docformat__ = "reStructuredText"
import zope.app.publication.zopepublication
def findObjectsMatching(root, condition):
"""Find all objects in the root that match the condition.
The condition is a callable Python object that takes an object as an
argument and must return `True` or `False`.
All sub-objects of the root will also be searched recursively. All mapping
objects providing `values()` are supported.
Example:
>>> class A(dict):
... def __init__(self, name):
... self.name = name
>>> class B(dict):
... def __init__(self, name):
... self.name = name
>>> class C(dict):
... def __init__(self, name):
... self.name = name
>>> tree = A('a1')
>>> tree['b1'] = B('b1')
>>> tree['c1'] = C('c1')
>>> tree['b1']['a2'] = A('a2')
>>> tree['b1']['b2'] = B('b2')
>>> tree['b1']['b2']['c2'] = C('c2')
>>> tree['b1']['b2']['a3'] = A('a3')
# Find all instances of class A
>>> matches = findObjectsMatching(tree, lambda x: isinstance(x, A))
>>> names = [x.name for x in matches]
>>> names.sort()
>>> names
['a1', 'a2', 'a3']
# Find all objects having a '2' in the name
>>> matches = findObjectsMatching(tree, lambda x: '2' in x.name)
>>> names = [x.name for x in matches]
>>> names.sort()
>>> names
['a2', 'b2', 'c2']
"""
if condition(root):
yield root
if hasattr(root, 'values'):
for subobj in root.values():
for match in findObjectsMatching(subobj, condition):
yield match
def findObjectsProviding(root, interface):
"""Find all objects in the root that provide the specified interface.
All sub-objects of the root will also be searched recursively.
Example:
>>> from zope.interface import Interface, implements
>>> class IA(Interface):
... pass
>>> class IB(Interface):
... pass
>>> class IC(IA):
... pass
>>> class A(dict):
... implements(IA)
... def __init__(self, name):
... self.name = name
>>> class B(dict):
... implements(IB)
... def __init__(self, name):
... self.name = name
>>> class C(dict):
... implements(IC)
... def __init__(self, name):
... self.name = name
>>> tree = A('a1')
>>> tree['b1'] = B('b1')
>>> tree['c1'] = C('c1')
>>> tree['b1']['a2'] = A('a2')
>>> tree['b1']['b2'] = B('b2')
>>> tree['b1']['b2']['c2'] = C('c2')
>>> tree['b1']['b2']['a3'] = A('a3')
# Find all objects that provide IB
>>> matches = findObjectsProviding(tree, IB)
>>> names = [x.name for x in matches]
>>> names.sort()
>>> names
['b1', 'b2']
# Find all objects that provide IA
>>> matches = findObjectsProviding(tree, IA)
>>> names = [x.name for x in matches]
>>> names.sort()
>>> names
['a1', 'a2', 'a3', 'c1', 'c2']
"""
for match in findObjectsMatching(root, interface.providedBy):
yield match
def getRootFolder(context):
"""Get the root folder of the ZODB.
We need some set up. Create a database:
>>> from ZODB.tests.util import DB
>>> from zope.app.generations.generations import Context
>>> db = DB()
>>> context = Context()
>>> context.connection = db.open()
>>> root = context.connection.root()
Add a root folder:
>>> from zope.site.folder import rootFolder
>>> from zope.app.publication.zopepublication import ZopePublication
>>> import transaction
>>> root[ZopePublication.root_name] = rootFolder()
>>> transaction.commit()
Now we can get the root folder using the function:
>>> getRootFolder(context) # doctest: +ELLIPSIS
<zope.site.folder.Folder object at ...>
We'd better clean up:
>>> context.connection.close()
>>> db.close()
"""
return context.connection.root().get(
zope.app.publication.zopepublication.ZopePublication.root_name, None)
|