relation: Python3 fixes

This commit is contained in:
Helmut Merz 2024-09-23 12:17:52 +02:00
parent f9a3326ec7
commit 3ec90f4b66
3 changed files with 20 additions and 39 deletions

View file

@ -1,8 +1,6 @@
Quickstart Instructions
=======================
($Id$)
In the ++etc++/default folder of your Zope 3 site create a Unique Id Utility
and a relation registry.
@ -87,15 +85,16 @@ reference these objects via IntIds later; the __parent__ and __name__
attributes are also needed later when we send an IObjectRemovedEvent event):
>>> from persistent import Persistent
>>> from zope.interface import implements
>>> from zope.interface import implementer
>>> from cybertools.relation.interfaces import IRelatable
>>> class Person(Persistent):
... __name__ = __parent__ = None
... implements(IRelatable)
>>> Person = implementer(IRelatable)(Person)
>>> class City(Persistent):
... implements(IRelatable)
... pass
>>> City = implementer(IRelatable)(City)
>>> clark = Person()
>>> kirk = Person()
@ -396,10 +395,8 @@ We also need a class for the predicate objects that will be used for
the constructor of the NamedPredicateRelation class:
>>> from cybertools.relation.interfaces import IPredicate
>>> from zope.interface import implements
>>> class Predicate(object):
... implements(IPredicate)
... def __init__(self, name):
... self.name = name
... self.forClass = None
@ -407,6 +404,7 @@ the constructor of the NamedPredicateRelation class:
... if self.forClass is not None:
... return self.forClass(self, None, None).getPredicateName()
... return self.name
>>> Predicate = implementer(IPredicate)(Predicate)
We can now create a predicate with the name '_lives in_' (that may replace
our LivesIn relation class from above) and use for registration:

View file

@ -1,23 +1,6 @@
#
# Copyright (c) 2013 Helmut Merz helmutm@cy55.de
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# cybertools.relation.registry
"""
Implementation of the utilities needed for the relations package.
""" Implementation of the utilities needed for the relations package.
"""
from logging import getLogger
@ -26,9 +9,9 @@ from persistent import Persistent
from persistent.interfaces import IPersistent
from zope import component
from zope.component import adapts
from zope.interface import Interface, Attribute, implements
from zope.app.catalog.catalog import Catalog, ResultSet
from zope.app.catalog.field import FieldIndex
from zope.interface import Interface, Attribute, implementer
from zope.catalog.catalog import Catalog, ResultSet
from zope.catalog.field import FieldIndex
from zope.intid.interfaces import IIntIds
from zope.location.interfaces import ILocation
from zope.event import notify
@ -36,18 +19,18 @@ from zope.interface.interfaces import ObjectEvent
from zope.security.proxy import removeSecurityProxy
from zope.traversing.api import getName, getParent
from interfaces import IRelationRegistry, IRelationInvalidatedEvent, IRelation
from cybertools.relation.interfaces import IRelationRegistry
from cybertools.relation.interfaces import IRelationInvalidatedEvent, IRelation
logger = getLogger('cybertools.relation.registry')
@implementer(IRelationRegistry)
class DummyRelationRegistry(object):
""" Dummy implementation for demonstration and test purposes.
"""
implements(IRelationRegistry)
def __init__(self):
self.relations = []
self.objects = []
@ -112,12 +95,11 @@ class DummyRelationRegistry(object):
return result
@implementer(IRelationRegistry)
class RelationRegistry(Catalog):
""" Local utility for registering (cataloguing) and searching relations.
"""
implements(IRelationRegistry)
relations = None
def __init__(self, *args, **kw):
@ -202,12 +184,12 @@ class IIndexableRelation(Interface):
"""
@implementer(IIndexableRelation)
class IndexableRelationAdapter(object):
""" Adapter for providing the attributes needed for indexing
relation objects.
"""
implements(IIndexableRelation)
adapts(IRelation)
def __init__(self, context):
@ -296,8 +278,9 @@ def setRelationSingle(relation, forSecond=True):
# events and handlers
@implementer(IRelationInvalidatedEvent)
class RelationInvalidatedEvent(ObjectEvent):
implements(IRelationInvalidatedEvent)
pass
def invalidateRelations(context, event):

View file

@ -44,15 +44,15 @@ class TestRelation(unittest.TestCase):
def testInterfaces(self):
verifyClass(IPredicate, Relation)
verifyClass(IRelation, Relation)
self.assert_(IDyadicRelation.providedBy(DyadicRelation(None, None)),
self.assertTrue(IDyadicRelation.providedBy(DyadicRelation(None, None)),
'Interface IDyadicRelation is not implemented by class DyadicRelation.')
verifyClass(IDyadicRelation, DyadicRelation)
self.assert_(ITriadicRelation.providedBy(TriadicRelation(None, None, None)),
self.assertTrue(ITriadicRelation.providedBy(TriadicRelation(None, None, None)),
'Interface ITriadicRelation is not implemented by class TriadicRelation.')
verifyClass(ITriadicRelation, TriadicRelation)
# avoid dependency on import:
from cybertools.relation.registry import RelationRegistry
self.assert_(IRelationRegistry.providedBy(RelationRegistry()),
self.assertTrue(IRelationRegistry.providedBy(RelationRegistry()),
'Interface IRelationRegistry is not implemented by class RelationRegistry.')
verifyClass(IRelationRegistry, RelationRegistry)