drawpyo 0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- drawpyo/__init__.py +8 -0
- drawpyo/diagram/__init__.py +3 -0
- drawpyo/diagram/base_diagram.py +296 -0
- drawpyo/diagram/edges.py +564 -0
- drawpyo/diagram/objects.py +722 -0
- drawpyo/diagram_types/__init__.py +2 -0
- drawpyo/diagram_types/class_diagram.py +35 -0
- drawpyo/diagram_types/tree.py +571 -0
- drawpyo/file.py +141 -0
- drawpyo/formatting_database/edge_styles.toml +147 -0
- drawpyo/formatting_database/line_styles.toml +9 -0
- drawpyo/page.py +166 -0
- drawpyo/shape_libraries/flowchart.toml +294 -0
- drawpyo/shape_libraries/general.toml +195 -0
- drawpyo/xml_base.py +111 -0
- drawpyo-0.1.dist-info/METADATA +141 -0
- drawpyo-0.1.dist-info/RECORD +18 -0
- drawpyo-0.1.dist-info/WHEEL +4 -0
drawpyo/__init__.py
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
from ..xml_base import XMLBase
|
|
2
|
+
from os import path
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"DiagramBase",
|
|
7
|
+
"style_str_from_dict",
|
|
8
|
+
"import_shape_database",
|
|
9
|
+
"color_input_check",
|
|
10
|
+
]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def color_input_check(color_str):
|
|
14
|
+
if color_str == None:
|
|
15
|
+
return None
|
|
16
|
+
elif color_str == "none":
|
|
17
|
+
return color_str
|
|
18
|
+
elif color_str == "default":
|
|
19
|
+
return color_str
|
|
20
|
+
elif color_str[0] == "#" and len(color_str) == 7:
|
|
21
|
+
return color_str
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def import_shape_database(file_name, relative=False):
|
|
25
|
+
"""
|
|
26
|
+
This function imports a TOML shape database and returns a dictionary of the
|
|
27
|
+
shapes defined therein. It supports inheritance, meaning that if there is
|
|
28
|
+
an inherit value in any of the shape dictionaries it will attempt to go
|
|
29
|
+
find the inherited master shape and use it as a starting format, but
|
|
30
|
+
overwriting any styles defined in both with the style defined in the child
|
|
31
|
+
object.
|
|
32
|
+
|
|
33
|
+
Parameters
|
|
34
|
+
----------
|
|
35
|
+
filename : str
|
|
36
|
+
The path to a TOML file containing a style library database.
|
|
37
|
+
|
|
38
|
+
Returns
|
|
39
|
+
-------
|
|
40
|
+
data : dict
|
|
41
|
+
A database of shapes defined in the TOML file.
|
|
42
|
+
|
|
43
|
+
"""
|
|
44
|
+
# Import the shape and edge definitions
|
|
45
|
+
from sys import version_info
|
|
46
|
+
|
|
47
|
+
if relative:
|
|
48
|
+
# toml path
|
|
49
|
+
dirname = path.dirname(__file__)
|
|
50
|
+
dirname = path.split(dirname)[0]
|
|
51
|
+
file_name = path.join(dirname, file_name)
|
|
52
|
+
|
|
53
|
+
if version_info.minor < 11:
|
|
54
|
+
import toml
|
|
55
|
+
|
|
56
|
+
data = toml.load(file_name)
|
|
57
|
+
else:
|
|
58
|
+
import tomllib
|
|
59
|
+
|
|
60
|
+
with open(file_name, "rb") as f:
|
|
61
|
+
data = tomllib.load(f)
|
|
62
|
+
|
|
63
|
+
for obj in data.values():
|
|
64
|
+
if "inherit" in obj:
|
|
65
|
+
obj.update(data[obj["inherit"]])
|
|
66
|
+
|
|
67
|
+
return data
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def style_str_from_dict(style_dict):
|
|
71
|
+
"""
|
|
72
|
+
This function returns a concatenated style string from a style dictionary.
|
|
73
|
+
This format is:
|
|
74
|
+
baseStyle;attr1=value;attr2=value
|
|
75
|
+
It will concatenate the key:value pairs with the appropriate semicolons and
|
|
76
|
+
equals except for the baseStyle, which it will prepend to the front with no
|
|
77
|
+
equals sign.
|
|
78
|
+
|
|
79
|
+
Parameters
|
|
80
|
+
----------
|
|
81
|
+
style_dict : dict
|
|
82
|
+
A dictionary of style:value pairs.
|
|
83
|
+
|
|
84
|
+
Returns
|
|
85
|
+
-------
|
|
86
|
+
str
|
|
87
|
+
A string with the style_dicts values concatenated correctly.
|
|
88
|
+
|
|
89
|
+
"""
|
|
90
|
+
if "baseStyle" in style_dict:
|
|
91
|
+
style_str = [style_dict.pop("baseStyle")]
|
|
92
|
+
else:
|
|
93
|
+
style_str = []
|
|
94
|
+
style_str = style_str + [
|
|
95
|
+
"{0}={1}".format(att, style)
|
|
96
|
+
for (att, style) in style_dict.items()
|
|
97
|
+
if style != "" and style != None
|
|
98
|
+
]
|
|
99
|
+
return ";".join(style_str)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
class DiagramBase(XMLBase):
|
|
103
|
+
"""
|
|
104
|
+
This class is the base for all diagram objects to inherit from. It defines some general creation methods and properties to make diagram objects printable and useful.
|
|
105
|
+
"""
|
|
106
|
+
|
|
107
|
+
def __init__(self, **kwargs):
|
|
108
|
+
super().__init__(**kwargs)
|
|
109
|
+
self._style_attributes = ["html"]
|
|
110
|
+
self.page = kwargs.get("page", None)
|
|
111
|
+
self.xml_parent = kwargs.get("xml_parent", None)
|
|
112
|
+
|
|
113
|
+
@classmethod
|
|
114
|
+
def create_from_library(cls, library, obj):
|
|
115
|
+
return cls
|
|
116
|
+
|
|
117
|
+
# XML_parent property
|
|
118
|
+
@property
|
|
119
|
+
def xml_parent_id(self):
|
|
120
|
+
if self.xml_parent is not None:
|
|
121
|
+
return self.xml_parent.id
|
|
122
|
+
else:
|
|
123
|
+
return 1
|
|
124
|
+
|
|
125
|
+
# Parent object linking
|
|
126
|
+
@property
|
|
127
|
+
def xml_parent(self):
|
|
128
|
+
return self._xml_parent
|
|
129
|
+
|
|
130
|
+
@xml_parent.setter
|
|
131
|
+
def xml_parent(self, p):
|
|
132
|
+
if p is not None:
|
|
133
|
+
p.add_object(self)
|
|
134
|
+
self._xml_parent = p
|
|
135
|
+
else:
|
|
136
|
+
self._xml_parent = None
|
|
137
|
+
|
|
138
|
+
@xml_parent.deleter
|
|
139
|
+
def xml_parent(self):
|
|
140
|
+
self._xml_parent.remove_object(self)
|
|
141
|
+
self._xml_parent = None
|
|
142
|
+
|
|
143
|
+
# Page property
|
|
144
|
+
@property
|
|
145
|
+
def page_id(self):
|
|
146
|
+
if self.page is not None:
|
|
147
|
+
return self.page.id
|
|
148
|
+
else:
|
|
149
|
+
return 1
|
|
150
|
+
|
|
151
|
+
# page object linking
|
|
152
|
+
@property
|
|
153
|
+
def page(self):
|
|
154
|
+
return self._page
|
|
155
|
+
|
|
156
|
+
@page.setter
|
|
157
|
+
def page(self, p):
|
|
158
|
+
if p is not None:
|
|
159
|
+
p.add_object(self)
|
|
160
|
+
self._page = p
|
|
161
|
+
else:
|
|
162
|
+
self._page = None
|
|
163
|
+
|
|
164
|
+
@page.deleter
|
|
165
|
+
def page(self):
|
|
166
|
+
self._page.remove_object(self)
|
|
167
|
+
self._page = None
|
|
168
|
+
|
|
169
|
+
###########################################################
|
|
170
|
+
# Style properties
|
|
171
|
+
###########################################################
|
|
172
|
+
def add_style_attribute(self, style_attr):
|
|
173
|
+
if style_attr not in self._style_attributes:
|
|
174
|
+
self._style_attributes.append(style_attr)
|
|
175
|
+
|
|
176
|
+
@property
|
|
177
|
+
def style_attributes(self):
|
|
178
|
+
"""
|
|
179
|
+
The style attributes are the list of style tags that should be printed into the style XML attribute. This is a subset of the attributes defined on the object method.
|
|
180
|
+
|
|
181
|
+
Returns:
|
|
182
|
+
list: A list of the names of the style_attributes.
|
|
183
|
+
"""
|
|
184
|
+
return self._style_attributes
|
|
185
|
+
|
|
186
|
+
@style_attributes.setter
|
|
187
|
+
def style_attributes(self, value):
|
|
188
|
+
self._style_attributes = value
|
|
189
|
+
|
|
190
|
+
@property
|
|
191
|
+
def style(self):
|
|
192
|
+
"""
|
|
193
|
+
This function returns the style string of the object to be appended into the style XML attribute.
|
|
194
|
+
|
|
195
|
+
First it searches the object properties called out in
|
|
196
|
+
self.style_attributes. If the property is initialized to something
|
|
197
|
+
that isn't None or an empty string, it will add it. Otherwise it
|
|
198
|
+
searches the base_style defined by the object template.
|
|
199
|
+
|
|
200
|
+
Returns:
|
|
201
|
+
str: The style string of the object.
|
|
202
|
+
|
|
203
|
+
"""
|
|
204
|
+
|
|
205
|
+
style_str = ""
|
|
206
|
+
if (
|
|
207
|
+
hasattr(self, "baseStyle")
|
|
208
|
+
and getattr(self, "baseStyle") is not None
|
|
209
|
+
and getattr(self, "baseStyle") != ""
|
|
210
|
+
):
|
|
211
|
+
style_str = getattr(self, "baseStyle") + ";"
|
|
212
|
+
|
|
213
|
+
for attribute in self.style_attributes:
|
|
214
|
+
if hasattr(self, attribute) and getattr(self, attribute) is not None:
|
|
215
|
+
attr_val = getattr(self, attribute)
|
|
216
|
+
# reformat different datatypes to strings
|
|
217
|
+
if isinstance(attr_val, bool):
|
|
218
|
+
attr_val = format(attr_val * 1)
|
|
219
|
+
style_str = style_str + "{0}={1};".format(attribute, attr_val)
|
|
220
|
+
return style_str
|
|
221
|
+
|
|
222
|
+
def _add_and_set_style_attrib(self, attrib, value):
|
|
223
|
+
if hasattr(self, attrib):
|
|
224
|
+
setattr(self, attrib, value)
|
|
225
|
+
else:
|
|
226
|
+
setattr(self, attrib, value)
|
|
227
|
+
self.add_style_attribute(attrib)
|
|
228
|
+
|
|
229
|
+
def apply_style_string(self, style_str):
|
|
230
|
+
"""
|
|
231
|
+
This function will apply a passed in style string to the object. This style string can be obtained from the Draw.io app by selecting Edit Style from the context menu of any object. This function will iterate through the attributes in the style string and assign the corresponding property the value.
|
|
232
|
+
|
|
233
|
+
Args:
|
|
234
|
+
style_str (str): A Draw.io style string
|
|
235
|
+
"""
|
|
236
|
+
for attrib in style_str.split(";"):
|
|
237
|
+
if attrib == "":
|
|
238
|
+
pass
|
|
239
|
+
elif "=" in attrib:
|
|
240
|
+
a_name = attrib.split("=")[0]
|
|
241
|
+
a_value = attrib.split("=")[1]
|
|
242
|
+
if a_value.isdigit():
|
|
243
|
+
if "." in a_value:
|
|
244
|
+
a_value = float(a_value)
|
|
245
|
+
else:
|
|
246
|
+
a_value = int(a_value)
|
|
247
|
+
elif a_value == "True" or a_value == "False":
|
|
248
|
+
a_value = bool(a_value)
|
|
249
|
+
|
|
250
|
+
self._add_and_set_style_attrib(a_name, a_value)
|
|
251
|
+
else:
|
|
252
|
+
self.baseStyle = attrib
|
|
253
|
+
|
|
254
|
+
def _apply_style_from_template(self, template):
|
|
255
|
+
for attrib in template.style_attributes:
|
|
256
|
+
value = getattr(template, attrib)
|
|
257
|
+
self._add_and_set_style_attrib(attrib, value)
|
|
258
|
+
|
|
259
|
+
def apply_attribute_dict(self, attr_dict):
|
|
260
|
+
"""
|
|
261
|
+
This function takes in a dictionary of attributes and applies them
|
|
262
|
+
to the object. These attributes can be style or properties. If the
|
|
263
|
+
attribute isn't already defined as a property of the class it's
|
|
264
|
+
assumed to be a style attribute. It will then be added as a property
|
|
265
|
+
and also appended to the .style_attributes list.
|
|
266
|
+
|
|
267
|
+
Parameters
|
|
268
|
+
----------
|
|
269
|
+
attr_dict : dict
|
|
270
|
+
A dictionary of attributes to set or add to the object.
|
|
271
|
+
|
|
272
|
+
Returns
|
|
273
|
+
-------
|
|
274
|
+
None.
|
|
275
|
+
|
|
276
|
+
"""
|
|
277
|
+
for attr, val in attr_dict.items():
|
|
278
|
+
self._add_and_set_style_attrib(attr, val)
|
|
279
|
+
|
|
280
|
+
@classmethod
|
|
281
|
+
def from_style_string(cls, style_string):
|
|
282
|
+
"""
|
|
283
|
+
This classmethod allows the intantiation of an object from a style
|
|
284
|
+
string. This is useful since Draw.io allows copying the style string
|
|
285
|
+
out of an object in their UI. This string can then be copied into the
|
|
286
|
+
Python environment and further objects created that match the style.
|
|
287
|
+
|
|
288
|
+
Args:
|
|
289
|
+
style_string (str): A Draw.io style string
|
|
290
|
+
|
|
291
|
+
Returns:
|
|
292
|
+
BaseDiagram: A BaseDiagram or subclass instantiated with the style from the Draw.io string
|
|
293
|
+
"""
|
|
294
|
+
new_obj = cls()
|
|
295
|
+
new_obj.apply_style_string(style_string)
|
|
296
|
+
return new_obj
|