Rev 5: Make sure we handle sub-process errors, in http://bzr.arbash-meinel.com/branches/bzr/extra/moin_graphviz

John Arbash Meinel john at arbash-meinel.com
Tue Apr 3 00:47:42 BST 2007


At http://bzr.arbash-meinel.com/branches/bzr/extra/moin_graphviz

------------------------------------------------------------
revno: 5
revision-id: john at arbash-meinel.com-20070402234736-fi7soeo351qgdxmy
parent: john at arbash-meinel.com-20070402231748-28s5n2c6ouft3urf
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: moin_graphviz
timestamp: Mon 2007-04-02 18:47:36 -0500
message:
  Make sure we handle sub-process errors,
  and a basic text that we generate a base64 image stream.
modified:
  graphviz.py                    graphviz.py-20070402222439-3qloodrjtlsuaz1o-1
  test_graphviz.py               test_graphviz.py-20070402223544-kez8ooyxqh1wx5w0-1
-------------- next part --------------
=== modified file 'graphviz.py'
--- a/graphviz.py	2007-04-02 23:07:56 +0000
+++ b/graphviz.py	2007-04-02 23:47:36 +0000
@@ -1,4 +1,18 @@
 # -*- coding: iso-8859-1 -*-
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
 """MoinMoin - Parse graphviz statements into PNG images.
 
 @copyright: 2006, Jeff Bailey <jbailey at raspberryginger.com>
@@ -13,6 +27,10 @@
 Dependencies = []
 
 
+class DotError(RuntimeError):
+    """Raised when we fail to run Dot"""
+
+
 class Parser(object):
     """Parse the output through graphviz to generate output."""
 
@@ -23,7 +41,6 @@
     def __init__(self, raw, request, **kw):
         self.raw = raw
         self.request = request
-        self.form = request.form
         # Used for i18n
         self._ = request.getText
 
@@ -31,6 +48,10 @@
         """Create the PNG image from the raw text."""
         fd = self._spawn_dot()
         (myout, myerr) = fd.communicate(self.raw)
+        if fd.returncode != 0:
+            raise DotError('%s failed with return code %s'
+                            % (' '.join(self._dot_command()),
+                               fd.returncode))
         return myout
 
     def _dot_command(self):
@@ -39,12 +60,14 @@
     def _spawn_dot(self):
         return Popen(self._dot_command(), stdin=PIPE, stdout=PIPE)
 
+    def _create_b64_png(self):
+        myout = self._create_png()
+        return b64encode(myout)
+
     def format(self, formatter):
-        """ Send the text. """
-        myout = self._create_png()
-        encoded = b64encode(myout)
+        """Send the text."""
         self.request.write('<img src="data:image/png;base64,')
-        self.request.write(encoded)
+        self.request.write(self._create_b64_png())
         self.request.write('" alt="Graphviz Image" />')
 
 

=== modified file 'test_graphviz.py'
--- a/test_graphviz.py	2007-04-02 23:17:23 +0000
+++ b/test_graphviz.py	2007-04-02 23:47:36 +0000
@@ -1,6 +1,23 @@
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
 """A test suite for the graphviz Moin plugin."""
 
+import base64
+import cStringIO
 import unittest
+import sys
 
 import graphviz
 
@@ -9,14 +26,12 @@
     """A Request object which conforms to what we need."""
 
     def __init__(self):
-        self._actions = []
-        self.form = None
+        self.output = cStringIO.StringIO()
 
     def write(self, txt):
-        self._actions.append(('write', txt))
+        self.output.write(txt)
 
     def getText(self, txt): # Wrapper for i18n
-        self._actions.append(('getText', txt))
         return txt
 
 
@@ -42,6 +57,13 @@
         return self._command
 
 
+class ParserWithForcedPng(graphviz.Parser):
+    """Override _create_png to return fixed text."""
+
+    def _create_png(self):
+        return '--start--\n' + self.raw + '\n--end--\n'
+
+
 class TestCaseWithParser(unittest.TestCase):
     """A simple class with convenience functions."""
 
@@ -135,3 +157,41 @@
 
         # _create_png should pass the raw text to the spawned command
         self.assertEqual('raw_text', p._create_png())
+
+    def test__create_png_handles_failure(self):
+        p = ParserWithCommand('raw_text', self.get_request())
+        p._command = [sys.executable, '-c',
+                        'import sys;'
+                        'sys.stdout.write(sys.stdin.read());'
+                        'sys.exit(1)']
+        # Pass through, but fail when done
+        self.assertRaises(graphviz.DotError, p._create_png)
+
+    def test__init__ignores_extra_args(self):
+        request = self.get_request()
+        p = graphviz.Parser('raw_text', request, extra=1)
+        p = graphviz.Parser('raw_text', request=request, other=2)
+
+    def test_format_base64(self):
+        """The output should end up as base64 encoded."""
+        request = self.get_request()
+        p = ParserWithForcedPng('raw_txt', request=request)
+        formatter = self.get_formatter()
+
+        expected_png_txt = ('--start--\n'
+                            'raw_txt'
+                            '\n--end--\n')
+        expected_base64_txt = base64.b64encode(expected_png_txt)
+
+        png_txt = p._create_png()
+        self.assertEqual('--start--\n'
+                         'raw_txt'
+                         '\n--end--\n',
+                         png_txt)
+
+        p.format(formatter)
+        expected_img_txt = ('<img src="data:image/png;base64,%s"'
+                            ' alt="Graphviz Image" />'
+                            % (expected_base64_txt,))
+        self.assertEqual(expected_img_txt,
+                         request.output.getvalue())



More information about the bazaar-commits mailing list