introspection of milc, strip full paths

This commit is contained in:
elpekenin 2024-09-10 16:34:46 +02:00
parent 2c7bf34d09
commit f05a5c3144
3 changed files with 28 additions and 7 deletions

View File

@ -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)

View File

@ -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="make_font")
# Render and write the header file
header_text = render_header(subs)

View File

@ -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,34 @@ def _render_image_metadata(metadata):
return "\n".join(lines)
def generate_subs(cli, out_bytes, *, font_metadata=None, image_metadata=None, command):
def generate_command_str(cli, command_name):
"""Given a command name, introspect milc to get the arguments passed in."""
args = []
for arg_name, was_passed in cli.args_passed[command_name].items():
# we might ignore a value if it was not passed in
# but not doing so (for now?)
val = getattr(cli.args, arg_name.replace("-", "_"))
# do not leak full paths, keep just file name
if isinstance(val, Path):
val = val.name
args.append(f"--{arg_name} {val}")
command_name = command_name.replace("_", "-")
args_str = " ".join(args)
return f"qmk {command_name} {args_str}"
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")
command = generate_command_str(cli, command_name)
subs = {
"year": datetime.date.today().strftime("%Y"),
"input_file": cli.args.input.name,