Documents¶
Document
dataclass
¶
A TouchOSC layout: one root control plus the format version.
Attributes:
| Name | Type | Description |
|---|---|---|
root |
Control
|
The group every other control lives inside. |
version |
str
|
The |
Source code in src/py2tosc/document.py
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 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 | |
new
classmethod
¶
Create an empty layout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
tuple[int, int, int, int]
|
The canvas position and size. |
(0, 0, 1024, 768)
|
**kwargs
|
Any
|
Further properties for the root group. |
{}
|
Returns:
| Type | Description |
|---|---|
Document
|
A document holding an empty root group. |
Source code in src/py2tosc/document.py
find ¶
Find the first control in the layout matching name and/or type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
The |
None
|
type
|
ControlType | str | None
|
The control type to match. |
None
|
Returns:
| Type | Description |
|---|---|
Control | None
|
The first match in depth-first order, or None. |
Source code in src/py2tosc/document.py
find_all ¶
Find every control in the layout matching name and/or type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
The |
None
|
type
|
ControlType | str | None
|
The control type to match. |
None
|
Returns:
| Type | Description |
|---|---|
list[Control]
|
All matches in depth-first order. |
Source code in src/py2tosc/document.py
walk ¶
Iterate over every control in the layout, depth first.
Yields:
| Type | Description |
|---|---|
Control
|
The root, then each control beneath it. |
add ¶
resolve ¶
Assign frames to everything the layout combinators described.
The combinators in py2tosc.ui record an arrangement without sizing
anything, since a layout can only divide a frame it knows. This is the
pass that hands the root's frame down the tree.
Call it when you want the frames before writing anything -- to read them, or to check them. Saving places whatever is still unplaced, so a layout is never written out unsized, but it will not re-run a layout that was already resolved. This will, which is how a tree is re-laid out after its root frame changes.
Returns:
| Type | Description |
|---|---|
Document
|
This document, so calls can be chained. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a layout cannot fit its children into its space. |
Source code in src/py2tosc/document.py
validate ¶
dumps ¶
Serialize the layout to XML text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pretty
|
bool
|
Emit one element per line, matching the editor's XML export. |
False
|
validate
|
bool
|
Check the layout first and refuse to serialize if it has errors. Off by default, because a rule in the checker being wrong should not stop you writing a file. |
False
|
Unlike save, this does not place an unresolved layout, and the
difference is deliberate rather than an oversight. save writes a file
for TouchOSC to open, where an unplaced layout is never what anyone
wanted; dumps is for looking at the tree, and while debugging a
layout the unplaced state is exactly what you need to see.
Returns:
| Type | Description |
|---|---|
str
|
The complete XML document. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If |
Source code in src/py2tosc/document.py
save ¶
Write the layout to disk.
The format follows the file extension: .xml writes readable XML, and
anything else writes a zlib-compressed .tosc that TouchOSC can open.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
_PathLike
|
Where to write. A |
required |
pretty
|
bool | None
|
Override the line-per-element formatting that the extension would otherwise choose. |
None
|
validate
|
bool
|
Check the layout first and write nothing if it has errors. This is the checkpoint worth using it at: the mistake is caught before the file exists, rather than when TouchOSC refuses it. |
False
|
Any layout the combinators described but nobody resolved is placed first, since the alternative is writing a file whose every control sits at the origin -- structurally valid, byte-exact on a round trip, and visibly wrong in TouchOSC. A layout that was already resolved is left as it is, so a frame placed by hand inside one survives. Loading a file and saving it again is unaffected: a loaded control carries no layout.
Raises:
| Type | Description |
|---|---|
ValidationError
|
If |
ValueError
|
If a layout cannot fit its children into its space. |
Source code in src/py2tosc/document.py
load ¶
Read a layout from a .tosc or .xml file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
_PathLike
|
The file to read. Either format is accepted. |
required |
Returns:
| Type | Description |
|---|---|
Document
|
The parsed document. |
Raises:
| Type | Description |
|---|---|
FormatError
|
If the file is neither valid XML nor a valid |
OSError
|
If the file cannot be read. Left as the builtin, since nothing about it is specific to this format. |
Source code in src/py2tosc/document.py
loads ¶
Parse a layout from XML text or from raw .tosc bytes.
Compressed input is detected and decompressed automatically, so this accepts either form without being told which it was given.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str | bytes
|
XML text, XML bytes, or the contents of a |
required |
Returns:
| Type | Description |
|---|---|
Document
|
The parsed document. |
Raises:
| Type | Description |
|---|---|
FormatError
|
If the input is neither valid XML nor a valid |
Source code in src/py2tosc/document.py
save ¶
Write a document to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document
|
Document
|
The layout to write. |
required |
path
|
_PathLike
|
Where to write. A |
required |
pretty
|
bool | None
|
Override the formatting the extension would choose. |
None
|
Source code in src/py2tosc/document.py
dumps ¶
Serialize a document to XML text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document
|
Document
|
The layout to serialize. |
required |
pretty
|
bool
|
Emit one element per line, matching the editor's XML export. |
False
|
Returns:
| Type | Description |
|---|---|
str
|
The complete XML document. |