[Checkins] SVN: martian/trunk/ Add a new kind of directive:
OptionalValueDirective, which takes either
Martijn Faassen
faassen at infrae.com
Wed Jan 23 08:52:27 EST 2008
Log message for revision 83108:
Add a new kind of directive: OptionalValueDirective, which takes either
zero or one argument. If no argument is supplied, the default value can
be determined by a rule.
Changed:
U martian/trunk/CHANGES.txt
U martian/trunk/src/martian/directive.py
U martian/trunk/src/martian/directive.txt
-=-
Modified: martian/trunk/CHANGES.txt
===================================================================
--- martian/trunk/CHANGES.txt 2008-01-23 13:39:50 UTC (rev 83107)
+++ martian/trunk/CHANGES.txt 2008-01-23 13:52:27 UTC (rev 83108)
@@ -2,12 +2,16 @@
*******
0.9.3 (unreleased)
-==================
+======-===========
-Bug fixes
----------
+Feature changes
+---------------
-* ...
+* Added an OptionalValueDirective which allows the construction of
+ directives that take either zero or one argument. If no arguments
+ are given, the ``default_value`` method on the directive is
+ called. Subclasses need to override this to return the default value
+ to use.
0.9.2 (2007-11-20)
==================
Modified: martian/trunk/src/martian/directive.py
===================================================================
--- martian/trunk/src/martian/directive.py 2008-01-23 13:39:50 UTC (rev 83107)
+++ martian/trunk/src/martian/directive.py 2008-01-23 13:52:27 UTC (rev 83108)
@@ -144,7 +144,18 @@
def value_factory(self, value):
return value
+class OptionalValueDirective(object):
+ def check_arguments(self, value=None):
+ pass
+ def value_factory(self, value=None):
+ if value is None:
+ return self.default_value()
+ return value
+
+ def default_value(self):
+ raise NotImplementedError
+
class BaseTextDirective(object):
"""
Base directive that only accepts unicode/ASCII values.
Modified: martian/trunk/src/martian/directive.txt
===================================================================
--- martian/trunk/src/martian/directive.txt 2008-01-23 13:39:50 UTC (rev 83107)
+++ martian/trunk/src/martian/directive.txt 2008-01-23 13:52:27 UTC (rev 83108)
@@ -198,3 +198,29 @@
Traceback (most recent call last):
...
GrokImportError: You can only pass interfaces to hello2.
+
+An ``OptionalValueDirective`` accepts a single value, but the value
+can be left out. If so, the value will be calculated by a function we
+pass it::
+
+ >>> from martian.directive import OptionalValueDirective
+ >>> class MyDirective(OptionalValueDirective, OnceDirective):
+ ... def default_value(self):
+ ... return 4 # the default if no value is given
+ >>> hello3 = MyDirective('hello3', ClassDirectiveContext())
+
+Let's try the directive with a single value::
+
+ >>> class Test(object):
+ ... hello3(1)
+ >>> Test.__hello3__
+ 1
+
+We can also try the directive without a value::
+
+ >>> class Test(object):
+ ... hello3()
+ >>> Test.__hello3__
+ 4
+
+
More information about the Checkins
mailing list