Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ lint:
flatpak run --user --command=/usr/lib/sdk/node18/bin/node --filesystem=host:ro org.gnome.Sdk//45 node_modules/.bin/eslint --max-warnings=0 src
# rustfmt
flatpak run --user --command=/usr/lib/sdk/rust-stable/bin/rustfmt --filesystem=host:ro org.gnome.Sdk//45 --check --edition 2021 src/**/*.rs
# black
./black.sh --check src/**/*.py
# gettext
find po/ -type f -name "*po" -print0 | xargs -0 -n1 msgfmt -o /dev/null --check
# Flatpak manifests
Expand Down
20 changes: 20 additions & 0 deletions black.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for local execution. See package.json.

# This is a small wrapper around black - the Python code formatter.
# The script will setup and activate a virtual env in .venv if black is not
# found in PATH.
#
# Still requires Python 3.
set -e

if ! which black &>/dev/null; then
if [ ! -d ".venv" ]; then
echo "Note: black was not found on your system. A virtualenv is created in .venv."
python3 -m venv .venv
fi
source .venv/bin/activate
if ! which black &>/dev/null; then
pip3 install black
fi
fi

exec black "$@"
Copy link
Contributor

@sonnyp sonnyp Oct 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we would use Flatpak

Could black be part of a Python sdk at some point?

An other option is to build the Workbench Flatpak once before running the pre-commits
And using the tooling from within the Workbench runtime

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would probably be useful to have in the SDK. I feel like pre-building the Workbench Flatpak is a bit convoluted and error prone.

27 changes: 27 additions & 0 deletions build-aux/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Build Utilities

This directory contains the Flatpak Manifest and other build utilities.

## Workbench Flatpak Manifests

### Python Black Dependency

`modules/python-black.json` contains the Flatpak modules to install [https://github.com/psf/black](black), the
uncompromising Python code formatter.

This file is partially auto-generated. Here is how to generate it:

1. Obtain an up-to-date copy of [flatpak-builder-tools](https://github.com/flatpak/flatpak-builder-tools).
2. Make sure you have a Python virtualenv activated with the dependencies for the `pip` generator from the repo above.
3. Run `python3 <...>/flatpak-builder-tools/pip/flatpak-pip-generator black -o modules/python-black --build-isolation`

You will notice that Workbench will now not build. This is due to these issues:

- https://github.com/flatpak/flatpak-builder-tools/issues/380
- https://github.com/pypa/pip/issues/7863

This means that the generated JSON file now needs its build dependencies manually added. Check the build dependencies
of black and their dependencies and add them to the JSON as well. Brute-forcing the build to obtain missing packages
may help as does referencing old commits of the file. You can also use the generator command to generate dependencies
for them and then merge it into the file by hand, but note that you will also need to manually collect their build
dependencies too.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

excellent doc 👌

thank you

104 changes: 104 additions & 0 deletions build-aux/modules/python-black.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"name": "python3-black",
"buildsystem": "simple",
"build-commands": [
"pip3 install --verbose --exists-action=i --no-index --find-links=\"file://${PWD}\" --prefix=${FLATPAK_DEST} \"black\""
],
"sources": [
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/36/bf/a462f36723824c60dc3db10528c95656755964279a6a5c287b4f9fd0fa84/black-23.10.1.tar.gz",
"sha256": "1f8ce316753428ff68749c65a5f7844631aa18c8679dfd3ca9dc1a289979c258"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/00/2e/d53fa4befbf2cfa713304affc7ca780ce4fc1fd8710527771b58311a3229/click-8.1.7-py3-none-any.whl",
"sha256": "ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/2a/e2/5d3f6ada4297caebe1a2add3b126fe800c96f56dbe5d1988a2cbe0b267aa/mypy_extensions-1.0.0-py3-none-any.whl",
"sha256": "4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/ec/1a/610693ac4ee14fcdf2d9bf3c493370e4f2ef7ae2e19217d7a237ff42367d/packaging-23.2-py3-none-any.whl",
"sha256": "8c491190033a9af7e1d931d0b5dacc2ef47509b34dd0de67ed209b5203fc88c7"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/b4/2a/9b1be29146139ef459188f5e420a66e835dda921208db600b7037093891f/pathspec-0.11.2-py3-none-any.whl",
"sha256": "1d6ed233af05e679efb96b1851550ea95bbb64b7c490b0f5aa52996c11e92a20"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/56/29/3ec311dc18804409ecf0d2b09caa976f3ae6215559306b5b530004e11156/platformdirs-3.11.0-py3-none-any.whl",
"sha256": "e9d171d00af68be50e9202731309c4e658fd8bc76f55c11c7dd760d023bda68e"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/6b/be/0f2f4a5e8adc114a02b63d92bf8edbfa24db6fc602fca83c885af2479e0e/editables-0.5-py3-none-any.whl",
"sha256": "61e5ffa82629e0d8bfe09bc44a07db3c1ab8ed1ce78a6980732870f19b5e7d4c"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/76/56/8ccca673e2c896931722f876bf040c0b6a7d8c1a128be60516a8a55bb27a/hatchling-1.18.0-py3-none-any.whl",
"sha256": "b66dc254931ec42aa68b5febd1d342c58142cc5267b7ff3b12ba3fa5b4900c93"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/ec/1a/610693ac4ee14fcdf2d9bf3c493370e4f2ef7ae2e19217d7a237ff42367d/packaging-23.2-py3-none-any.whl",
"sha256": "8c491190033a9af7e1d931d0b5dacc2ef47509b34dd0de67ed209b5203fc88c7"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/b4/2a/9b1be29146139ef459188f5e420a66e835dda921208db600b7037093891f/pathspec-0.11.2-py3-none-any.whl",
"sha256": "1d6ed233af05e679efb96b1851550ea95bbb64b7c490b0f5aa52996c11e92a20"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/05/b8/42ed91898d4784546c5f06c60506400548db3f7a4b3fb441cba4e5c17952/pluggy-1.3.0-py3-none-any.whl",
"sha256": "d89c696a773f8bd377d18e5ecda92b7a3793cbe66c87060a6fb58c7b6e1061f7"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/ec/40/05cb2725ca7e6c844c66af626c5749efd254ec4506f17a1d01ba79ae9da6/trove_classifiers-2023.10.18-py3-none-any.whl",
"sha256": "20a3da8e3cb65587cc9f5d5b837bf74edeb480bba9bd8cd4f03ab056d6b06c4c"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/00/b1/90cc7881c2e870333eeafa6afb7b27de53418aad1ba5409ad331c96608a1/hatch_vcs-0.3.0-py3-none-any.whl",
"sha256": "60ce59a3fa4664057e4a858b6a96ab0b9dec21bf8f562f836139315bb361be8c"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/0e/a3/b9a8b0adfe672bf0df5901707aa929d30a97ee390ba651910186776746d2/setuptools_scm-8.0.4-py3-none-any.whl",
"sha256": "b47844cd2a84b83b3187a5782c71128c28b4c94cad8bfb871da2784a5cb54c4f"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/24/21/7d397a4b7934ff4028987914ac1044d3b7d52712f30e2ac7a2ae5bc86dd0/typing_extensions-4.8.0-py3-none-any.whl",
"sha256": "8f92fc8806f9a6b641eaa5318da32b44d401efaac0f6678c9bc448ba3605faa0"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/6b/74/74a15619861065fa75f6170c649cd2fd5467726bb407ec2017d4a3f92d28/hatch_fancy_pypi_readme-23.1.0-py3-none-any.whl",
"sha256": "9a2c0e5d527611701858083bfb355876f26e04a075342b7d4da725ba5a6dd6ea"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/ef/cc/93f7213b2ab5ed383f98ce8020e632ef256b406b8569606c3f160ed8e1c9/setuptools-68.2.2.tar.gz",
"sha256": "4ac1475276d2f1c48684874089fefcd83bd7162ddaafb81fac866ba0db282a87"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/a4/99/78c4f3bd50619d772168bec6a0f34379b02c19c9cced0ed833ecd021fd0d/wheel-0.41.2.tar.gz",
"sha256": "0c5ac5ff2afb79ac23ab82bab027a0be7b5dbcf2e54dc50efe4bf507de1f7985"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/c4/e6/c1ac50fe3eebb38a155155711e6e864e254ce4b6e17fe2429b4c4d5b9e80/flit_core-3.9.0.tar.gz",
"sha256": "72ad266176c4a3fcfab5f2930d76896059851240570ce9a98733b658cb786eba"
}
]
}
1 change: 1 addition & 0 deletions build-aux/re.sonny.Workbench.Devel.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"modules/blueprint-compiler.json",
"modules/biome.json",
"modules/icon-development-kit.json",
"modules/python-black.json",
{
"name": "Workbench",
"buildsystem": "meson",
Expand Down
1 change: 1 addition & 0 deletions build-aux/re.sonny.Workbench.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"modules/blueprint-compiler.json",
"modules/biome.json",
"modules/icon-development-kit.json",
"modules/python-black.json",
{
"name": "Workbench",
"buildsystem": "meson",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"lint-staged": {
"*.{css,json,md,yaml,yml}": "prettier --write",
"*.{js,cjs,mjs}": "eslint --fix",
"*.py": "./black.sh",
"*.rs": "flatpak run --user --command=/usr/lib/sdk/rust-stable/bin/rustfmt --filesystem=host org.gnome.Sdk//45 --edition 2021"
}
}
11 changes: 2 additions & 9 deletions src/Library/demos/Welcome/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

Copy link
Contributor Author

@theCapypara theCapypara Oct 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the pre-commit hook doing its magic :)


def greet(_widget):
dialog = Adw.MessageDialog(
body="Hello World!",
transient_for=workbench.window
)
dialog = Adw.MessageDialog(body="Hello World!", transient_for=workbench.window)

dialog.add_response("ok", "Ok")
dialog.connect("response", dialog_response)
Expand All @@ -24,11 +21,7 @@ def dialog_response(dialog: Adw.MessageDialog, response: str):

subtitle_box: Gtk.Box = workbench.builder.get_object("subtitle")

button = Gtk.Button(
label="Press me",
margin_top=6,
css_classes=["suggested-action"]
)
button = Gtk.Button(label="Press me", margin_top=6, css_classes=["suggested-action"])
button.connect("clicked", greet)

subtitle_box.append(button)
Expand Down
2 changes: 1 addition & 1 deletion src/langs/python/python-previewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
gi.require_version("Gtk", "4.0")
gi.require_version("Adw", "1")
gi.require_version("Graphene", "1.0")
gi.require_version('Gsk', '4.0')
gi.require_version("Gsk", "4.0")

from gi.repository import GLib, Gdk, Gtk, Adw, Graphene, Gio, Gsk
from gi.repository.Gio import DBusConnection, DBusConnectionFlags
Expand Down
23 changes: 23 additions & 0 deletions src/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,25 @@ export default function Window({ application, session }) {
return stdout;
}

function formatPythonCode(text) {
const blackLauncher = Gio.SubprocessLauncher.new(
Gio.SubprocessFlags.STDIN_PIPE |
Gio.SubprocessFlags.STDOUT_PIPE |
Gio.SubprocessFlags.STDERR_PIPE,
);

const blackProcess = blackLauncher.spawnv(["black", "--quiet", "-"]);

const [success, stdout, stderr] = blackProcess.communicate_utf8(text, null);

if (!success || stderr !== "") {
console.error(`Error running black: ${stderr}`);
return text;
}

return stdout;
}

async function formatCode() {
if (panel_code.panel.visible) {
if (panel_code.language === "JavaScript") {
Expand All @@ -255,6 +274,10 @@ export default function Window({ application, session }) {
await format(langs.rust.document.code_view, (text) => {
return formatRustCode(text);
});
} else if (panel_code.language === "Python") {
await format(langs.python.document.code_view, (text) => {
return formatPythonCode(text);
});
}
}

Expand Down