Python: Add wrapper for Slint Compiler

This permits invoking the Slint compiler conveniently via `uvx
slint-compiler` or `pipx slint-compiler`.

cc #4136
This commit is contained in:
Simon Hausmann 2025-07-10 11:02:37 +02:00 committed by Simon Hausmann
parent 82f34d9e57
commit 75052fb902
7 changed files with 188 additions and 1 deletions

1
api/python/compiler/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*.egg-info

View file

@ -0,0 +1,15 @@
<!-- Copyright © SixtyFPS GmbH <info@slint.dev> ; SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 -->
# Python Slint Compiler
This package is a wrapper around [Slint's](https://slint.dev) compiler for Python, to generate a typed `.py` file from a `.slint` file, for use with [Slint for Python](https://pypi.org/project/slint/).
When run, the slint compiler binary is downloaded, cached, and run.
By default, the Slint compiler matching the version of this package is downloaded. To select a specific version, set the `SLINT_COMPILER_VERSION` environment variable. Set it to `nightly` to select the latest nightly release.
## Example
```bash
uxv run slint-compiler -f python -o app_window.py app-window.slint
```

View file

@ -0,0 +1,13 @@
# Copyright © SixtyFPS GmbH <info@slint.dev>
# SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
[project]
name = "slint-compiler"
version = "1.12.1b1"
description = "Python wrapper around the Slint compiler for Python"
readme = "README.md"
requires-python = ">=3.10"
dependencies = ["cached-path>=1.7.3"]
[project.scripts]
slint-compiler = "slint_compiler:main"

View file

@ -0,0 +1,67 @@
# Copyright © SixtyFPS GmbH <info@slint.dev>
# SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
import cached_path
import platform
import sys
import subprocess
import re
import os
from importlib.metadata import version, PackageNotFoundError
def main():
try:
package_version = version("slint-compiler")
# Strip alpha/beta from for example "1.13.0b1"
version_regex = re.search("([0-9]*)\\.([0-9]*)\\.([0-9]*).*", package_version)
assert version_regex is not None
major = version_regex.group(1)
minor = version_regex.group(2)
patch = version_regex.group(3)
github_release = f"v{major}.{minor}.{patch}"
except PackageNotFoundError:
github_release = "nightly"
# Permit override by environment variable
github_release = os.environ.get("SLINT_COMPILER_VERSION") or github_release
operating_system = {
"darwin": "Darwin",
"linux": "Linux",
"win32": "Windows",
"msys": "Windows",
}[sys.platform]
arch = {
"aarch64": "aarch64",
"amd64": "x86_64",
"arm64": "aarch64",
"x86_64": "x86_64",
}[platform.machine().lower()]
exe_suffix = ""
if operating_system == "Windows":
arch = {"aarch64": "ARM64", "x86_64": "AMD64"}[arch]
exe_suffix = ".exe"
elif operating_system == "Linux":
pass
elif operating_system == "Darwin":
arch = {"aarch64": "arm64", "x86_64": "x86_64"}[arch]
else:
raise Exception(f"Unsupported operarating system: {operating_system}")
platform_suffix = f"{operating_system}-{arch}"
prebuilt_archive_filename = f"slint-compiler-{platform_suffix}.tar.gz"
download_url = f"https://github.com/slint-ui/slint/releases/download/{github_release}/{prebuilt_archive_filename}"
url_and_path_within_archive = f"{download_url}!slint-compiler{exe_suffix}"
local_path = cached_path.cached_path(
url_and_path_within_archive, extract_archive=True
)
args = [local_path]
args.extend(sys.argv[1:])
subprocess.run(args)
if __name__ == "__main__":
main()