[Checkins] SVN: grokcore.component/branches/sylvain-subscribers/src/grokcore/component/ PEP 8 format. Add component for subscribers. Not yet tested.
Sylvain Viollon
sylvain at infrae.com
Wed Jan 5 10:30:19 EST 2011
Log message for revision 119390:
PEP 8 format. Add component for subscribers. Not yet tested.
Changed:
U grokcore.component/branches/sylvain-subscribers/src/grokcore/component/__init__.py
U grokcore.component/branches/sylvain-subscribers/src/grokcore/component/components.py
U grokcore.component/branches/sylvain-subscribers/src/grokcore/component/interfaces.py
U grokcore.component/branches/sylvain-subscribers/src/grokcore/component/meta.py
U grokcore.component/branches/sylvain-subscribers/src/grokcore/component/util.py
-=-
Modified: grokcore.component/branches/sylvain-subscribers/src/grokcore/component/__init__.py
===================================================================
--- grokcore.component/branches/sylvain-subscribers/src/grokcore/component/__init__.py 2011-01-05 15:26:09 UTC (rev 119389)
+++ grokcore.component/branches/sylvain-subscribers/src/grokcore/component/__init__.py 2011-01-05 15:30:18 UTC (rev 119390)
@@ -22,7 +22,8 @@
from martian import ClassGrokker, InstanceGrokker, GlobalGrokker
from grokcore.component.components import (
- Adapter, GlobalUtility, MultiAdapter, Context)
+ Adapter, GlobalUtility, MultiAdapter, Context, Subscriber,
+ MultiSubscriber)
from grokcore.component.directive import (
context, description, direct, name, order, path, provides, title,
@@ -31,6 +32,9 @@
from grokcore.component.decorators import (
subscribe, adapter, implementer, provider)
+from grokcore.component.util import (
+ querySubscribers, queryOrderedSubscribers)
+
# Import this module so that it's available as soon as you import the
# 'grokcore.component' package. Useful for tests and interpreter examples.
import grokcore.component.testing
Modified: grokcore.component/branches/sylvain-subscribers/src/grokcore/component/components.py
===================================================================
--- grokcore.component/branches/sylvain-subscribers/src/grokcore/component/components.py 2011-01-05 15:26:09 UTC (rev 119389)
+++ grokcore.component/branches/sylvain-subscribers/src/grokcore/component/components.py 2011-01-05 15:30:18 UTC (rev 119390)
@@ -17,6 +17,7 @@
from grokcore.component.interfaces import IContext
+
class Adapter(object):
"""Base class to define an adapter.
@@ -25,11 +26,13 @@
.. attribute:: context
The adapted object.
-
+
"""
+
def __init__(self, context):
self.context = context
+
class GlobalUtility(object):
"""Base class to define a globally registered utility.
@@ -37,10 +40,25 @@
"""
pass
+
class MultiAdapter(object):
"""Base class to define a Multi Adapter.
"""
pass
+
+class Subscriber(object):
+ """Base class for a subscriber.
+ """
+
+ def __init__(self, context):
+ self.context = context
+
+
+class MultiSubscriber(object):
+ """Base class for a multi-subcriber.
+ """
+
+
class Context(object):
implements(IContext)
Modified: grokcore.component/branches/sylvain-subscribers/src/grokcore/component/interfaces.py
===================================================================
--- grokcore.component/branches/sylvain-subscribers/src/grokcore/component/interfaces.py 2011-01-05 15:26:09 UTC (rev 119389)
+++ grokcore.component/branches/sylvain-subscribers/src/grokcore/component/interfaces.py 2011-01-05 15:30:18 UTC (rev 119390)
@@ -40,10 +40,12 @@
GlobalGrokker = Attribute("Base class to define a module grokker.")
Context = Attribute("Base class for automatically associated contexts.")
-
+
Adapter = Attribute("Base class for adapters.")
MultiAdapter = Attribute("Base class for multi-adapters.")
GlobalUtility = Attribute("Base class for global utilities.")
+ Subscriber = Attribute("Base class for subscribers.")
+ MultiSubscriber = Attribute("Base class for multi-subscribers.")
class IDirectives(Interface):
@@ -57,13 +59,13 @@
def implements(*interfaces):
"""Declare that a class implements the given interfaces."""
-
+
def implementsOnly(*interfaces):
"""Declare that a class implements only the given interfaces.
-
+
Interfaces implemented by base classes are explicitly not inherited.
"""
-
+
def classProvides(*interfaces):
"""Declare that a class (as opposed to instances of the class)
directly provides the given interfaces.
@@ -165,7 +167,7 @@
"""Describes that a function that's used as an adapter
implements an interface or a number of interfaces.
"""
-
+
def provider(*interfaces):
"""Describes that a function directly provides an interface or a
number of interfaces.
@@ -195,3 +197,6 @@
class IGrokcoreComponentAPI(IBaseClasses, IDirectives, IDecorators,
IGrokErrors, IMartianAPI):
"""grokcore.component's public API."""
+
+ querySubscribers = Attribute("Function to query subscribers.")
+ queryOrderedSubscribers = Attribute("Function to query and order subscribers.")
Modified: grokcore.component/branches/sylvain-subscribers/src/grokcore/component/meta.py
===================================================================
--- grokcore.component/branches/sylvain-subscribers/src/grokcore/component/meta.py 2011-01-05 15:26:09 UTC (rev 119389)
+++ grokcore.component/branches/sylvain-subscribers/src/grokcore/component/meta.py 2011-01-05 15:30:18 UTC (rev 119390)
@@ -30,6 +30,7 @@
return list(interface.providedBy(component))[0]
return _provides(component)
+
class AdapterGrokker(martian.ClassGrokker):
martian.component(grokcore.component.Adapter)
martian.directive(grokcore.component.context)
@@ -44,17 +45,18 @@
)
return True
+
class MultiAdapterGrokker(martian.ClassGrokker):
martian.component(grokcore.component.MultiAdapter)
martian.directive(grokcore.component.provides)
martian.directive(grokcore.component.name)
def execute(self, factory, config, provides, name, **kw):
- if component.adaptedBy(factory) is None:
+ for_ = component.adaptedBy(factory)
+ if for_ is None:
raise GrokError("%r must specify which contexts it adapts "
"(use the 'adapts' directive to specify)."
% factory, factory)
- for_ = component.adaptedBy(factory)
config.action(
discriminator=('adapter', for_, provides, name),
@@ -63,6 +65,42 @@
)
return True
+
+class SubscriberGrokker(martian.ClassGrokker):
+ martian.component(grokcore.component.Subscriber)
+ martian.directive(grokcore.component.context)
+ martian.directive(grokcore.component.provides)
+ martian.directive(grokcore.component.name)
+
+ def execute(self, factory, config, context, provides, name, **kw):
+ config.action(
+ discriminator=None,
+ callable=component.provideSubscriptionAdapter,
+ args=(factory, (context,), provides),
+ )
+ return True
+
+
+class MultiSubscriberGrokker(martian.ClassGrokker):
+ martian.component(grokcore.component.MultiSubscriber)
+ martian.directive(grokcore.component.provides)
+ martian.directive(grokcore.component.name)
+
+ def execute(self, factory, config, provides, name, **kw):
+ adapts = component.adaptedBy(factory)
+ if adapts is None:
+ raise GrokError("%r must specify which contexts it adapts "
+ "(use the 'adapts' directive to specify)."
+ % factory, factory)
+
+ config.action(
+ discriminator=None,
+ callable=component.provideAdapter,
+ args=(factory, adapts, provides),
+ )
+ return True
+
+
class GlobalUtilityGrokker(martian.ClassGrokker):
martian.component(grokcore.component.GlobalUtility)
@@ -86,6 +124,7 @@
)
return True
+
class AdapterDecoratorGrokker(martian.GlobalGrokker):
def grok(self, name, module, module_info, config, **kw):
@@ -135,6 +174,7 @@
return True
+
class GlobalAdapterDirectiveGrokker(martian.GlobalGrokker):
def grok(self, name, module, module_info, config, **kw):
@@ -158,8 +198,9 @@
return True
-class SubscriberGrokker(martian.GlobalGrokker):
+class SubscriberDirectiveGrokker(martian.GlobalGrokker):
+
def grok(self, name, module, module_info, config, **kw):
subscribers = module_info.getAnnotation('grok.subscribers', [])
Modified: grokcore.component/branches/sylvain-subscribers/src/grokcore/component/util.py
===================================================================
--- grokcore.component/branches/sylvain-subscribers/src/grokcore/component/util.py 2011-01-05 15:26:09 UTC (rev 119389)
+++ grokcore.component/branches/sylvain-subscribers/src/grokcore/component/util.py 2011-01-05 15:30:18 UTC (rev 119390)
@@ -14,8 +14,8 @@
"""Grok utility functions.
"""
from grokcore.component import directive
+from zope import component
-
def _sort_key(component):
# If components have a grok.order directive, sort by that.
explicit_order, implicit_order = directive.order.bind().get(component)
@@ -27,3 +27,9 @@
def sort_components(components):
return sorted(components, key=_sort_key)
+
+
+def queryOrderedSubscribers(objects, interface):
+ return sort_components(component.subscribers(objects, interface))
+
+querySubscribers = component.subscribers
More information about the checkins
mailing list