Skip to content

Commit

Permalink
Py3 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ale-rt committed Mar 31, 2018
1 parent c1f920b commit e13249e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
11 changes: 6 additions & 5 deletions src/zc/relationship/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
##############################################################################
# a common case transitive queries factory

@interface.implementer(interfaces.ITransitiveQueriesFactory)
class TransposingTransitiveQueriesFactory(persistent.Persistent):
interface.implements(interfaces.ITransitiveQueriesFactory)

def __init__(self, name1, name2):
self.names = [name1, name2] # a list so we can use index
Expand Down Expand Up @@ -105,11 +105,12 @@ def resolveToken(token, index, cache):
##############################################################################
# the relationship index

@interface.implementer_only(
interfaces.IIndex, interface.implementedBy(persistent.Persistent),
interface.implementedBy(zope.app.container.contained.Contained)
)
class Index(zc.relation.catalog.Catalog,
zope.app.container.contained.Contained):
interface.implementsOnly(
interfaces.IIndex, interface.implementedBy(persistent.Persistent),
interface.implementedBy(zope.app.container.contained.Contained))

def __init__(self, attrs, defaultTransitiveQueriesFactory=None,
dumpRel=generateToken, loadRel=resolveToken,
Expand Down Expand Up @@ -158,7 +159,7 @@ def apply(self, query):
# there are two kinds of queries: values and relationships.
if len(query) != 1:
raise ValueError('one key in the primary query dictionary')
(searchType, query) = query.items()[0]
(searchType, query) = list(query.items())[0]
if searchType=='relationships':
relTools = self.getRelationModuleTools()
if relTools['TreeSet'].__name__[:2] not in ('IF', 'LF'):
Expand Down
24 changes: 16 additions & 8 deletions src/zc/relationship/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
$Id$
"""
import random
import six

import persistent
from zope import interface
Expand All @@ -41,9 +42,16 @@ class RelationshipBase(
zc.listcontainer.Contained):
pass

try:
apply
except NameError:
# PY3
def apply(func, *args, **kw):
return func(*args, **kw)

@interface.implementer(interfaces.IRelationship)
class ImmutableRelationship(RelationshipBase):
interface.implements(interfaces.IRelationship)


_marker = __name__ = __parent__ = None

def __init__(self, sources, targets):
Expand All @@ -61,8 +69,8 @@ def targets(self):
def __repr__(self):
return '<Relationship from %r to %r>' % (self.sources, self.targets)

@interface.implementer(interfaces.IMutableRelationship)
class Relationship(ImmutableRelationship):
interface.implements(interfaces.IMutableRelationship)

@apply
def sources():
Expand All @@ -89,8 +97,8 @@ def set(self, value):
# some small conveniences; maybe overkill, but I wanted some for a client
# package.

@interface.implementer(interfaces.IOneToOneRelationship)
class OneToOneRelationship(ImmutableRelationship):
interface.implements(interfaces.IOneToOneRelationship)

def __init__(self, source, target):
super(OneToOneRelationship, self).__init__((source,), (target,))
Expand All @@ -117,8 +125,8 @@ def set(self, value):
self.__parent__.reindex(self)
return property(get, set)

@interface.implementer(interfaces.IOneToManyRelationship)
class OneToManyRelationship(ImmutableRelationship):
interface.implements(interfaces.IOneToManyRelationship)

def __init__(self, source, targets):
super(OneToManyRelationship, self).__init__((source,), targets)
Expand All @@ -145,8 +153,8 @@ def set(self, value):
self.__parent__.reindex(self)
return property(get, set)

@interface.implementer(interfaces.IManyToOneRelationship)
class ManyToOneRelationship(ImmutableRelationship):
interface.implements(interfaces.IManyToOneRelationship)

def __init__(self, sources, target):
super(ManyToOneRelationship, self).__init__(sources, (target,))
Expand Down Expand Up @@ -188,7 +196,7 @@ def __call__(self, relchain, query, index, cache):
def minDepthFilter(depth):
if depth is None:
return None
if not isinstance(depth, (int, long)) or depth < 1:
if not isinstance(depth, six.integer_types) or depth < 1:
raise ValueError('invalid minDepth', depth)
return lambda relchain, query, index, cache: len(relchain) >= depth

Expand All @@ -213,7 +221,7 @@ def __init__(self,
target['load'] = loadTarget
if targetFamily is not None:
target['btree'] = targetFamily

ix = index.Index(
(source, target),
index.TransposingTransitiveQueriesFactory('source', 'target'),
Expand Down

0 comments on commit e13249e

Please sign in to comment.