[Checkins]
SVN: martian/branches/jw-philipp-using-ndir-directives/src/martian/directive.
MULTIPLE and DICT store now combine the values set by directives
Jan-Wijbrand Kolman
janwijbrand at gmail.com
Sat May 3 14:19:39 EDT 2008
Log message for revision 86262:
MULTIPLE and DICT store now combine the values set by directives
on the class and all of its base classses.
Changed:
U martian/branches/jw-philipp-using-ndir-directives/src/martian/directive.py
U martian/branches/jw-philipp-using-ndir-directives/src/martian/directive.txt
-=-
Modified: martian/branches/jw-philipp-using-ndir-directives/src/martian/directive.py
===================================================================
--- martian/branches/jw-philipp-using-ndir-directives/src/martian/directive.py 2008-05-03 18:06:08 UTC (rev 86261)
+++ martian/branches/jw-philipp-using-ndir-directives/src/martian/directive.py 2008-05-03 18:19:38 UTC (rev 86262)
@@ -30,6 +30,21 @@
class StoreMultipleTimes(StoreOnce):
+ def get(self, directive, component, default):
+ if getattr(component, directive.dotted_name(), default) is default:
+ return default
+
+ result = []
+ for base in reversed(component.mro()):
+ list = getattr(base, directive.dotted_name(), default)
+ if list is not default and list not in result:
+ result.append(list)
+
+ result_flattened = []
+ for entry in result:
+ result_flattened.extend(entry)
+ return result_flattened
+
def set(self, locals_, directive, value):
values = locals_.setdefault(directive.dotted_name(), [])
values.append(value)
@@ -38,6 +53,17 @@
class StoreDict(StoreOnce):
+ def get(self, directive, component, default):
+ if getattr(component, directive.dotted_name(), default) is default:
+ return default
+
+ result = {}
+ for base in reversed(component.mro()):
+ mapping = getattr(base, directive.dotted_name(), default)
+ if mapping is not default:
+ result.update(mapping)
+ return result
+
def set(self, locals_, directive, value):
values_dict = locals_.setdefault(directive.dotted_name(), {})
try:
Modified: martian/branches/jw-philipp-using-ndir-directives/src/martian/directive.txt
===================================================================
--- martian/branches/jw-philipp-using-ndir-directives/src/martian/directive.txt 2008-05-03 18:06:08 UTC (rev 86261)
+++ martian/branches/jw-philipp-using-ndir-directives/src/martian/directive.txt 2008-05-03 18:19:38 UTC (rev 86262)
@@ -194,6 +194,16 @@
>>> multi.get(Bar)
[]
+Whenever the directive is used on a sub class of a component, the values set by
+directives on the base classes are concatenated::
+
+ >>> class Qux(Foo):
+ ... multi(u'Triple')
+ ...
+ >>> multi.get(Qux)
+ [u'Once', u'Twice', u'Triple']
+
+
Using a directive multiple times, as a dictionary
-------------------------------------------------
@@ -248,6 +258,25 @@
GrokImportError: The factory method for the 'wrongmulti2' directive should
return a key-value pair.
+ >>> class multi(Directive):
+ ... scope = CLASS
+ ... store = DICT
+ ... def factory(self, value, andanother):
+ ... return value, andanother
+ ...
+ >>> class Frepple(object):
+ ... multi(1, 'AAA')
+ ... multi(2, 'BBB')
+ ...
+ >>> class Fropple(Frepple):
+ ... multi(1, 'CCC')
+ ... multi(3, 'DDD')
+ ... multi(4, 'EEE')
+
+ >>> d = multi.get(Fropple)
+ >>> print sorted(d.items())
+ [(1, 'CCC'), (2, 'BBB'), (3, 'DDD'), (4, 'EEE')]
+
Calculated defaults
-------------------
More information about the Checkins
mailing list