Architecture
Drawpyo provides two high level classes to define critical methods and attributes for all exportable Draw.io objects. Primarily they define the parent and id attributes as well as a series of methods and properties for generating XML and style strings.
XMLBase
XMLBase is the base class for all exportable objects in drawpyo. This class defines a few useful properties that drawpyo needs to use to generate a Draw.io file.
Source code in src/drawpyo/xml_base.py
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 |
|
attributes
property
The most basic attributes of a Draw.io object. Extended by subclasses.
Returns:
Name | Type | Description |
---|---|---|
dict |
A dict containing an 'id' and 'xml_parent' object. |
id
property
id is a unique identifier. Draw.io generated diagrams use an ID many more characters but the app isn't picky when parsing so drawpyo just uses Python's built-in id() function as it guarantees unique identifiers.
Returns:
Name | Type | Description |
---|---|---|
int |
A unique identifier for the Draw.io object |
xml
property
All drawpyo exportable classes contain an xml property that returns the formatted string of their XML output.
This default version of the function assumes no inner value so it just returns the opening tag closed with a '/>'. Subclasses that require more printing overload this function with their own implementation.
Example:
Returns:
Name | Type | Description |
---|---|---|
str |
A single XML tag containing the object name, style attributes, and a closer. |
xml_close_tag
property
The closing tag contains the name of the object wrapped in angle brackets.
Example:
Returns:
Name | Type | Description |
---|---|---|
str |
The closing tag of the object with all the attributes. |
xml_open_tag
property
The open tag contains the name of the object but also the attribute tags. This property function concatenates all the attributes in the class along with the opening and closing angle brackets and returns them as a string.
Example:
Returns:
Name | Type | Description |
---|---|---|
str |
The opening tag of the object with all the attributes. |
DiagramBase
Bases: XMLBase
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.
Source code in src/drawpyo/diagram/base_diagram.py
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
|
style
property
This function returns the style string of the object to be appended into the style XML attribute.
First it searches the object properties called out in self.style_attributes. If the property is initialized to something that isn't None or an empty string, it will add it. Otherwise it searches the base_style defined by the object template.
Returns:
Name | Type | Description |
---|---|---|
str |
The style string of the object. |
style_attributes
property
writable
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.
Returns:
Name | Type | Description |
---|---|---|
list |
A list of the names of the style_attributes. |
apply_attribute_dict(attr_dict)
This function takes in a dictionary of attributes and applies them to the object. These attributes can be style or properties. If the attribute isn't already defined as a property of the class it's assumed to be a style attribute. It will then be added as a property and also appended to the .style_attributes list.
Parameters
attr_dict : dict A dictionary of attributes to set or add to the object.
Returns
None.
Source code in src/drawpyo/diagram/base_diagram.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
|
apply_style_string(style_str)
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.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
style_str |
str
|
A Draw.io style string |
required |
Source code in src/drawpyo/diagram/base_diagram.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
|
from_style_string(style_string)
classmethod
This classmethod allows the intantiation of an object from a style string. This is useful since Draw.io allows copying the style string out of an object in their UI. This string can then be copied into the Python environment and further objects created that match the style.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
style_string |
str
|
A Draw.io style string |
required |
Returns:
Name | Type | Description |
---|---|---|
BaseDiagram |
A BaseDiagram or subclass instantiated with the style from the Draw.io string |
Source code in src/drawpyo/diagram/base_diagram.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
|