From 88afd53b1fb4183195ac5ee9d1d1c9506de3814e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Mart=C3=ADnez?= <58857054+elpekenin@users.noreply.github.com> Date: Thu, 21 Nov 2024 07:16:46 +0100 Subject: [PATCH] [CLI] Refactor painter arguments to table instead of commandline (#24456) Co-authored-by: Nick Brassel --- .../qmk/cli/painter/convert_graphics.py | 4 +-- lib/python/qmk/cli/painter/make_font.py | 4 +-- lib/python/qmk/painter.py | 32 ++++++++++++++++--- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lib/python/qmk/cli/painter/convert_graphics.py b/lib/python/qmk/cli/painter/convert_graphics.py index 553c26aa5d5..f74d655fd57 100644 --- a/lib/python/qmk/cli/painter/convert_graphics.py +++ b/lib/python/qmk/cli/painter/convert_graphics.py @@ -60,9 +60,7 @@ def painter_convert_graphics(cli): return # Work out the text substitutions for rendering the output data - args_str = " ".join((f"--{arg} {getattr(cli.args, arg.replace('-', '_'))}" for arg in ["input", "output", "format", "no-rle", "no-deltas"])) - command = f"qmk painter-convert-graphics {args_str}" - subs = generate_subs(cli, out_bytes, image_metadata=metadata, command=command) + subs = generate_subs(cli, out_bytes, image_metadata=metadata, command_name="painter_convert_graphics") # Render and write the header file header_text = render_header(subs) diff --git a/lib/python/qmk/cli/painter/make_font.py b/lib/python/qmk/cli/painter/make_font.py index 19db8449316..3e18fd74a56 100644 --- a/lib/python/qmk/cli/painter/make_font.py +++ b/lib/python/qmk/cli/painter/make_font.py @@ -61,10 +61,8 @@ def painter_convert_font_image(cli): return # Work out the text substitutions for rendering the output data - args_str = " ".join((f"--{arg} {getattr(cli.args, arg.replace('-', '_'))}" for arg in ["input", "output", "no-ascii", "unicode-glyphs", "format", "no-rle"])) - command = f"qmk painter-convert-font-image {args_str}" metadata = {"glyphs": _generate_font_glyphs_list(not cli.args.no_ascii, cli.args.unicode_glyphs)} - subs = generate_subs(cli, out_bytes, font_metadata=metadata, command=command) + subs = generate_subs(cli, out_bytes, font_metadata=metadata, command_name="painter_convert_font_image") # Render and write the header file header_text = render_header(subs) diff --git a/lib/python/qmk/painter.py b/lib/python/qmk/painter.py index 512a486ce87..ed0372c163e 100644 --- a/lib/python/qmk/painter.py +++ b/lib/python/qmk/painter.py @@ -3,6 +3,7 @@ import datetime import math import re +from pathlib import Path from string import Template from PIL import Image, ImageOps @@ -137,10 +138,31 @@ def _render_image_metadata(metadata): return "\n".join(lines) -def generate_subs(cli, out_bytes, *, font_metadata=None, image_metadata=None, command): +def command_args_str(cli, command_name): + """Given a command name, introspect milc to get the arguments passed in.""" + + args = {} + max_length = 0 + for arg_name, was_passed in cli.args_passed[command_name].items(): + max_length = max(max_length, len(arg_name)) + + val = getattr(cli.args, arg_name.replace("-", "_")) + + # do not leak full paths, keep just file name + if isinstance(val, Path): + val = val.name + + args[arg_name] = val + + return "\n".join(f"// {arg_name.ljust(max_length)} | {val}" for arg_name, val in args.items()) + + +def generate_subs(cli, out_bytes, *, font_metadata=None, image_metadata=None, command_name): if font_metadata is not None and image_metadata is not None: raise ValueError("Cant generate subs for font and image at the same time") + args = command_args_str(cli, command_name) + subs = { "year": datetime.date.today().strftime("%Y"), "input_file": cli.args.input.name, @@ -148,7 +170,8 @@ def generate_subs(cli, out_bytes, *, font_metadata=None, image_metadata=None, co "byte_count": len(out_bytes), "bytes_lines": render_bytes(out_bytes), "format": cli.args.format, - "generator_command": command, + "generator_command": command_name.replace("_", "-"), + "command_args": args, } if font_metadata is not None: @@ -167,7 +190,7 @@ def generate_subs(cli, out_bytes, *, font_metadata=None, image_metadata=None, co subs.update({ "generated_type": "image", "var_prefix": "gfx", - "generator_command": command, + "generator_command": command_name, "metadata": _render_image_metadata(image_metadata), }) @@ -183,7 +206,8 @@ license_template = """\ // Copyright ${year} QMK -- generated source code only, ${generated_type} retains original copyright // SPDX-License-Identifier: GPL-2.0-or-later -// This file was auto-generated by `${generator_command}` +// This file was auto-generated by `${generator_command}` with arguments: +${command_args} """