[Checkins] SVN: martian/trunk/src/martian/ndir. Add support for calling a directive multiple times.

Martijn Faassen faassen at infrae.com
Wed Jan 30 14:33:54 EST 2008


Log message for revision 83321:
  Add support for calling a directive multiple times.
  

Changed:
  U   martian/trunk/src/martian/ndir.py
  U   martian/trunk/src/martian/ndir.txt

-=-
Modified: martian/trunk/src/martian/ndir.py
===================================================================
--- martian/trunk/src/martian/ndir.py	2008-01-30 18:50:11 UTC (rev 83320)
+++ martian/trunk/src/martian/ndir.py	2008-01-30 19:33:54 UTC (rev 83321)
@@ -5,7 +5,9 @@
 
 NOT_FOUND = object()
 
+# ONCE or MULTIPLE
 ONCE = object()
+MULTIPLE = object()
 
 class ClassScope(object):
     description = 'class'
@@ -39,11 +41,18 @@
         if not self.scope.check(frame):
             raise GrokImportError("%s can only be used on %s level." %
                                   (name, self.scope.description))
-        if name in frame.f_locals:
-            raise GrokImportError("%s can only be called once per %s." %
-                                  (name, self.scope.description))
-        frame.f_locals[name] = value
-        
+        if self.times is ONCE:
+            if name in frame.f_locals:
+                raise GrokImportError("%s can only be called once per %s." %
+                                      (name, self.scope.description))
+            frame.f_locals[name] = value
+        elif self.times is MULTIPLE:
+            values = frame.f_locals.get(name, [])
+            values.append(value)
+            frame.f_locals[name] = values
+        else:
+            assert False, "Unknown value for times: %" % self.times
+            
     def get(self, component, module=None):
         name = self.namespaced_name()
         value = getattr(component, name, NOT_FOUND)

Modified: martian/trunk/src/martian/ndir.txt
===================================================================
--- martian/trunk/src/martian/ndir.txt	2008-01-30 18:50:11 UTC (rev 83320)
+++ martian/trunk/src/martian/ndir.txt	2008-01-30 19:33:54 UTC (rev 83321)
@@ -7,6 +7,9 @@
 define these directives, and how to use them to retrieve information
 for a class for use during the grokking procedure.
 
+A simple directive
+------------------
+
 Let's define a simple directive that sets a description::
 
   >>> from martian.ndir import Directive, CLASS, ONCE
@@ -82,6 +85,9 @@
     ...
   GrokImportError: martian.description can only be called once per class.
 
+Class and module scope
+----------------------
+
 Let's now define a ``layer`` directive that can be used in class and
 module scope both::
 
@@ -129,3 +135,22 @@
   >>> layer.get(testmodule.Foo, testmodule) is None
   True
 
+Using a directive multiple times
+--------------------------------
+
+A directive can be configured to allow it to be called multiple times
+in the same scope::
+
+  >>> from martian.ndir import MULTIPLE
+  >>> multi = Directive('martian', 'multi', CLASS, MULTIPLE, None)
+
+Let's try it::
+
+  >>> class Foo(object):
+  ...   multi(u"Once")
+  ...   multi(u"Twice")
+
+We can now retrieve the value and we'll get a list::
+
+  >>> multi.get(Foo)
+  [u'Once', u'Twice']



More information about the Checkins mailing list