[Checkins] SVN: martian/trunk/ Inherit module-level directive information properly for class or module scope

Martijn Faassen faassen at infrae.com
Mon Jan 26 09:53:27 EST 2009


Log message for revision 95027:
  Inherit module-level directive information properly for class or module scope
  directives.
  

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	2009-01-26 14:14:12 UTC (rev 95026)
+++ martian/trunk/CHANGES.txt	2009-01-26 14:53:26 UTC (rev 95027)
@@ -4,6 +4,25 @@
 0.12 (unreleased)
 =================
 
+Feature changes
+---------------
+
+* ``CLASS_OR_MODULE`` scope directives will be aware of inheritance of
+  values that are defined in module-scope. Consider the following case::
+
+    module a:
+      some_directive('A')
+      class Foo(object):
+        pass
+
+    module b:
+      import a
+      class Bar(a.Foo):
+        pass
+
+  As before, ``Foo`` will have the value ``A`` configured for it. ``Bar``,
+  since it inherits from ``Foo``, will inherit this value.
+
 Bugs fixed
 ----------
 

Modified: martian/trunk/src/martian/directive.py
===================================================================
--- martian/trunk/src/martian/directive.py	2009-01-26 14:14:12 UTC (rev 95026)
+++ martian/trunk/src/martian/directive.py	2009-01-26 14:53:26 UTC (rev 95027)
@@ -5,6 +5,7 @@
 
 from martian import util
 from martian.error import GrokImportError, GrokError
+from martian import scan
 
 class StoreOnce(object):
 
@@ -114,6 +115,12 @@
         value = directive.store.get(directive, component, default)
         if value is default:
             value = directive.store.get(directive, module, default)
+        if value is default:
+            mro = component.__mro__
+            if len(mro) > 1:
+                base = mro[1]
+                module_of_base = scan.resolve(base.__module__)
+                value = self.get(directive, base, module_of_base, default)
         return value
 
 CLASS_OR_MODULE = ClassOrModuleScope()

Modified: martian/trunk/src/martian/directive.txt
===================================================================
--- martian/trunk/src/martian/directive.txt	2009-01-26 14:14:12 UTC (rev 95026)
+++ martian/trunk/src/martian/directive.txt	2009-01-26 14:53:26 UTC (rev 95027)
@@ -179,6 +179,68 @@
   ...     scope = CLASS
   ...     store = ONCE
 
+Inheritance in combination with module scope
+--------------------------------------------
+
+Let's look at how our layer directive can inherit in combination with
+directive use on module scope. 
+
+First we define a module which defines a class that gets the ``Test``
+layer through the use of the layer directive on module scope::
+
+  >>> class inheritmodule1(FakeModule):
+  ...   layer('Test')
+  ...   class Foo(object):
+  ...      pass
+  >>> from martiantest.fake import inheritmodule1
+
+Now we define another module that has a class that inherits from ``Foo``::
+
+  >>> class inheritmodule2(FakeModule):
+  ...   class Bar(inheritmodule1.Foo):
+  ...      pass
+  >>> from martiantest.fake import inheritmodule2
+
+We will now see that ``Bar`` has inherited the layer ``Test``::
+
+  >>> layer.bind().get(inheritmodule2.Bar, inheritmodule2)
+  'Test'
+
+Let's try it with another level of inheritance::
+
+  >>> class inheritmodule3(FakeModule):
+  ...   class Baz(inheritmodule2.Bar):
+  ...       pass
+  >>> from martiantest.fake import inheritmodule3
+ 
+The layer should still be inherited::
+
+  >>> layer.bind().get(inheritmodule3.Baz, inheritmodule3)
+  'Test'
+
+Let's override now by having an explicit layer directive on the class
+that subclasses ``Foo``::
+
+  >>> class inheritmodule4(FakeModule):
+  ...   class OverrideFoo(inheritmodule1.Foo):
+  ...      layer('AnotherTest')
+  >>> from martiantest.fake import inheritmodule4
+
+  >>> layer.bind().get(inheritmodule4.OverrideFoo, inheritmodule4)
+  'AnotherTest'
+
+Let's override now by having an explicit layer directive on
+module-level instead::
+
+  >>> class inheritmodule5(FakeModule):
+  ...   layer('AnotherTest')
+  ...   class OverrideFoo(inheritmodule1.Foo):
+  ...      pass
+  >>> from martiantest.fake import inheritmodule5
+
+  >>> layer.bind().get(inheritmodule5.OverrideFoo, inheritmodule5)
+  'AnotherTest'
+
 Using a directive multiple times
 --------------------------------
 



More information about the Checkins mailing list