[Checkins]
SVN: martian/branches/philikon-decl-dir-rules/src/martian/directive.
A sanity check: should not be able to use a CLASS_OR_MODULE
directive
Martijn Faassen
faassen at infrae.com
Mon May 5 08:53:58 EDT 2008
Log message for revision 86447:
A sanity check: should not be able to use a CLASS_OR_MODULE directive
without passing in a module.
Changed:
U martian/branches/philikon-decl-dir-rules/src/martian/directive.py
U martian/branches/philikon-decl-dir-rules/src/martian/directive.txt
-=-
Modified: martian/branches/philikon-decl-dir-rules/src/martian/directive.py
===================================================================
--- martian/branches/philikon-decl-dir-rules/src/martian/directive.py 2008-05-05 12:48:43 UTC (rev 86446)
+++ martian/branches/philikon-decl-dir-rules/src/martian/directive.py 2008-05-05 12:53:57 UTC (rev 86447)
@@ -4,7 +4,7 @@
from zope.interface.interfaces import IInterface
from martian import util
-from martian.error import GrokImportError
+from martian.error import GrokImportError, GrokError
class StoreOnce(object):
@@ -180,6 +180,12 @@
def get(self, component, module=None, **data):
directive = self.directive
+ # a sanity check
+ if directive.scope is CLASS_OR_MODULE and module is None:
+ raise TypeError(
+ "The directive '%s' has scope CLASS_OR_MODULE "
+ "but no module was passed in to ``get``" %
+ self.directive.__name__)
value = directive.store.get(directive, component, default=_USE_DEFAULT)
if value is _USE_DEFAULT and module is not None:
value = directive.store.get(directive, module, default=_USE_DEFAULT)
Modified: martian/branches/philikon-decl-dir-rules/src/martian/directive.txt
===================================================================
--- martian/branches/philikon-decl-dir-rules/src/martian/directive.txt 2008-05-05 12:48:43 UTC (rev 86446)
+++ martian/branches/philikon-decl-dir-rules/src/martian/directive.txt 2008-05-05 12:53:57 UTC (rev 86447)
@@ -125,18 +125,24 @@
By default, the ``default`` property is None which is why we can omit
specifying it here.
+This directive has been declared ``CLASS_OR_MODULE``, so you will
+always have to pass a module to the directive. Since we don't have a
+module yet we'll simply create a dummy, empty, fallback module::
+
+ >>> dummy = object()
+
We can use this directive now on a class::
>>> class Foo(object):
... layer('Test')
- >>> layer.bind().get(Foo)
+ >>> layer.bind().get(Foo, dummy)
'Test'
The defaulting to ``None`` works::
>>> class Foo(object):
... pass
- >>> layer.bind().get(Foo) is None
+ >>> layer.bind().get(Foo, dummy) is None
True
We can also use it in a module::
More information about the Checkins
mailing list