forked from fgmacedo/python-statemachine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape_images.py
More file actions
64 lines (47 loc) · 2.11 KB
/
scrape_images.py
File metadata and controls
64 lines (47 loc) · 2.11 KB
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
import os
import re
from statemachine.contrib.diagram import DotGraphMachine
from statemachine.factory import StateMachineMetaclass
from .helpers import import_module_by_path
class MachineScraper:
"""Scrapes images of the statemachines defined into the examples for the gallery"""
re_replace_png_extension = re.compile(r"\.png$")
def __init__(self, project_root):
self.project_root = project_root
sanitized_path = re.escape(os.path.abspath(self.project_root))
self.re_machine_module_name = re.compile(f"{sanitized_path}/(.*)\\.py$")
self.seen = set()
def __repr__(self):
return "MachineScraper"
def _get_module(self, src_file):
module_name = self.re_machine_module_name.findall(src_file)
if len(module_name) != 1:
return
return import_module_by_path(module_name[0])
def generate_image(self, sm_class, original_path):
image_path = self.re_replace_png_extension.sub(".svg", original_path)
svg = DotGraphMachine(sm_class).get_graph().create_svg().decode()
with open(image_path, "w") as f:
f.write(svg)
return image_path
def __call__(self, block, block_vars, gallery_conf):
"Find all PNG files in the directory of this example."
from sphinx_gallery.scrapers import figure_rst
module = self._get_module(block_vars["src_file"])
if module is None:
return ""
image_names = []
image_path_iterator = block_vars["image_path_iterator"]
for key, value in module.__dict__.items():
unique_key = f"{module.__name__}.{key}"
if (
key.startswith("__")
or unique_key in self.seen
or not isinstance(value, StateMachineMetaclass)
or value._abstract
):
continue
self.seen.add(unique_key)
image_names.append(self.generate_image(value, image_path_iterator.next()))
# Use the `figure_rst` helper function to generate rST for image files
return figure_rst(image_names, gallery_conf["src_dir"])