Issues (108)

examples/check_examples.py (1 issue)

1
import os
2
import subprocess
3
import tempfile
4
import warnings
5
import importlib
6
from datetime import datetime
7
8
import matplotlib
9
import nbformat
10
from termcolor import colored
11
12
try:
13
    from oemof.visio import ESGraphRenderer
14
15
    oemof_visio = True
16
except ImportError:
17
    oemof_visio = False
18
19
warnings.filterwarnings("ignore", "", UserWarning)
20
matplotlib.use("Agg")
21
22
stop_at_error = False  # If True script will stop if error is raised
23
exclude_notebooks = False
24
exclude_python_scripts = False
25
has_main_function = True
26
test_optimize = True
27
28
29
def notebook_run(path):
30
    """
31
    Execute a notebook via nbconvert and collect output.
32
    Returns (parsed nb object, execution errors)
33
    """
34
    dirname, __ = os.path.split(path)
35
    os.chdir(dirname)
36
    with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
37
        args = [
38
            "jupyter",
39
            "nbconvert",
40
            "--to",
41
            "notebook",
42
            "--execute",
43
            "--ExecutePreprocessor.timeout=60",
44
            "--output",
45
            fout.name,
46
            path,
47
        ]
48
        subprocess.check_call(args)
49
50
        fout.seek(0)
51
        nb = nbformat.read(fout, nbformat.current_nbformat)
52
53
    errors = [
54
        output
55
        for cell in nb.cells
56
        if "outputs" in cell
57
        for output in cell["outputs"]
58
        if output.output_type == "error"
59
    ]
60
61
    return nb, errors
62
63
64
fullpath = os.path.dirname(__file__)
65
doc_path = os.path.join(os.path.dirname(fullpath), "docs", "_files")
66
67
checker = {}
68
number = 0
69
70
start_check = datetime.now()
71
72
for root, dirs, files in sorted(os.walk(fullpath)):
73
    if root != fullpath:
74
        for name in sorted(files):
75
            if name.endswith(".py"):
76
                number += 1
77
                module_name = name[:-3]
78
                try:
79
                    file_module = importlib.import_module(
80
                        f"{os.path.basename(root)}.{module_name}"
81
                    )
82
                    main = file_module.main
83
                    has_main_function = True
84
                except AttributeError:
85
                    print(
86
                        f"{os.path.basename(root)}.{name} does not have main() function"
87
                    )
88
                    has_main_function = False
89
90
                if stop_at_error is True:
91
                    es = main(optimize=test_optimize)
0 ignored issues
show
The variable main does not seem to be defined for all execution paths.
Loading history...
92
                    checker[name] = "okay"
93
                else:
94
                    try:
95
                        es = main(optimize=test_optimize)
96
                        checker[name] = "okay"
97
                    except Exception as e:
98
                        print(e)
99
                        if has_main_function is False:
100
                            checker[name] = "failed because no main() function"
101
                        else:
102
                            checker[name] = "failed"
103
                        es = None
104
105
                if es is not None and oemof_visio is True:
106
                    esgr = ESGraphRenderer(
107
                        es,
108
                        legend=False,
109
                        filepath=os.path.join(doc_path, f"{module_name}"),
110
                        img_format="svg",
111
                    )
112
                    esgr.render()
113
114
115
print("******* TEST RESULTS ***********************************")
116
117
print(
118
    "\n{0} examples tested in {1}.\n".format(
119
        number, datetime.now() - start_check
120
    )
121
)
122
123
f = 0
124
for k, v in checker.items():
125
    if "failed" in v:
126
        print(k, colored(v, "red"))
127
        f += 1
128
    else:
129
        print(k, colored(v, "green"))
130
131
print()
132
if f > 0:
133
    print("{0} of {1} examples failed!".format(f, number))
134
else:
135
    print("Congratulations! All examples are fine.")
136