[Checkins] SVN: manuel/trunk/src/manuel/ use decorator for specifying timing

Benji York benji at zope.com
Mon Apr 27 08:36:50 EDT 2009


Log message for revision 99533:
  use decorator for specifying timing
  

Changed:
  U   manuel/trunk/src/manuel/README.txt
  U   manuel/trunk/src/manuel/__init__.py
  U   manuel/trunk/src/manuel/doctest.py
  U   manuel/trunk/src/manuel/footnote.py
  U   manuel/trunk/src/manuel/tests.py

-=-
Modified: manuel/trunk/src/manuel/README.txt
===================================================================
--- manuel/trunk/src/manuel/README.txt	2009-04-27 11:31:15 UTC (rev 99532)
+++ manuel/trunk/src/manuel/README.txt	2009-04-27 12:36:49 UTC (rev 99533)
@@ -373,14 +373,15 @@
 ==========
 
 Some functionality requires that code be called early or late in a phase.  The
-"manuel_timing" attribute allows either "early" or "late" to be specified.
+"timing" decorator allows either EARLY or LATE to be specified.
 
 Early functions are run first (in arbitrary order), then functions with no
 specified timing, then the late functions are called (again in arbitrary
 order).  This function also demonstrates the "copy" method of Region objects
 and the "insert_region_before" and "insert_region_after" methods of Documents.
 
-    >>> def cloner_parser(document):
+    >>> @manuel.timing(manuel.LATE)
+    ... def cloning_parser(document):
     ...     to_be_cloned = None
     ...     # find the region to clone
     ...     document_iter = iter(document)
@@ -405,9 +406,7 @@
     ...                 clone.provenance = 'cloned to go after'
     ...                 document.insert_region_after(region, clone)
 
-    >>> cloner_parser.manuel_timing = 'late'
-    >>> cloning_manuel = manuel.Manuel([cloner_parser])
-    >>> m.extend(cloning_manuel)
+    >>> m.add_parser(cloning_parser)
 
     >>> source = """\
     ... This is my clone:

Modified: manuel/trunk/src/manuel/__init__.py
===================================================================
--- manuel/trunk/src/manuel/__init__.py	2009-04-27 11:31:15 UTC (rev 99532)
+++ manuel/trunk/src/manuel/__init__.py	2009-04-27 12:36:49 UTC (rev 99533)
@@ -1,7 +1,20 @@
 import imp
 import re
 
+# constants for use with "timing" decorator
+EARLY = 'early'
+LATE = 'late'
 
+
+def timing(timing):
+    assert timing in (EARLY, LATE)
+    def decorate(func):
+        func.manuel_timing = timing
+        return func
+
+    return decorate
+
+
 def absolute_import(name):
     return imp.load_module(name, *imp.find_module(name))
 
@@ -272,6 +285,15 @@
         else:
             self.formatters = []
 
+    def add_parser(self, parser):
+        self.parsers.append(parser)
+
+    def add_evaluater(self, evaluater):
+        self.evaluaters.append(evaluater)
+
+    def add_formatter(self, formatter):
+        self.formatters.append(formatter)
+
     def extend(self, other):
         self.parsers.extend(other.parsers)
         self.evaluaters.extend(other.evaluaters)

Modified: manuel/trunk/src/manuel/doctest.py
===================================================================
--- manuel/trunk/src/manuel/doctest.py	2009-04-27 11:31:15 UTC (rev 99532)
+++ manuel/trunk/src/manuel/doctest.py	2009-04-27 12:36:49 UTC (rev 99533)
@@ -3,6 +3,7 @@
 import os.path
 
 doctest = manuel.absolute_import('doctest')
+#from zope.testing import doctest
 
 class DocTestResult(StringIO.StringIO):
     pass

Modified: manuel/trunk/src/manuel/footnote.py
===================================================================
--- manuel/trunk/src/manuel/footnote.py	2009-04-27 11:31:15 UTC (rev 99532)
+++ manuel/trunk/src/manuel/footnote.py	2009-04-27 12:36:49 UTC (rev 99533)
@@ -18,6 +18,7 @@
         self.name = name
 
 
+ at manuel.timing(manuel.EARLY)
 def find_footnote_references(document):
     # find the markers that show where footnotes have been defined.
     footnote_names = []
@@ -37,9 +38,8 @@
         assert names
         document.replace_region(region, FootnoteReference(names))
 
-find_footnote_references.manuel_timing = 'early'
 
-
+ at manuel.timing(manuel.LATE)
 def do_footnotes(document):
     """Copy footnoted items into their appropriate position.
     """
@@ -70,9 +70,7 @@
                 document.insert_region_before(region, footnoted.copy())
         document.remove_region(region)
 
-do_footnotes.manuel_timing = 'late'
 
-
 class Manuel(manuel.Manuel):
     def __init__(self):
         manuel.Manuel.__init__(self, [find_footnote_references, do_footnotes])

Modified: manuel/trunk/src/manuel/tests.py
===================================================================
--- manuel/trunk/src/manuel/tests.py	2009-04-27 11:31:15 UTC (rev 99532)
+++ manuel/trunk/src/manuel/tests.py	2009-04-27 12:36:49 UTC (rev 99533)
@@ -5,8 +5,8 @@
 import re
 import unittest
 
-doctest = manuel.absolute_import('doctest')
-#from zope.testing import doctest
+#doctest = manuel.absolute_import('doctest')
+from zope.testing import doctest
 
 def test_suite():
     optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS



More information about the Checkins mailing list