[Checkins] SVN: martian/trunk/src/martian/ Make sure registrations
happens only once.
Martijn Faassen
faassen at infrae.com
Tue Jun 19 15:58:43 EDT 2007
Log message for revision 76811:
Make sure registrations happens only once.
Changed:
U martian/trunk/src/martian/README.txt
U martian/trunk/src/martian/core.py
-=-
Modified: martian/trunk/src/martian/README.txt
===================================================================
--- martian/trunk/src/martian/README.txt 2007-06-19 19:49:26 UTC (rev 76810)
+++ martian/trunk/src/martian/README.txt 2007-06-19 19:58:42 UTC (rev 76811)
@@ -936,6 +936,47 @@
>>> multi_grokker.grok('ColorGrokker', ColorGrokker)
True
+Executing meta grokkers only once
+---------------------------------
+
+In case of ``ClassGrokker`` and all other grokkers that are grokked
+by meta grokkers, we only want the grokking to occur once even if
+the same module (or package) is grokked twice::
+
+ >>> class TestOnce(object):
+ ... pass
+ >>> executed = []
+ >>> class somemodule(FakeModule):
+ ... class TestGrokker(ClassGrokker):
+ ... component_class = TestOnce
+ ... def grok(self, name, obj):
+ ... executed.append(name)
+ ... return True
+ >>> somemodule = fake_import(somemodule)
+ >>> module_grokker = ModuleGrokker(MetaMultiGrokker())
+
+Let's grok the module once::
+
+ >>> module_grokker.grok('somemodule', somemodule)
+ True
+
+Let's grok it twice::
+
+ >>> module_grokker.grok('somemodule', somemodule)
+ True
+
+Even though we have grokked it twice, it is still only registered once. We
+can show this by actually having it grok a ``TestOnce`` subclass::
+
+ >>> class anothermodule(FakeModule):
+ ... class TestSub(TestOnce):
+ ... pass
+ >>> anothermodule = fake_import(anothermodule)
+ >>> module_grokker.grok('anothermodule', anothermodule)
+ True
+ >>> executed
+ ['TestSub']
+
Priority
--------
Modified: martian/trunk/src/martian/core.py
===================================================================
--- martian/trunk/src/martian/core.py 2007-06-19 19:49:26 UTC (rev 76810)
+++ martian/trunk/src/martian/core.py 2007-06-19 19:58:42 UTC (rev 76811)
@@ -58,9 +58,8 @@
# sort grokkers by priority
grokkers = sorted(self.grokkers(name, module),
- key=lambda (grokker, name, obj): grokker.priority)
- # reverse so highest priority happens first
- grokkers.reverse()
+ key=lambda (grokker, name, obj): grokker.priority,
+ reverse=True)
for g, name, obj in grokkers:
grokked = g.grok(name, obj, **kw)
@@ -103,8 +102,10 @@
def register(self, grokker):
key = grokker.component_class
grokkers = self._grokkers.setdefault(key, [])
- if grokker not in grokkers:
- grokkers.append(grokker)
+ for g in grokkers:
+ if g.__class__ is grokker.__class__:
+ return
+ grokkers.append(grokker)
def clear(self):
self._grokkers = {}
@@ -136,7 +137,8 @@
self.clear()
def register(self, grokker):
- self._grokkers.append(grokker)
+ if grokker not in self._grokkers:
+ self._grokkers.append(grokker)
def clear(self):
self._grokkers = []
More information about the Checkins
mailing list