Document theme_parser.py

This commit is contained in:
miruka 2019-12-18 09:50:53 -04:00
parent 845fa40ed8
commit d89c83af5b

View File

@ -1,3 +1,9 @@
"""QPL (Qt Property Language) theme files to QML converter.
QPL is a custom configuration format currently used for theme files.
This is a big hack and will be replaced in the future by a standard language.
"""
import re import re
from typing import Generator from typing import Generator
@ -6,6 +12,8 @@ PROPERTY_TYPES = {"bool", "double", "int", "list", "real", "string", "url",
def _add_property(line: str) -> str: def _add_property(line: str) -> str:
"""Return a QML property declaration line from a QPL property line."""
if re.match(r"^\s*[a-zA-Z\d_]+\s*:$", line): if re.match(r"^\s*[a-zA-Z\d_]+\s*:$", line):
return re.sub(r"^(\s*)(\S*\s*):$", return re.sub(r"^(\s*)(\S*\s*):$",
r"\1readonly property QtObject \2: QtObject", r"\1readonly property QtObject \2: QtObject",
@ -19,6 +27,8 @@ def _add_property(line: str) -> str:
def _process_lines(content: str) -> Generator[str, None, None]: def _process_lines(content: str) -> Generator[str, None, None]:
"""Yield lines of real QML from lines of QPL."""
skip = False skip = False
indent = " " * 4 indent = " " * 4
current_indent = 0 current_indent = 0
@ -55,6 +65,8 @@ def _process_lines(content: str) -> Generator[str, None, None]:
def convert_to_qml(theme_content: str) -> str: def convert_to_qml(theme_content: str) -> str:
"""Return valid QML code with imports from QPL content."""
lines = [ lines = [
"import QtQuick 2.12", "import QtQuick 2.12",
'import "../Base"', 'import "../Base"',
@ -66,4 +78,5 @@ def convert_to_qml(theme_content: str) -> str:
] ]
lines += [f" {line}" for line in _process_lines(theme_content)] lines += [f" {line}" for line in _process_lines(theme_content)]
lines += ["}"] lines += ["}"]
return "\n".join(lines) return "\n".join(lines)