File: pythonscript_sphinxext.py

package info (click to toggle)
yt 4.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,084 kB
  • sloc: python: 132,484; ansic: 5,628; cpp: 1,588; javascript: 352; makefile: 138; sh: 43; csh: 36
file content (102 lines) | stat: -rw-r--r-- 2,769 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import errno
import glob
import os
import shutil
import subprocess
import tempfile
import time
import uuid

from docutils import nodes
from docutils.parsers.rst import Directive


class PythonScriptDirective(Directive):
    """Execute an inline python script and display images.

    This uses exec to execute an inline python script, copies
    any images produced by the script, and embeds them in the document
    along with the script.

    """

    required_arguments = 0
    optional_arguments = 0
    has_content = True

    def run(self):
        cwd = os.getcwd()
        tmpdir = tempfile.mkdtemp()
        os.chdir(tmpdir)

        rst_file = self.state_machine.document.attributes["source"]
        rst_dir = os.path.abspath(os.path.dirname(rst_file))

        image_dir, image_rel_dir = make_image_dir(setup, rst_dir)

        # Construct script from cell content
        content = "\n".join(self.content)
        with open("temp.py", "w") as f:
            f.write(content)

        # Use sphinx logger?
        uid = uuid.uuid4().hex[:8]
        print("")
        print(f">> Contents of the script: {uid}")
        print(content)
        print("")

        start = time.time()
        subprocess.call(["python", "temp.py"])
        print(f">> The execution of the script {uid} took {time.time() - start:f} s")
        text = ""
        for im in sorted(glob.glob("*.png")):
            text += get_image_tag(im, image_dir, image_rel_dir)

        code = content

        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
    setup.config = app.config
    setup.confdir = app.confdir

    retdict = dict(version="0.1", parallel_read_safe=True, parallel_write_safe=True)

    return retdict


def get_image_tag(filename, image_dir, image_rel_dir):
    my_uuid = uuid.uuid4().hex
    shutil.move(filename, image_dir + os.path.sep + my_uuid + filename)
    relative_filename = image_rel_dir + os.path.sep + my_uuid + filename
    return f'<img src="{relative_filename}" width="600"><br>'


def make_image_dir(setup, rst_dir):
    image_dir = os.path.join(setup.app.builder.outdir, "_images")
    rel_dir = os.path.relpath(setup.confdir, rst_dir)
    image_rel_dir = os.path.join(rel_dir, "_images")
    thread_safe_mkdir(image_dir)
    return image_dir, image_rel_dir


def thread_safe_mkdir(dirname):
    try:
        os.makedirs(dirname)
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise