[Checkins] SVN: grok/branches/grok-martian/src/grok/ Check this in
*with* a test, as I should've done in the first place.
Martijn Faassen
faassen at infrae.com
Wed Jun 20 16:51:00 EDT 2007
Log message for revision 76863:
Check this in *with* a test, as I should've done in the first place.
Changed:
U grok/branches/grok-martian/src/grok/_grok.py
A grok/branches/grok-martian/src/grok/tests/grokker/grokcomponent.py
-=-
Modified: grok/branches/grok-martian/src/grok/_grok.py
===================================================================
--- grok/branches/grok-martian/src/grok/_grok.py 2007-06-20 20:17:58 UTC (rev 76862)
+++ grok/branches/grok-martian/src/grok/_grok.py 2007-06-20 20:50:59 UTC (rev 76863)
@@ -81,8 +81,11 @@
def grok_component(name, component,
context=None, module_info=None, templates=None):
+ #import pdb; pdb.set_trace()
return the_multi_grokker.grok(name, component,
- context, module_info, templates)
+ context=context,
+ module_info=module_info,
+ templates=templates)
def prepare_grok(name, module, kw):
module_info = scan.module_info_from_module(module)
Added: grok/branches/grok-martian/src/grok/tests/grokker/grokcomponent.py
===================================================================
--- grok/branches/grok-martian/src/grok/tests/grokker/grokcomponent.py (rev 0)
+++ grok/branches/grok-martian/src/grok/tests/grokker/grokcomponent.py 2007-06-20 20:50:59 UTC (rev 76863)
@@ -0,0 +1,61 @@
+"""
+
+Let's first grok the meta module to define some basic grokkers::
+
+ >>> import grok
+ >>> grok.grok('grok.meta')
+
+It is possible to grok an individual component. Let's define an adapter::
+
+ >>> from zope.interface import Interface
+ >>> class IMyInterface(Interface):
+ ... pass
+ >>> class SomeClass(object):
+ ... pass
+ >>> class MyAdapter(grok.Adapter):
+ ... grok.provides(IMyInterface)
+ ... grok.context(SomeClass)
+
+To grok this adapter, you can simply write this::
+
+ >>> grok.grok_component('MyAdapter', MyAdapter)
+ True
+
+We can now use the adapter::
+
+ >>> instance = SomeClass()
+ >>> adapter = IMyInterface(instance)
+ >>> isinstance(adapter, MyAdapter)
+ True
+
+We can use grok_component with only two arguments because we know the
+adapter grokker is not looking for more. Sometimes we need to supply
+an extra argument however::
+
+ >>> class ISecondInterface(Interface):
+ ... pass
+ >>> class SecondAdapter(grok.Adapter):
+ ... grok.provides(ISecondInterface)
+
+This adapter does not supply its own context. Trying to do what we did
+before will therefore fail::
+
+ >>> grok.grok_component('SecondAdapter', SecondAdapter)
+ Traceback (most recent call last):
+ ...
+ GrokError: No module-level context for <class 'grok.tests.grokker.grokcomponent.SecondAdapter'>, please use grok.context.
+
+So we need to supply the context ourselves::
+
+ >>> grok.grok_component('SecondAdapter', SecondAdapter, context=SomeClass)
+ True
+
+Now we can use the SecondAdapter as well::
+
+ >>> adapter = ISecondInterface(instance)
+ >>> isinstance(adapter, SecondAdapter)
+ True
+
+The next optional argument is module_info and the final argument is
+templates.
+"""
More information about the Checkins
mailing list