[Checkins] SVN: martian/trunk/src/martian/ Make sure it also works for instance grokkers and global grokkers.

Martijn Faassen faassen at infrae.com
Tue Jun 19 16:05:17 EDT 2007


Log message for revision 76812:
  Make sure it also works for instance grokkers and global grokkers.
  

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:58:42 UTC (rev 76811)
+++ martian/trunk/src/martian/README.txt	2007-06-19 20:05:16 UTC (rev 76812)
@@ -977,6 +977,61 @@
   >>> executed
   ['TestSub']
 
+This also works for instance grokkers::
+
+  >>> class TestInstanceOnce(object):
+  ...   pass
+  >>> executed = []
+  >>> class somemodule(FakeModule):
+  ...   class TestGrokker(InstanceGrokker):
+  ...     component_class = TestInstanceOnce
+  ...     def grok(self, name, obj):
+  ...        executed.append(name)
+  ...        return True
+  >>> somemodule = fake_import(somemodule)
+  >>> module_grokker.clear()
+  >>> module_grokker.grok('somemodule', somemodule) # once
+  True
+  >>> module_grokker.grok('somemodule', somemodule) # twice
+  True
+  >>> class anothermodule(FakeModule):
+  ...   test = TestInstanceOnce()
+  >>> anothermodule = fake_import(anothermodule)
+  >>> module_grokker.grok('anothermodule', anothermodule)
+  True
+  >>> executed
+  ['test']
+
+It also works for global grokkers::
+
+  >>> executed = []
+  >>> class somemodule(FakeModule):
+  ...   class TestGrokker(GlobalGrokker):
+  ...     def grok(self, name, obj):
+  ...       executed.append(name)
+  ...       return True
+  >>> somemodule = fake_import(somemodule)
+  >>> module_grokker.clear()
+  >>> module_grokker.grok('somemodule', somemodule) # once
+  True
+  >>> module_grokker.grok('somemodule', somemodule) # twice
+  True
+
+The second grokking will already make ``somemodule`` grokked::
+
+  >>> executed
+  ['somemodule']
+
+Now let's grok another module::
+
+  >>> class anothermodule(FakeModule):
+  ...   pass
+  >>> anothermodule = fake_import(anothermodule)
+  >>> module_grokker.grok('anothermodule', anothermodule)
+  True
+  >>> executed
+  ['somemodule', 'anothermodule']
+
 Priority
 --------
 

Modified: martian/trunk/src/martian/core.py
===================================================================
--- martian/trunk/src/martian/core.py	2007-06-19 19:58:42 UTC (rev 76811)
+++ martian/trunk/src/martian/core.py	2007-06-19 20:05:16 UTC (rev 76812)
@@ -137,8 +137,10 @@
         self.clear()
 
     def register(self, grokker):
-        if grokker not in self._grokkers:
-            self._grokkers.append(grokker)
+        for g in self._grokkers:
+            if grokker.__class__ is g.__class__:
+                return
+        self._grokkers.append(grokker)
 
     def clear(self):
         self._grokkers = []



More information about the Checkins mailing list