[yt-svn] commit/yt: xarthisius: Make pythonscript_sphinxext.py parallel safe

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Fri Dec 5 09:19:08 PST 2014


1 new commit in yt:

https://bitbucket.org/yt_analysis/yt/commits/09a0a10893ef/
Changeset:   09a0a10893ef
Branch:      yt
User:        xarthisius
Date:        2014-12-05 17:16:29+00:00
Summary:     Make pythonscript_sphinxext.py parallel safe
Affected #:  1 file

diff -r 539fc4d8afb4c69c8948e5bb42b2d0dfcc817916 -r 09a0a10893ef77dce53dd67269577154b669d5bd doc/extensions/pythonscript_sphinxext.py
--- a/doc/extensions/pythonscript_sphinxext.py
+++ b/doc/extensions/pythonscript_sphinxext.py
@@ -1,8 +1,12 @@
+import tempfile
+import os
+import glob
+import base64
+import shutil
+import subprocess
 from sphinx.util.compat import Directive
-from subprocess import Popen,PIPE
-from docutils.parsers.rst import directives
 from docutils import nodes
-import os, glob, base64
+
 
 class PythonScriptDirective(Directive):
     """Execute an inline python script and display images.
@@ -17,6 +21,10 @@
     has_content = True
 
     def run(self):
+        cwd = os.getcwd()
+        tmpdir = tempfile.mkdtemp()
+        os.chdir(tmpdir)
+
         # Construct script from cell content
         content = "\n".join(self.content)
         with open("temp.py", "w") as f:
@@ -27,28 +35,27 @@
         print content
         print ""
 
-        codeproc = Popen(['python', 'temp.py'], stdout=PIPE)
-        out = codeproc.stdout.read()
+        subprocess.call(['python', 'temp.py'])
 
-        images = sorted(glob.glob("*.png"))
-        fns = []
         text = ''
-        for im in images:
+        for im in sorted(glob.glob("*.png")):
             text += get_image_tag(im)
-            os.remove(im)
-            
-        os.remove("temp.py")
 
         code = content
 
-        literal = nodes.literal_block(code,code)
+        literal = nodes.literal_block(code, code)
         literal['language'] = 'python'
 
         attributes = {'format': 'html'}
         img_node = nodes.raw('', text, **attributes)
-        
+
+        # clean up
+        os.chdir(cwd)
+        shutil.rmtree(tmpdir, True)
+
         return [literal, img_node]
 
+
 def setup(app):
     app.add_directive('python-script', PythonScriptDirective)
     setup.app = app
@@ -63,7 +70,9 @@
 
     return retdict
 
+
 def get_image_tag(filename):
     with open(filename, "rb") as image_file:
         encoded_string = base64.b64encode(image_file.read())
-        return '<img src="data:image/png;base64,%s" width="600"><br>' % encoded_string
+        return '<img src="data:image/png;base64,%s" width="600"><br>' \
+            % encoded_string

Repository URL: https://bitbucket.org/yt_analysis/yt/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.



More information about the yt-svn mailing list