Skip to content

Commit

Permalink
Lint the code
Browse files Browse the repository at this point in the history
* Add support for Python 3.9.
  • Loading branch information
Michael Howitz committed Feb 24, 2021
1 parent c2f9dbc commit 40db7be
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 41 deletions.
5 changes: 3 additions & 2 deletions CHANGES.rst
Expand Up @@ -5,7 +5,8 @@ Changelog
2.3 (unreleased)
----------------

- Nothing changed yet.
- Add support for Python 3.9.
[icemac]


2.2 (2019-10-16)
Expand All @@ -30,7 +31,7 @@ Changelog
2.0 (2018-03-22)
----------------

* Added support for Python 3.5, 3.6, 3.7, PyPy2 and PyPy3.
* Add support for Python 3.5, 3.6, 3.7, PyPy2 and PyPy3.
[icemac]


Expand Down
12 changes: 7 additions & 5 deletions setup.py
Expand Up @@ -9,6 +9,7 @@ def read(name):
with open(name) as f:
return f.read()


setup(
name="z3c.caching",
version=version,
Expand All @@ -28,6 +29,7 @@ def read(name):
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"Topic :: Software Development :: Libraries :: Python Modules",
Expand All @@ -44,15 +46,15 @@ def read(name):
zip_safe=False,
install_requires=[
"setuptools",
"zope.interface>=3.8.0",
"zope.browser",
"zope.component",
"zope.event",
"zope.interface>=3.8.0",
"zope.schema",
"zope.lifecycleevent",
"zope.browser",
],
extras_require={
"zcml": ("zope.configuration", )
"zcml": ("zope.configuration", ),
"test": ("zope.configuration", ),
},
tests_require="nose >=0.10.0b1",
test_suite="nose.collector",
)
2 changes: 1 addition & 1 deletion src/z3c/__init__.py
@@ -1 +1 @@
__import__('pkg_resources').declare_namespace(__name__)
__import__('pkg_resources').declare_namespace(__name__) # pragma: no cover
1 change: 0 additions & 1 deletion src/z3c/caching/__init__.py
@@ -1 +0,0 @@

20 changes: 13 additions & 7 deletions src/z3c/caching/interfaces.py
Expand Up @@ -2,6 +2,7 @@
from zope.interface.interfaces import IObjectEvent
from zope import schema


class IRulesetRegistry(Interface):

def register(obj, rule):
Expand Down Expand Up @@ -42,21 +43,23 @@ def enumerateTypes():
"""

explicit = schema.Bool(
title=u"Explicit mode",
description=u"If true, ruleset types must be declared before being used.",
required=True,
default=False
)
title=u"Explicit mode",
description=(u"If true, ruleset types must be declared before being"
u" used."),
required=True,
default=False)


class IRulesetType(Interface):
"""A ruleset type. The name can be used in a <cache:ruleset /> directive.
The title and description are used for UI support.
"""

name = schema.DottedName(title=u"Ruleset name")
title = schema.TextLine(title=u"Title")
name = schema.DottedName(title=u"Ruleset name")
title = schema.TextLine(title=u"Title")
description = schema.TextLine(title=u"Description", required=False)


class ILastModified(Interface):
"""An abstraction to help obtain a last-modified date for a published
resource.
Expand All @@ -74,6 +77,7 @@ def __call__():
May return None if the last modified date cannot be determined.
"""


class IPurgeEvent(IObjectEvent):
"""Event which can be fired to purge a particular object.
Expand All @@ -85,6 +89,7 @@ class IPurgeEvent(IObjectEvent):
object will only be purged once.
"""


class IPurgeable(Interface):
"""Marker interface for content which should be purged when modified or
removed.
Expand All @@ -94,6 +99,7 @@ class IPurgeable(Interface):
automatically purged.
"""


class IPurgePaths(Interface):
"""Return paths to send as PURGE requests for a given object.
Expand Down
1 change: 1 addition & 0 deletions src/z3c/caching/lastmodified.py
Expand Up @@ -4,6 +4,7 @@
from zope.browser.interfaces import IView
from z3c.caching.interfaces import ILastModified


@implementer(ILastModified)
@adapter(IView)
def viewDelegateLastModified(view):
Expand Down
2 changes: 2 additions & 0 deletions src/z3c/caching/purge.py
Expand Up @@ -8,6 +8,7 @@
from z3c.caching.interfaces import IPurgeable
from z3c.caching.interfaces import IPurgeEvent


@implementer(IPurgeEvent)
class Purge(object):
"""Event implementation.
Expand All @@ -31,6 +32,7 @@ def __init__(self, object):
def purgeOnModified(object, event):
notify(Purge(object))


@adapter(IPurgeable, IObjectMovedEvent)
def purgeOnMovedOrRemoved(object, event):
# Don't purge when added
Expand Down
36 changes: 28 additions & 8 deletions src/z3c/caching/registry.py
Expand Up @@ -12,26 +12,30 @@

from zope.interface import implementer, Interface, Attribute

from zope.component import adapts, queryUtility, getUtilitiesFor, getGlobalSiteManager
from zope.component import adapts, queryUtility, getUtilitiesFor
from zope.component import getGlobalSiteManager
from zope.interface.interfaces import IComponents

from z3c.caching.interfaces import IRulesetRegistry
from z3c.caching.interfaces import IRulesetType


class ICacheRule(Interface):
"""Represents the cache rule applied to an object.
This is strictly an implementation detail of IRulesetRegistry.
"""

id = Attribute("The identifier of this cache rule")


@implementer(ICacheRule)
class CacheRule(object):
__slots__ = ("id")

def __init__(self, identifier):
self.id = identifier


@implementer(IRulesetType)
class RulesetType(object):
__slots__ = ('name', 'title', 'description',)
Expand All @@ -41,6 +45,7 @@ def __init__(self, name, title, description):
self.title = title
self.description = description


def get_context_to_cacherule_adapter_factory(rule):
"""Given a cache rule return an adapter factory which expects an object
but only returns the pre-specified cache rule."""
Expand All @@ -49,6 +54,7 @@ def CacheRuleFactory(context):
CacheRuleFactory.id = rule
return CacheRuleFactory


@implementer(IRulesetRegistry)
class RulesetRegistry(object):

Expand All @@ -58,21 +64,26 @@ def __init__(self, registry):
self.registry = registry

def register(self, obj, rule):
rule = str(rule) # We only want ascii, tyvm
rule = str(rule) # We only want ascii, tyvm

if self.explicit and queryUtility(IRulesetType, rule) is None:
raise LookupError("Explicit mode set and ruleset %s not found" % rule)
raise LookupError(
"Explicit mode set and ruleset %s not found" %
rule)

factory = get_context_to_cacherule_adapter_factory(rule)
existing = self.directLookup(obj)
if existing is None:
# Only register if we haven't got this one already
self.registry.registerAdapter(factory, provided=ICacheRule, required=(obj,))
self.registry.registerAdapter(
factory, provided=ICacheRule, required=(obj,))
else:
warnings.warn("Ignoring attempted to register caching rule %s for %s. %s is already registered." % (rule, repr(obj), existing))
warnings.warn(
"Ignoring attempted to register caching rule %s for %s."
" %s is already registered." %
(rule, repr(obj), existing))
return None


def unregister(self, obj):
self.registry.unregisterAdapter(provided=ICacheRule, required=(obj,))
return None
Expand All @@ -82,15 +93,15 @@ def clear(self):
# storage will be changing size
for rule in list(self.registry.registeredAdapters()):
if rule.provided != ICacheRule:
continue # Not our responsibility
continue # Not our responsibility
else:
self.registry.unregisterAdapter(factory=rule.factory,
provided=rule.provided,
required=rule.required)

for type_ in list(self.registry.registeredUtilities()):
if type_.provided != IRulesetType:
continue # Not our responsibility
continue # Not our responsibility
else:
self.registry.unregisterUtility(component=type_.component,
provided=IRulesetType,
Expand All @@ -117,6 +128,7 @@ def enumerateTypes(self):

def _get_explicit(self):
return getattr(self.registry, '_z3c_caching_explicit', False)

def _set_explicit(self, value):
setattr(self.registry, '_z3c_caching_explicit', value)
explicit = property(_get_explicit, _set_explicit)
Expand All @@ -132,46 +144,54 @@ def directLookup(self, obj):
return rule.factory(None).id
return None


def getGlobalRulesetRegistry():
return IRulesetRegistry(getGlobalSiteManager(), None)

# Convenience API


def register(obj, rule):
registry = getGlobalRulesetRegistry()
if registry is None:
raise LookupError("Global registry initialised")
return registry.register(obj, rule)


def unregister(obj):
registry = getGlobalRulesetRegistry()
if registry is None:
raise LookupError("Global registry initialised")
return registry.unregister(obj)


def lookup(obj):
registry = getGlobalRulesetRegistry()
if registry is None:
return None
return registry.lookup(obj)


def enumerateTypes():
registry = getGlobalRulesetRegistry()
if registry is None:
raise LookupError("Global registry initialised")
return registry.enumerateTypes()


def declareType(name, title, description):
registry = getGlobalRulesetRegistry()
if registry is None:
raise LookupError("Global registry initialised")
registry.declareType(name, title, description)


def setExplicitMode(mode=True):
registry = getGlobalRulesetRegistry()
if registry is None:
raise LookupError("Global registry initialised")
registry.explicit = mode


__all__ = ['getGlobalRulesetRegistry', 'register', 'unregister', 'lookup',
'enumerateTypes', 'declareType', 'setExplicitMode']
1 change: 0 additions & 1 deletion src/z3c/caching/tests/__init__.py
@@ -1 +0,0 @@

32 changes: 16 additions & 16 deletions src/z3c/caching/zcml.py
Expand Up @@ -8,16 +8,16 @@

class IRuleset(Interface):
for_ = GlobalObject(
title=u"Object to be configured",
description=u"The object for which the cache ruleset is to be defined",
default=None,
required=True)
title=u"Object to be configured",
description=u"The object for which the cache ruleset is to be defined",
default=None,
required=True)

ruleset = TextLine(
title=u"ruleset",
description=u"The id of the cache ruleset to use",
default=None,
required=True)
title=u"ruleset",
description=u"The id of the cache ruleset to use",
default=None,
required=True)


def rulesetType(_context, name, title, description=u""):
Expand All @@ -27,19 +27,19 @@ def rulesetType(_context, name, title, description=u""):
# registered, a big number is called for to ensure that we occur after
# z3c.caching has been configured.
_context.action(
discriminator=("declareCacheRuleSetType", name),
callable=declareType,
args=(name, title, description,),
order=ORDER)
discriminator=("declareCacheRuleSetType", name),
callable=declareType,
args=(name, title, description,),
order=ORDER)


def ruleset(_context, for_, ruleset):
# We need to ensure this has a higher order than rulesetType() always
_context.action(
discriminator=("registerCacheRule", for_),
callable=register,
args=(for_, ruleset,),
order=ORDER + 1)
discriminator=("registerCacheRule", for_),
callable=register,
args=(for_, ruleset,),
order=ORDER + 1)


# Handlers
Expand Down

0 comments on commit 40db7be

Please sign in to comment.