1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-06-26 01:42:34 +00:00

tools: coin_gen can render into a given filename

before, it could only render from `foo.bar.mako` to `foo.bar`
This commit is contained in:
matejcik 2018-08-07 13:00:30 +02:00
parent 11a386fed4
commit 25a706f042

View File

@ -65,23 +65,26 @@ def make_support_filter(support_info):
for coin in coins: for coin in coins:
if support_info[coin.key].get(device): if support_info[coin.key].get(device):
yield coin yield coin
return supported_on return supported_on
MAKO_FILTERS = {"c_str": c_str_filter, "ascii": ascii_filter} MAKO_FILTERS = {"c_str": c_str_filter, "ascii": ascii_filter}
def render_file(filename, coins, support_info): def render_file(src, dst, coins, support_info):
"""Opens `filename.j2`, renders the template and stores the result in `filename`.""" """Renders `src` template into `dst`.
template = mako.template.Template(filename=filename + ".mako")
`src` is a filename, `dst` is an open file object.
"""
template = mako.template.Template(filename=src)
result = template.render( result = template.render(
support_info=support_info, support_info=support_info,
supported_on=make_support_filter(support_info), supported_on=make_support_filter(support_info),
**coins, **coins,
**MAKO_FILTERS **MAKO_FILTERS
) )
with open(filename, "w") as f: dst.write(result)
f.write(result)
# ====== validation functions ====== # ====== validation functions ======
@ -353,35 +356,29 @@ def coindefs(outfile):
@cli.command() @cli.command()
@click.argument("paths", metavar="[path]...", nargs=-1) @click.argument("paths", metavar="[path]...", nargs=-1)
@click.option("-o", "--outfile", type=click.File("w"), help="Alternate output file")
@click.option("-v", "--verbose", is_flag=True, help="Print rendered file names")
@click.option( @click.option(
"--erc20-support/--no-erc20-support", "-e", help="Download ERC20 support info" "--erc20-support/--no-erc20-support", "-e", help="Download ERC20 support info"
) )
def render(paths, erc20_support): def render(paths, outfile, verbose, erc20_support):
"""Generate source code from Jinja2 templates. """Generate source code from Mako templates.
For every "foo.bar.j2" filename passed, runs the template and For every "foo.bar.mako" filename passed, runs the template and
saves the result as "foo.bar". saves the result as "foo.bar". For every directory name passed,
processes all ".mako" files found in that directory.
For every directory name passed, processes all ".j2" files found If `-o` is specified, renders a single file into the specified outfile.
in that directory.
If no arguments are given, processes the current directory. If no arguments are given, processes the current directory.
""" """
if not CAN_RENDER: if not CAN_RENDER:
raise click.ClickException("Please install 'mako' and 'munch'") raise click.ClickException("Please install 'mako' and 'munch'")
if not paths: if outfile and (len(paths) != 1 or not os.path.isfile(paths[0])):
paths = ["."] raise click.ClickException("Option -o can only be used with single input file")
files = []
for path in paths:
if not os.path.exists(path):
click.echo("Path {} does not exist".format(path))
elif os.path.isdir(path):
files += glob.glob(os.path.join(path, "*.mako"))
else:
files.append(path)
# prepare defs
defs = coin_info.get_all() defs = coin_info.get_all()
if erc20_support: if erc20_support:
versions = coin_info.latest_releases() versions = coin_info.latest_releases()
@ -395,17 +392,37 @@ def render(paths, erc20_support):
for key, value in support_info.items(): for key, value in support_info.items():
support_info[key] = Munch(value) support_info[key] = Munch(value)
def do_render(src, dst):
if verbose:
click.echo("Rendering {} => {}".format(src, dst))
render_file(src, dst, defs, support_info)
# single in-out case
if outfile:
do_render(paths[0], outfile)
return
# find files in directories
if not paths:
paths = ["."]
files = []
for path in paths:
if not os.path.exists(path):
click.echo("Path {} does not exist".format(path))
elif os.path.isdir(path):
files += glob.glob(os.path.join(path, "*.mako"))
else:
files.append(path)
# render each file
for file in files: for file in files:
if not file.endswith(".mako"): if not file.endswith(".mako"):
click.echo("File {} does not end with .mako".format(file)) click.echo("File {} does not end with .mako".format(file))
else: else:
target = file[: -len(".mako")] target = file[: -len(".mako")]
click.echo("Rendering {} => {}".format(file, target)) with open(target, "w") as dst:
try: do_render(file, dst)
render_file(target, defs, support_info)
except Exception as e:
click.echo("Error occured: {}".format(e))
raise
if __name__ == "__main__": if __name__ == "__main__":