[Checkins] SVN: martian/branches/jw-philipp-using-ndir-directives/src/martian/ndir. Implement a dictionary store.

Jan-Wijbrand Kolman janwijbrand at gmail.com
Sat May 3 06:05:56 EDT 2008


Log message for revision 86164:
  Implement a dictionary store.

Changed:
  U   martian/branches/jw-philipp-using-ndir-directives/src/martian/ndir.py
  U   martian/branches/jw-philipp-using-ndir-directives/src/martian/ndir.txt

-=-
Modified: martian/branches/jw-philipp-using-ndir-directives/src/martian/ndir.py
===================================================================
--- martian/branches/jw-philipp-using-ndir-directives/src/martian/ndir.py	2008-05-03 10:02:54 UTC (rev 86163)
+++ martian/branches/jw-philipp-using-ndir-directives/src/martian/ndir.py	2008-05-03 10:05:55 UTC (rev 86164)
@@ -41,7 +41,22 @@
 
 MULTIPLE = StoreMultipleTimes()
 
+class StoreDict(StoreOnce):
 
+    def set(self, frame, directive, value):
+        dotted_name = (directive.__class__.__module__ + '.' +
+                       directive.__class__.__name__)
+        values_dict = frame.f_locals.setdefault(dotted_name, {})
+        try:
+            key, value = value
+        except (TypeError, ValueError):
+            raise GrokImportError(
+                "The factory method for the '%s' directive should return a "
+                "key-value pair." % directive.name)
+        values_dict[key] = value
+
+DICT = StoreDict()
+
 _SENTINEL = object()
 _USE_DEFAULT = object()
 

Modified: martian/branches/jw-philipp-using-ndir-directives/src/martian/ndir.txt
===================================================================
--- martian/branches/jw-philipp-using-ndir-directives/src/martian/ndir.txt	2008-05-03 10:02:54 UTC (rev 86163)
+++ martian/branches/jw-philipp-using-ndir-directives/src/martian/ndir.txt	2008-05-03 10:05:55 UTC (rev 86164)
@@ -181,6 +181,60 @@
   >>> multi.get(Foo)
   [u'Once', u'Twice']
 
+Using a directive multiple times, as a dictionary
+-------------------------------------------------
+
+A directive can be configured to allow it to be called multiple times in the
+same scope. In this case the factory method should be overridden to return a
+key-value pair::
+
+  >>> from martian.ndir import DICT
+  >>> class multi(Directive):
+  ...     scope = CLASS
+  ...     store = DICT
+  ...     def factory(self, value):
+  ...         return value.lower(), value
+
+We can now use the directive multiple times without any errors::
+
+  >>> class Bar(object):
+  ...   multi(u"Once")
+  ...   multi(u"Twice")
+
+We can now retrieve the value and we'll get a to the items::
+
+  >>> d = multi.get(Bar)
+  >>> print sorted(d.items())
+  [(u'once', u'Once'), (u'twice', u'Twice')]
+
+When the factory method does not return a key-value pair, an error is raised::
+
+  >>> class wrongmulti(Directive):
+  ...     scope = CLASS
+  ...     store = DICT
+  ...     def factory(self, value):
+  ...         return None
+
+  >>> class Baz(object):
+  ...   wrongmulti(u"Once")
+  Traceback (most recent call last):
+  ...
+  GrokImportError: The factory method for the 'wrongmulti' directive should
+  return a key-value pair.
+
+  >>> class wrongmulti2(Directive):
+  ...     scope = CLASS
+  ...     store = DICT
+  ...     def factory(self, value):
+  ...         return value, value, value
+
+  >>> class Baz(object):
+  ...   wrongmulti2(u"Once")
+  Traceback (most recent call last):
+  ...
+  GrokImportError: The factory method for the 'wrongmulti2' directive should
+  return a key-value pair.
+
 Calculated defaults
 -------------------
 



More information about the Checkins mailing list