File: execforresourcemap_directive.py

package info (click to toggle)
openturns 1.26-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,708 kB
  • sloc: cpp: 261,605; python: 67,030; ansic: 4,378; javascript: 406; sh: 185; xml: 164; makefile: 101
file content (111 lines) | stat: -rw-r--r-- 3,169 bytes parent folder | download
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
103
104
105
106
107
108
109
110
111
import sys
from os.path import basename
import openturns as ot


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


class execforresourcemap_directive(Directive):
    """Execute the specified python code and insert the output into the document"""

    has_content = True

    def run(self):
        oldStdout, sys.stdout = sys.stdout, StringIO()
        source = self.state_machine.input_lines.source(
            self.lineno - self.state_machine.input_offset - 1
        )
        try:
            table = nodes.table()

            tgroup = nodes.tgroup(cols=3)
            table += tgroup
            tgroup += nodes.colspec(colwidth=25, classes=["key"])
            tgroup += nodes.colspec(colwidth=8, classes=["value"])
            tgroup += nodes.colspec(colwidth=8, classes=["type"])
            thead = nodes.thead()
            tgroup += thead

            # Add headers
            row = nodes.row()
            thead += row

            entry = nodes.entry()
            row += entry
            node = nodes.paragraph(text="Key")
            entry += node

            entry = nodes.entry()
            row += entry
            node = nodes.paragraph(text="Value")
            entry += node

            entry = nodes.entry()
            row += entry
            node = nodes.paragraph(text="Type")
            entry += node

            # Add body
            tbody = nodes.tbody()
            tgroup += tbody
            row = nodes.row()
            tbody += row

            # reset state in case altered by examples
            ot.ResourceMap.Reset()

            for key in ot.ResourceMap.GetKeys():
                row = nodes.row()
                tbody += row

                entry = nodes.entry()
                row += entry

                node = nodes.paragraph(text=key)
                entry += node

                entry = nodes.entry()
                row += entry

                value = ot.ResourceMap.Get(key)
                if not len(value):
                    value = " ".__repr__()
                if "\t" in value:
                    value = value.replace("\t", "\\t")
                node = nodes.paragraph(text=value)
                entry += node

                entry = nodes.entry()
                row += entry

                keyType = ot.ResourceMap.GetType(key)
                node = nodes.paragraph(text=keyType)
                entry += node

            return [table]
        except Exception:
            return [
                nodes.error(
                    None,
                    nodes.paragraph(
                        text="Unable to execute python code at %s:%d:"
                        % (basename(source), self.lineno)
                    ),
                    nodes.paragraph(text=str(sys.exc_info()[1])),
                )
            ]
        finally:
            sys.stdout = oldStdout


def setup(app):
    setup.app = app
    setup.config = app.config
    setup.confdir = app.confdir
    app.add_directive("execforresourcemap", execforresourcemap_directive)

    metadata = {"version": "0", "parallel_read_safe": True}
    return metadata