[Checkins] SVN: grok/branches/faassen-martian/martian/src/martian/ Support grokking a package.

Martijn Faassen faassen at infrae.com
Sat May 5 13:44:14 EDT 2007


Log message for revision 75509:
  Support grokking a package.
  

Changed:
  U   grok/branches/faassen-martian/martian/src/martian/README.txt
  U   grok/branches/faassen-martian/martian/src/martian/__init__.py
  U   grok/branches/faassen-martian/martian/src/martian/core.py
  A   grok/branches/faassen-martian/martian/src/martian/tests/testpackage/
  A   grok/branches/faassen-martian/martian/src/martian/tests/testpackage/__init__.py
  A   grok/branches/faassen-martian/martian/src/martian/tests/testpackage/alpha/
  A   grok/branches/faassen-martian/martian/src/martian/tests/testpackage/alpha/__init__.py
  A   grok/branches/faassen-martian/martian/src/martian/tests/testpackage/animal.py
  A   grok/branches/faassen-martian/martian/src/martian/tests/testpackage/beta/
  A   grok/branches/faassen-martian/martian/src/martian/tests/testpackage/beta/__init__.py
  A   grok/branches/faassen-martian/martian/src/martian/tests/testpackage/beta/three.py
  A   grok/branches/faassen-martian/martian/src/martian/tests/testpackage/one.py
  A   grok/branches/faassen-martian/martian/src/martian/tests/testpackage/two.py

-=-
Modified: grok/branches/faassen-martian/martian/src/martian/README.txt
===================================================================
--- grok/branches/faassen-martian/martian/src/martian/README.txt	2007-05-05 17:13:06 UTC (rev 75508)
+++ grok/branches/faassen-martian/martian/src/martian/README.txt	2007-05-05 17:44:14 UTC (rev 75509)
@@ -746,3 +746,39 @@
   True
   >>> oldstyle.all_machine_instances.keys()
   ['robot']
+
+Grokking a package
+------------------
+
+A package consists of several sub modules. When grokking a package,
+all the files in the package will be grokked. Let's first create a simple
+grokker for the ``Animal`` class defined by the package::
+
+  >>> from martian.tests.testpackage import animal
+  >>> all_animals = {}
+  >>> class AnimalGrokker(ClassGrokker):
+  ...   component_class = animal.Animal
+  ...   def grok(self, name, obj, **kw):
+  ...     all_animals[name] = obj
+  ...     return True
+
+The grokker will collect animals into the ``all_animals`` dictionary.
+
+Let's register this grokker for a ModuleGrokker::
+
+  >>> module_grokker = ModuleGrokker()
+  >>> module_grokker.register(AnimalGrokker())
+
+Now let's grok the whole ``testpackage`` for animals::
+
+  >>> from martian import grok_dotted_name
+  >>> grok_dotted_name('martian.tests.testpackage', grokker=module_grokker)
+  
+We should now get some animals::
+
+  >>> sorted(all_animals.keys())
+  ['Animal', 'Bear', 'Dragon', 'Lizard', 'Python', 'SpermWhale', 'Whale']
+
+
+  
+

Modified: grok/branches/faassen-martian/martian/src/martian/__init__.py
===================================================================
--- grok/branches/faassen-martian/martian/src/martian/__init__.py	2007-05-05 17:13:06 UTC (rev 75508)
+++ grok/branches/faassen-martian/martian/src/martian/__init__.py	2007-05-05 17:44:14 UTC (rev 75509)
@@ -1,2 +1,2 @@
-from core import ModuleGrokker, MultiGrokker
+from core import ModuleGrokker, MultiGrokker, grok_dotted_name
 from components import GlobalGrokker, ClassGrokker, InstanceGrokker

Modified: grok/branches/faassen-martian/martian/src/martian/core.py
===================================================================
--- grok/branches/faassen-martian/martian/src/martian/core.py	2007-05-05 17:13:06 UTC (rev 75508)
+++ grok/branches/faassen-martian/martian/src/martian/core.py	2007-05-05 17:44:14 UTC (rev 75509)
@@ -3,7 +3,7 @@
 from zope.interface import implements
 
 from martian.interfaces import IGrokker, IMultiGrokker
-from martian import util
+from martian import util, scan
 from martian.components import (GrokkerBase, ClassGrokker, InstanceGrokker,
                                 GlobalGrokker)
 
@@ -41,7 +41,7 @@
                 grokked_status = True
 
         return grokked_status
-    
+        
 class MultiGrokkerBase(GrokkerBase):
     implements(IMultiGrokker)
 
@@ -123,6 +123,22 @@
         else:
             return self._multi_instance_grokker.grok(name, obj, **kw)
 
+def grok_dotted_name(dotted_name, grokker=None, **kw):
+    module_info = scan.module_info_from_dotted_name(dotted_name)
+    grok_package(module_info, grokker, **kw)
+    
+def grok_package(module_info, grokker=None, **kw):
+    if grokker is None:
+        grokker = the_module_grokker
+    grok_module(module_info, grokker, **kw)
+    for sub_module_info in module_info.getSubModuleInfos():
+        grok_package(sub_module_info, grokker, **kw)
+
+def grok_module(module_info, grokker, **kw):
+    if grokker is None:
+        grokker = the_module_grokker
+    grokker.grok(module_info.dotted_name, module_info.getModule(), **kw)
+    
 # deep meta mode here - we define grokkers that can pick up the
 # three kinds of grokker: ClassGrokker, InstanceGrokker and ModuleGrokker
 class MetaGrokker(ClassGrokker):
@@ -137,10 +153,13 @@
 
 class GlobalMetaGrokker(MetaGrokker):
     component_class = GlobalGrokker
-    
+
 # the global single grokker to bootstrap everything
 the_grokker = MultiGrokker()
 # bootstrap the meta-grokkers
 the_grokker.register(ClassMetaGrokker())
 the_grokker.register(InstanceMetaGrokker())
 the_grokker.register(GlobalMetaGrokker())
+
+# a global module grokker
+the_module_grokker = ModuleGrokker(the_grokker)

Added: grok/branches/faassen-martian/martian/src/martian/tests/testpackage/__init__.py
===================================================================
--- grok/branches/faassen-martian/martian/src/martian/tests/testpackage/__init__.py	2007-05-05 17:13:06 UTC (rev 75508)
+++ grok/branches/faassen-martian/martian/src/martian/tests/testpackage/__init__.py	2007-05-05 17:44:14 UTC (rev 75509)
@@ -0,0 +1,2 @@
+# 
+

Added: grok/branches/faassen-martian/martian/src/martian/tests/testpackage/alpha/__init__.py
===================================================================
--- grok/branches/faassen-martian/martian/src/martian/tests/testpackage/alpha/__init__.py	2007-05-05 17:13:06 UTC (rev 75508)
+++ grok/branches/faassen-martian/martian/src/martian/tests/testpackage/alpha/__init__.py	2007-05-05 17:44:14 UTC (rev 75509)
@@ -0,0 +1,4 @@
+from martian.tests.testpackage import animal
+
+class Python(animal.Animal):
+    pass

Added: grok/branches/faassen-martian/martian/src/martian/tests/testpackage/animal.py
===================================================================
--- grok/branches/faassen-martian/martian/src/martian/tests/testpackage/animal.py	2007-05-05 17:13:06 UTC (rev 75508)
+++ grok/branches/faassen-martian/martian/src/martian/tests/testpackage/animal.py	2007-05-05 17:44:14 UTC (rev 75509)
@@ -0,0 +1,2 @@
+class Animal(object):
+    pass

Added: grok/branches/faassen-martian/martian/src/martian/tests/testpackage/beta/__init__.py
===================================================================
--- grok/branches/faassen-martian/martian/src/martian/tests/testpackage/beta/__init__.py	2007-05-05 17:13:06 UTC (rev 75508)
+++ grok/branches/faassen-martian/martian/src/martian/tests/testpackage/beta/__init__.py	2007-05-05 17:44:14 UTC (rev 75509)
@@ -0,0 +1 @@
+#

Added: grok/branches/faassen-martian/martian/src/martian/tests/testpackage/beta/three.py
===================================================================
--- grok/branches/faassen-martian/martian/src/martian/tests/testpackage/beta/three.py	2007-05-05 17:13:06 UTC (rev 75508)
+++ grok/branches/faassen-martian/martian/src/martian/tests/testpackage/beta/three.py	2007-05-05 17:44:14 UTC (rev 75509)
@@ -0,0 +1,5 @@
+from martian.tests.testpackage import animal
+
+class Lizard(animal.Animal):
+    pass
+

Added: grok/branches/faassen-martian/martian/src/martian/tests/testpackage/one.py
===================================================================
--- grok/branches/faassen-martian/martian/src/martian/tests/testpackage/one.py	2007-05-05 17:13:06 UTC (rev 75508)
+++ grok/branches/faassen-martian/martian/src/martian/tests/testpackage/one.py	2007-05-05 17:44:14 UTC (rev 75509)
@@ -0,0 +1,10 @@
+import animal
+
+class Whale(animal.Animal):
+    pass
+
+class Dragon(animal.Animal):
+    pass
+
+class SpermWhale(Whale):
+    pass

Added: grok/branches/faassen-martian/martian/src/martian/tests/testpackage/two.py
===================================================================
--- grok/branches/faassen-martian/martian/src/martian/tests/testpackage/two.py	2007-05-05 17:13:06 UTC (rev 75508)
+++ grok/branches/faassen-martian/martian/src/martian/tests/testpackage/two.py	2007-05-05 17:44:14 UTC (rev 75509)
@@ -0,0 +1,4 @@
+import animal
+
+class Bear(animal.Animal):
+    pass



More information about the Checkins mailing list