George parsing utilities¶
Parsing functions used to handle data coming from TVPaint and also preparing arguments for them to be sent.
The two main functions are tv_parse_dict and tv_parse_list which handle the return values of George functions.
George can either return a list of values or a list of key/value pairs which are consecutive.
T = TypeVar('T', bound=Any)
module-attribute
¶
Value = Union[int, float, str, bool, None]
module-attribute
¶
FieldTypes: TypeAlias = MutableSequence[tuple[Union[tuple[str, str], str], Any]]
module-attribute
¶
TVP_DOUBLE_QUOTES_RE = re.compile('""(.*?)""', re.I)
module-attribute
¶
DataclassInstance
¶
Bases: typing_extensions.Protocol
Protocol that describes a Dataclass instance.
tv_handle_string(s: str) -> str
¶
String handling for George arguments. It wraps the string into quotes if it has spaces.
See an example here: https://www.tvpaint.com/doc/tvpaint-animation-11/george-commands#tv_projectnew
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
the input string |
required |
Returns:
| Type | Description |
|---|---|
str
|
the "escaped" string |
Source code in pytvpaint/george/client/parse.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
camel_to_pascal(s: str) -> str
¶
Convert a camel case string to pascal case.
Example
this_is_a_text -> ThisIsAText
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
the input string |
required |
Returns:
| Type | Description |
|---|---|
str
|
the string in pascal case |
Source code in pytvpaint/george/client/parse.py
60 61 62 63 64 65 66 67 68 69 70 71 72 | |
tv_cast_to_type(value: str, cast_type: type[T]) -> T
¶
Casts a string value to a specific Python type, supporting: - Primitives (int, float, bool, str) - Path objects - Enums (by name, value, index) - Collections (List[T], Tuple[T, ...], Tuple[A, B])
Source code in pytvpaint/george/client/parse.py
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 | |
get_dataclass_fields(datacls: DataclassInstance | type[DataclassInstance], alt_names: bool = False) -> FieldTypes
¶
Get the dataclass key/type pairs and filter those with the "parsed" metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
datacls
|
pytvpaint.george.client.parse.DataclassInstance | type[pytvpaint.george.client.parse.DataclassInstance]
|
input dataclass |
required |
alt_names
|
bool
|
get alt names, if no alt_name field name will be used |
False
|
Returns:
| Type | Description |
|---|---|
pytvpaint.george.client.parse.FieldTypes
|
the list of key/type tuple |
Source code in pytvpaint/george/client/parse.py
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 | |
tv_parse_list(text: str, with_fields: FieldTypes | type[DataclassInstance], unused_indices: list[int] | None = None) -> dict[str, Any]
¶
Parses a positional string into typed arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The raw input string. |
required |
with_fields
|
pytvpaint.george.client.parse.FieldTypes | type[pytvpaint.george.client.parse.DataclassInstance]
|
A list of tuples [('name', Type), ...]. |
required |
unused_indices
|
list[int] | None
|
Some George functions return positional arguments that are unused. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, typing.Any]
|
a dict with the provided fields and the values cast to the given types |
Source code in pytvpaint/george/client/parse.py
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 | |
tv_parse_dict(text: str, with_fields: FieldTypes | type[DataclassInstance]) -> dict[str, T]
¶
Parse a list of values as key value pairs returned from TVPaint commands.
Cast the values to a provided dataclass type or list of key/types pairs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input string (e.g. 'X 10 y 20 TAGS "a b"') |
required |
with_fields
|
pytvpaint.george.client.parse.FieldTypes | type[pytvpaint.george.client.parse.DataclassInstance]
|
A list of tuples [('name', Type), ...]. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, pytvpaint.george.client.parse.T]
|
a dict with the provided fields and the values cast to the given types |
Source code in pytvpaint/george/client/parse.py
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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | |
args_dict_to_list(args: dict[str, Any]) -> list[Any]
¶
Converts a dict of named arguments to a flat list of key/values.
It also filters pairs with None values
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
dict[str, typing.Any]
|
dict of arguments |
required |
Returns:
| Type | Description |
|---|---|
list[typing.Any]
|
key/values list |
Source code in pytvpaint/george/client/parse.py
340 341 342 343 344 345 346 347 348 349 350 351 352 | |
validate_args_list(optional_args: Sequence[Value | tuple[Value, ...]]) -> list[Any]
¶
Validates *args equivalent for tvpaint.
Some George functions only accept a list of values and not key:value pairs. If for instance, you need to set the last positional argument for a function call, you need to provide all the preceding arguments. This function, given a list of arguments or key:value pairs (as tuples) checks that they are valid/not None.
For example, for tv_camerainfo [<iWidth> <iHeight> [<field_order>]]
you can't pass [500, None, "upper"] because <iHeight> is not defined.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optional_args
|
collections.abc.Sequence[pytvpaint.george.client.parse.Value | tuple[pytvpaint.george.client.parse.Value, ...]]
|
list of values or tuple of values (args block) |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
if not all the parameters were given |
Returns:
| Type | Description |
|---|---|
list[typing.Any]
|
the list of parameters |
Source code in pytvpaint/george/client/parse.py
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 | |
normalize_windows_paths(text: str) -> str
¶
Identifies Windows-style file paths and converts backslashes to forward slashes.
Supports: 1. Drive Roots: C:\Folder 2. Relative: .\Folder or ..\Folder 3. Root Relative: \.\Folder or \..\Folder <-- NEW 4. UNC Paths: \\Server\Share\File
Source code in pytvpaint/george/client/parse.py
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 | |
unescape_everything_safely(text: str) -> str
¶
Unescapes a raw/non-formatted tvpaint string while trying to preserve Windows file paths and filenames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
raw string. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
unescaped string. |
Source code in pytvpaint/george/client/parse.py
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | |