[Checkins] SVN: grok/trunk/martian/src/martian/ Introduce
GlobalMartian.
Martijn Faassen
faassen at infrae.com
Tue May 1 17:16:41 EDT 2007
Log message for revision 74973:
Introduce GlobalMartian.
Changed:
U grok/trunk/martian/src/martian/README.txt
U grok/trunk/martian/src/martian/core.py
-=-
Modified: grok/trunk/martian/src/martian/README.txt
===================================================================
--- grok/trunk/martian/src/martian/README.txt 2007-05-01 20:37:22 UTC (rev 74972)
+++ grok/trunk/martian/src/martian/README.txt 2007-05-01 21:16:40 UTC (rev 74973)
@@ -661,6 +661,39 @@
GlobalMartian
-------------
+Sometimes you want to let a grok action happen for each module. The
+grok action could for instance read the globals of a module, or even
+static files associated with the module by name. Let's create a module
+with some global value::
+
+ >>> class g(FakeModule):
+ ... amount = 50
+ >>> g = fake_import(g)
+
+Now let's create a ``GlobalMartian`` that reads ``amount`` and stores
+it in the ``read_amount`` dictionary::
+
+ >>> read_amount = {}
+ >>> from martian import GlobalMartian
+ >>> class AmountMartian(GlobalMartian):
+ ... def grok(self, name, module):
+ ... read_amount[None] = module.amount
+ ... return True
+
+Let's construct a ``ModuleMartian`` with this ``GlobalMartian`` registered::
+
+ >>> from martian.core import MultiGlobalMartian
+ >>> multi_global_martian = MultiGlobalMartian()
+ >>> multi_global_martian.register(AmountMartian())
+ >>> mix_martian = ModuleMartian(multi, multi_global_martian)
+
+Now we grok and should pick up the right value::
+
+ >>> mix_martian.grok('g', g)
+ True
+ >>> read_amount[None]
+ 50
+
Old-style class support
-----------------------
Modified: grok/trunk/martian/src/martian/core.py
===================================================================
--- grok/trunk/martian/src/martian/core.py 2007-05-01 20:37:22 UTC (rev 74972)
+++ grok/trunk/martian/src/martian/core.py 2007-05-01 21:16:40 UTC (rev 74973)
@@ -13,16 +13,18 @@
util.class_annotation_nobase(component, 'grok.baseclass', False)))
class ModuleMartian(MartianBase):
- implements(IMartian)
+ implements(IMultiMartian)
- def __init__(self, martian):
+ def __init__(self, martian, global_martian=None):
self._martian = martian
-
+ self._global_martian = global_martian
+
def grok(self, name, module, **kw):
+ grokked_status = False
+ if self._global_martian:
+ grokked_status = self._global_martian.grok(name, module, **kw)
martian = self._martian
-
- grokked_status = False
-
+
for name in dir(module):
if name.startswith('__grok_'):
continue
@@ -71,7 +73,24 @@
class MultiClassMartian(MultiMartianBase):
def get_bases(self, obj):
return inspect.getmro(obj)
-
+
+class MultiGlobalMartian(MartianBase):
+ implements(IMultiMartian)
+
+ def __init__(self):
+ self._martians = []
+
+ def register(self, martian):
+ self._martians.append(martian)
+
+ def grok(self, name, module, **kw):
+ grokked_status = False
+ for martian in self._martians:
+ status = martian.grok(name, module, **kw)
+ if status:
+ grokked_status = True
+ return grokked_status
+
class MultiMartian(MartianBase):
implements(IMultiMartian)
More information about the Checkins
mailing list