diff options
| author | Pablo MartÃnez <58857054+elpekenin@users.noreply.github.com> | 2024-11-21 07:16:46 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-21 17:16:46 +1100 |
| commit | 88afd53b1fb4183195ac5ee9d1d1c9506de3814e (patch) | |
| tree | af3c29726f93ddf6ef69bbd0125c1e2984ead405 /lib | |
| parent | 9c865839819bf9ec3ed573354a53bcbe905080f7 (diff) | |
[CLI] Refactor painter arguments to table instead of commandline (#24456)
Co-authored-by: Nick Brassel <nick@tzarc.org>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/python/qmk/cli/painter/convert_graphics.py | 4 | ||||
| -rw-r--r-- | lib/python/qmk/cli/painter/make_font.py | 4 | ||||
| -rw-r--r-- | 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 553c26aa5d..f74d655fd5 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): | |||
| 60 | return | 60 | return |
| 61 | 61 | ||
| 62 | # Work out the text substitutions for rendering the output data | 62 | # Work out the text substitutions for rendering the output data |
| 63 | args_str = " ".join((f"--{arg} {getattr(cli.args, arg.replace('-', '_'))}" for arg in ["input", "output", "format", "no-rle", "no-deltas"])) | 63 | subs = generate_subs(cli, out_bytes, image_metadata=metadata, command_name="painter_convert_graphics") |
| 64 | command = f"qmk painter-convert-graphics {args_str}" | ||
| 65 | subs = generate_subs(cli, out_bytes, image_metadata=metadata, command=command) | ||
| 66 | 64 | ||
| 67 | # Render and write the header file | 65 | # Render and write the header file |
| 68 | header_text = render_header(subs) | 66 | 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 19db844931..3e18fd74a5 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): | |||
| 61 | return | 61 | return |
| 62 | 62 | ||
| 63 | # Work out the text substitutions for rendering the output data | 63 | # Work out the text substitutions for rendering the output data |
| 64 | args_str = " ".join((f"--{arg} {getattr(cli.args, arg.replace('-', '_'))}" for arg in ["input", "output", "no-ascii", "unicode-glyphs", "format", "no-rle"])) | ||
| 65 | command = f"qmk painter-convert-font-image {args_str}" | ||
| 66 | metadata = {"glyphs": _generate_font_glyphs_list(not cli.args.no_ascii, cli.args.unicode_glyphs)} | 64 | metadata = {"glyphs": _generate_font_glyphs_list(not cli.args.no_ascii, cli.args.unicode_glyphs)} |
| 67 | subs = generate_subs(cli, out_bytes, font_metadata=metadata, command=command) | 65 | subs = generate_subs(cli, out_bytes, font_metadata=metadata, command_name="painter_convert_font_image") |
| 68 | 66 | ||
| 69 | # Render and write the header file | 67 | # Render and write the header file |
| 70 | header_text = render_header(subs) | 68 | header_text = render_header(subs) |
diff --git a/lib/python/qmk/painter.py b/lib/python/qmk/painter.py index 512a486ce8..ed0372c163 100644 --- a/lib/python/qmk/painter.py +++ b/lib/python/qmk/painter.py | |||
| @@ -3,6 +3,7 @@ | |||
| 3 | import datetime | 3 | import datetime |
| 4 | import math | 4 | import math |
| 5 | import re | 5 | import re |
| 6 | from pathlib import Path | ||
| 6 | from string import Template | 7 | from string import Template |
| 7 | from PIL import Image, ImageOps | 8 | from PIL import Image, ImageOps |
| 8 | 9 | ||
| @@ -137,10 +138,31 @@ def _render_image_metadata(metadata): | |||
| 137 | return "\n".join(lines) | 138 | return "\n".join(lines) |
| 138 | 139 | ||
| 139 | 140 | ||
| 140 | def generate_subs(cli, out_bytes, *, font_metadata=None, image_metadata=None, command): | 141 | def command_args_str(cli, command_name): |
| 142 | """Given a command name, introspect milc to get the arguments passed in.""" | ||
| 143 | |||
| 144 | args = {} | ||
| 145 | max_length = 0 | ||
| 146 | for arg_name, was_passed in cli.args_passed[command_name].items(): | ||
| 147 | max_length = max(max_length, len(arg_name)) | ||
| 148 | |||
| 149 | val = getattr(cli.args, arg_name.replace("-", "_")) | ||
| 150 | |||
| 151 | # do not leak full paths, keep just file name | ||
| 152 | if isinstance(val, Path): | ||
| 153 | val = val.name | ||
| 154 | |||
| 155 | args[arg_name] = val | ||
| 156 | |||
| 157 | return "\n".join(f"// {arg_name.ljust(max_length)} | {val}" for arg_name, val in args.items()) | ||
| 158 | |||
| 159 | |||
| 160 | def generate_subs(cli, out_bytes, *, font_metadata=None, image_metadata=None, command_name): | ||
| 141 | if font_metadata is not None and image_metadata is not None: | 161 | if font_metadata is not None and image_metadata is not None: |
| 142 | raise ValueError("Cant generate subs for font and image at the same time") | 162 | raise ValueError("Cant generate subs for font and image at the same time") |
| 143 | 163 | ||
| 164 | args = command_args_str(cli, command_name) | ||
| 165 | |||
| 144 | subs = { | 166 | subs = { |
| 145 | "year": datetime.date.today().strftime("%Y"), | 167 | "year": datetime.date.today().strftime("%Y"), |
| 146 | "input_file": cli.args.input.name, | 168 | "input_file": cli.args.input.name, |
| @@ -148,7 +170,8 @@ def generate_subs(cli, out_bytes, *, font_metadata=None, image_metadata=None, co | |||
| 148 | "byte_count": len(out_bytes), | 170 | "byte_count": len(out_bytes), |
| 149 | "bytes_lines": render_bytes(out_bytes), | 171 | "bytes_lines": render_bytes(out_bytes), |
| 150 | "format": cli.args.format, | 172 | "format": cli.args.format, |
| 151 | "generator_command": command, | 173 | "generator_command": command_name.replace("_", "-"), |
| 174 | "command_args": args, | ||
| 152 | } | 175 | } |
| 153 | 176 | ||
| 154 | if font_metadata is not None: | 177 | if font_metadata is not None: |
| @@ -167,7 +190,7 @@ def generate_subs(cli, out_bytes, *, font_metadata=None, image_metadata=None, co | |||
| 167 | subs.update({ | 190 | subs.update({ |
| 168 | "generated_type": "image", | 191 | "generated_type": "image", |
| 169 | "var_prefix": "gfx", | 192 | "var_prefix": "gfx", |
| 170 | "generator_command": command, | 193 | "generator_command": command_name, |
| 171 | "metadata": _render_image_metadata(image_metadata), | 194 | "metadata": _render_image_metadata(image_metadata), |
| 172 | }) | 195 | }) |
| 173 | 196 | ||
| @@ -183,7 +206,8 @@ license_template = """\ | |||
| 183 | // Copyright ${year} QMK -- generated source code only, ${generated_type} retains original copyright | 206 | // Copyright ${year} QMK -- generated source code only, ${generated_type} retains original copyright |
| 184 | // SPDX-License-Identifier: GPL-2.0-or-later | 207 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 185 | 208 | ||
| 186 | // This file was auto-generated by `${generator_command}` | 209 | // This file was auto-generated by `${generator_command}` with arguments: |
| 210 | ${command_args} | ||
| 187 | """ | 211 | """ |
| 188 | 212 | ||
| 189 | 213 | ||
