49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
"""Small XML helpers shared by the workspace writer."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from xml.sax.saxutils import escape, quoteattr
|
|
|
|
|
|
def attr(value: object) -> str:
|
|
return quoteattr(str(value))
|
|
|
|
|
|
def text(value: object) -> str:
|
|
return escape(str(value))
|
|
|
|
|
|
def qlc_color(r: int, g: int, b: int) -> str:
|
|
"""QLC+ widget colour as a decimal ARGB word."""
|
|
return str((0xFF << 24) | ((int(r) & 255) << 16) | ((int(g) & 255) << 8) | (int(b) & 255))
|
|
|
|
|
|
def colour(value: object, default: str = "Default") -> str:
|
|
if value is None:
|
|
return default
|
|
if isinstance(value, str):
|
|
return value
|
|
if isinstance(value, int) and not isinstance(value, bool):
|
|
return str(value)
|
|
if isinstance(value, (list, tuple)) and len(value) == 3:
|
|
return qlc_color(int(value[0]), int(value[1]), int(value[2]))
|
|
raise TypeError(f"colour must be 'Default', an ARGB decimal, or [r, g, b], not {value!r}")
|
|
|
|
|
|
def appearance(
|
|
fg: object = "Default",
|
|
bg: object = "Default",
|
|
frame: str = "Sunken",
|
|
indent: str = " ",
|
|
) -> str:
|
|
inner = indent + " "
|
|
return (
|
|
f"{indent}<Appearance>\n"
|
|
f"{inner}<FrameStyle>{text(frame)}</FrameStyle>\n"
|
|
f"{inner}<ForegroundColor>{text(colour(fg))}</ForegroundColor>\n"
|
|
f"{inner}<BackgroundColor>{text(colour(bg))}</BackgroundColor>\n"
|
|
f"{inner}<BackgroundImage>None</BackgroundImage>\n"
|
|
f"{inner}<Font>Default</Font>\n"
|
|
f"{indent}</Appearance>"
|
|
)
|