[Checkins] SVN: martian/branches/philikon-decl-dir-rules/src/martian/ * fallback behavior is now aware of scoping rules; fallback won't

Martijn Faassen faassen at infrae.com
Mon May 5 09:46:06 EDT 2008


Log message for revision 86453:
  * fallback behavior is now aware of scoping rules; fallback won't
    just happen because a module argument was passed in.
  
  * assertions to make sure not the wrong combination of arguments
    was passed. Perhaps this is being too strict and we want to require
    the user to *always* pass in component and module *both*, no defaulting,
    since the fallback rule now takes scope into account this should work too.
  
  * a fit in fake_import to also import names with '.' in it, so that
    directives work properly with fake modules.
  

Changed:
  U   martian/branches/philikon-decl-dir-rules/src/martian/directive.py
  U   martian/branches/philikon-decl-dir-rules/src/martian/directive.txt
  U   martian/branches/philikon-decl-dir-rules/src/martian/tests/test_all.py

-=-
Modified: martian/branches/philikon-decl-dir-rules/src/martian/directive.py
===================================================================
--- martian/branches/philikon-decl-dir-rules/src/martian/directive.py	2008-05-05 13:39:28 UTC (rev 86452)
+++ martian/branches/philikon-decl-dir-rules/src/martian/directive.py	2008-05-05 13:46:05 UTC (rev 86453)
@@ -91,6 +91,17 @@
     def check(self, frame):
         return util.frame_is_class(frame) and not is_fake_module(frame)
 
+    def get(self, directive, component, module):
+        assert component is not None, (
+            "The directive '%s' has scope CLASS "
+            "but no class was passed into 'get'" %
+                               directive.__name__)
+        assert module is None, (
+            "The directive '%s' has scope CLASS "
+            "but a module was also passed into 'get'" %
+            directive.__name__)
+        return directive.store.get(directive, component, default=_USE_DEFAULT)
+    
 CLASS = ClassScope()
 
 class ClassOrModuleScope(object):
@@ -99,6 +110,21 @@
     def check(self, frame):
         return util.frame_is_class(frame) or util.frame_is_module(frame)
 
+    def get(self, directive, component, module):
+        assert component is not None, (
+            "The directive '%s' has scope CLASS_OR_MODULE "
+            "but no class was passed into 'get'" %
+                               directive.__name__)
+        assert module is not None, (
+            "The directive '%s' has scope CLASS_OR_MODULE "
+            "but no module was passed into 'get'" %
+            directive.__name__)
+
+        value = directive.store.get(directive, component, default=_USE_DEFAULT)
+        if value is _USE_DEFAULT:
+            value = directive.store.get(directive, module, default=_USE_DEFAULT)
+        return value
+    
 CLASS_OR_MODULE = ClassOrModuleScope()
 
 class ModuleScope(object):
@@ -107,6 +133,18 @@
     def check(self, frame):
         return util.frame_is_module(frame) or is_fake_module(frame)
 
+    def get(self, directive, component, module):
+        assert component is None, (
+            "The directive '%s' has scope MODULE "
+            "but a class was also passed into 'get'" %
+            directive.__name__)
+        assert module is not None, (
+            "The directive '%s' has scope MODULE "
+            "but no module was passed into 'get'" %
+            directive.__name__)
+
+        return directive.store.get(directive, module, default=_USE_DEFAULT)
+    
 MODULE = ModuleScope()
 
 class Directive(object):
@@ -178,21 +216,9 @@
             return self.default
         return self.directive.default
 
-    def get(self, component, module=None, **data):
+    def get(self, component=None, 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__)
-        if directive.scope is CLASS and module is not None:
-            raise TypeError(
-                "The directive '%s' has scope CLASS "
-                "but a module was also passed into ``get``")
-        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)
+        value = directive.scope.get(directive, component, module)
         if value is _USE_DEFAULT:
             value = self.get_default(component, module, **data)
         return value

Modified: martian/branches/philikon-decl-dir-rules/src/martian/directive.txt
===================================================================
--- martian/branches/philikon-decl-dir-rules/src/martian/directive.txt	2008-05-05 13:39:28 UTC (rev 86452)
+++ martian/branches/philikon-decl-dir-rules/src/martian/directive.txt	2008-05-05 13:46:05 UTC (rev 86453)
@@ -151,7 +151,7 @@
   ...    layer('Test2')
   ...    class Foo(object):
   ...       pass
-  >>> test_module = fake_import(testmodule)
+  >>> testmodule = fake_import(testmodule)
 
 When we now try to access ``layer`` on ``Foo``, we find the
 module-level default which we just set. We pass the module as the
@@ -173,6 +173,25 @@
   >>> layer.bind().get(testmodule.Foo, testmodule) is None
   True
 
+Let's now look at this using a directive with CLASS scope only::
+
+  >>> class layer2(Directive):
+  ...     scope = CLASS
+  ...     store = ONCE
+
+We are not allowed to pass in a module argument to ``get`` now::
+
+  >>> class testmodule2(FakeModule):
+  ...    layer2('Test2')
+  ...    class Foo(object):
+  ...       pass
+  >>> testmodule2 = fake_import(testmodule2)
+
+  >>> layer2.bind().get(testmodule2.Foo, testmodule2)
+  Traceback (most recent call last):
+    ...
+  AssertionError: The directive 'layer2' has scope CLASS but a module was also passed into 'get'
+
 Using a directive multiple times
 --------------------------------
 
@@ -302,7 +321,7 @@
   ...     multi('Two')
   ...
   >>> module_with_directive = fake_import(module_with_directive)
-  >>> print multi.bind().get(module_with_directive)
+  >>> print multi.bind().get(module=module_with_directive)
   ['One', 'Two']
 
   >>> from martian import MODULE
@@ -320,7 +339,7 @@
   ...     multi(2, 'Two')
   ...
   >>> module_with_directive = fake_import(module_with_directive)
-  >>> d = multi.bind().get(module_with_directive)
+  >>> d = multi.bind().get(module=module_with_directive)
   >>> print sorted(d.items())
   [(1, 'One'), (2, 'Two')]
 

Modified: martian/branches/philikon-decl-dir-rules/src/martian/tests/test_all.py
===================================================================
--- martian/branches/philikon-decl-dir-rules/src/martian/tests/test_all.py	2008-05-05 13:39:28 UTC (rev 86452)
+++ martian/branches/philikon-decl-dir-rules/src/martian/tests/test_all.py	2008-05-05 13:46:05 UTC (rev 86453)
@@ -16,7 +16,7 @@
     module = new.module(fake_module.__name__)
     glob = {}
     for name in dir(fake_module):
-        if name.startswith('__'):
+        if name.startswith('__') and '.' not in name:
             continue
         obj = getattr(fake_module, name)
         glob[name] = obj



More information about the Checkins mailing list