-
Notifications
You must be signed in to change notification settings - Fork 1
/
blockdiag_helper.py
163 lines (138 loc) · 3.93 KB
/
blockdiag_helper.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# coding: utf-8
import os
import io
import glob
def _create_fontmap(fontmap, font):
"""
Inspired from :epkg:`blockdiag` source file (*_bootstrap.py*).
"""
from blockdiag.utils.fontmap import FontMap
fontmap = FontMap(fontmap)
if fontmap.find().path is None or font:
fontpath = _detectfont(font)
fontmap.set_default_font(fontpath)
return fontmap
def _build_diagram(
module,
builder,
drawer,
tree,
type,
code,
fontmap=None,
antialias=True,
nodoctype=False,
transparency=False,
size=None,
):
"""
Inspired from :epkg:`blockdiag` source file (*_bootstrap.py*).
"""
ScreenNodeBuilder = builder.ScreenNodeBuilder
diagram = ScreenNodeBuilder.build(tree, None)
DiagramDraw = drawer.DiagramDraw
drawer = DiagramDraw(
type,
diagram,
fontmap=fontmap,
code=code,
antialias=antialias,
nodoctype=nodoctype,
transparency=transparency,
)
drawer.draw()
if size:
return drawer.save(size=size)
else:
return drawer.save()
def _detectfont(font):
fontdirs = [
"/usr/share/fonts",
"/Library/Fonts",
"/System/Library/Fonts",
"c:/windows/fonts",
"/usr/local/share/font-*",
]
fontfiles = [
"ipagp.ttf",
"ipagp.otf",
"VL-PGothic-Regular.ttf",
"Hiragino Sans GB W3.otf",
"AppleGothic.ttf",
"msgothic.ttf",
"msgoth04.ttf",
"msgothic.ttc",
]
fontpath = None
if font:
from blockdiag.utils.fontmap import parse_fontpath
for path in font:
_path, _ = parse_fontpath(path)
if os.path.isfile(_path):
fontpath = path
break
else:
msg = "fontfile is not found: %s" % font
raise RuntimeError(msg)
if fontpath is None:
globber = (glob.glob(d) for d in fontdirs)
for fontdir in sum(globber, []):
for root, _, files in os.walk(fontdir):
for font_ in fontfiles:
if font_ in files:
fontpath = os.path.join(root, font_)
break
return fontpath
def draw_diagram(graph, module="blockdiag", fmt="pillow", **options):
"""
Draws a graph based on module :epkg:`blockdiag`.
:param graph: definition, see `blockdiag <http://blockdiag.com/en/>`_
:param module: ``'blockdiag'`` (only available value)
:param fmt: can be a filename or a module name (``'pillow'``)
:param options: additional options for :epkg:`blockdiag`
:return: graph
::
blockdiag {
A -> B -> C -> D;
A -> E -> F -> G;
}
"""
if module == "blockdiag":
import blockdiag
module = blockdiag
import blockdiag.parser
parser = blockdiag.parser
import blockdiag.builder
builder = blockdiag.builder
import blockdiag.drawer
drawer = blockdiag.drawer
else:
raise ValueError("Unexected value for 'blockdiag': '{0}'".format(module))
if fmt in ("pillow", "png"):
ftype = "png"
elif fmt == "svg":
ftype = "svg"
else:
raise ValueError(f"fmt={fmt!r} should in ['pillow', 'svg', 'png']")
fontmap = _create_fontmap(
fontmap=options.get("fontmap", None), font=options.get("font", None)
)
tree = parser.parse_string(graph)
res = _build_diagram(
module=module,
builder=builder,
drawer=drawer,
tree=tree,
code=graph,
fontmap=fontmap,
type=ftype,
antialias=options.get("antialias", True),
nodoctype=options.get("nodoctype", False),
transparency=options.get("transparency", False),
size=options.get("size", None),
)
if fmt == "pillow":
from PIL import Image
image = Image.open(io.BytesIO(res))
return image
return res