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
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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
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 the readable XML
export, .json writes the JSON encoding of
to_json, 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 formatting the extension would otherwise choose: one element per line for XML, one line per property for JSON. |
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, .xml or .json file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
_PathLike
|
The file to read. Any of the three formats is accepted, and the contents rather than the extension decides which it is. |
required |
Returns:
| Type | Description |
|---|---|
Document
|
The parsed document. |
Raises:
| Type | Description |
|---|---|
FormatError
|
If the file is not a readable layout in any of the three
formats.
This is a |
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, JSON text, or raw .tosc bytes.
Compressed input is detected and decompressed automatically, and JSON is
told from XML by its first character, so this accepts any of them without
being told which it was given. Either JSON dialect is read: the faithful
encoding of py2tosc.json_codec, and the layout description of
py2tosc.ui_json, which the envelope's format distinguishes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str | bytes
|
XML text or bytes, JSON text or bytes, or the contents of a
|
required |
Returns:
| Type | Description |
|---|---|
Document
|
The parsed document. |
Raises:
| Type | Description |
|---|---|
FormatError
|
If the input is not a readable layout in any of the three
forms. This is a |
Source code in src/py2tosc/document.py
edit ¶
edit(
path: _PathLike,
*,
save_as: _PathLike | None = None,
validate: bool = False,
pretty: bool | None = None,
) -> Iterator[Document]
Open a layout, change it, and write it back.
The file is read on the way in and written on the way out, so the two lines
that bracket an editing script become the with itself:
with py2tosc.edit("mixer.tosc") as doc:
for fader in doc.find_all(type="FADER"):
fader.set("color", "#e76f51")
The path is named once rather than twice, which is the mistake this is
mostly here to prevent: a load and a save far apart in a script are two
places for a filename to be wrong, and the one that writes is the one that
matters. Nothing is written if the block raises, so a save in the middle
of a script cannot leave a half-edited layout on disk either.
Anything the combinators described and nobody resolved is placed on the way
out, since Document.save does that. Editing a
loaded layout is unaffected: a loaded control carries no layout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
_PathLike
|
The layout to read. Any of the three formats, as
|
required |
save_as
|
_PathLike | None
|
Where to write, if not back over |
None
|
validate
|
bool
|
Check before writing and write nothing if the layout has
errors, as |
False
|
pretty
|
bool | None
|
Override the formatting the extension would choose. |
None
|
Yields:
| Type | Description |
|---|---|
Document
|
The document, to be edited in place. |
Raises:
| Type | Description |
|---|---|
FormatError
|
If the file is not a readable layout. |
ValidationError
|
If |
ValueError
|
If a layout cannot fit its children into its space. |
OSError
|
If the file cannot be read or written. |
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. |