Skip to content

Commit

Permalink
fix: handle zero values for TemplateVar
Browse files Browse the repository at this point in the history
boolean values are now also allowed as `True` or `False`

also incorrect values will no longer result in a critical error, but a CLI usage error instead

---------

PuyaOptions now contains a parsed set of key-values for template variables
  • Loading branch information
achidlow committed Sep 13, 2024
1 parent b5d4eb8 commit 6087dc2
Show file tree
Hide file tree
Showing 84 changed files with 171 additions and 163 deletions.
2 changes: 1 addition & 1 deletion examples/amm/puya.log

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/arc_28/puya.log

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/auction/puya.log

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/box_storage/puya.log

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/calculator/puya.log

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/global_state/puya.log

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/hello_world/puya.log

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/hello_world_arc4/puya.log

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/local_state/puya.log

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/merkle/puya.log

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/struct_in_box/puya.log

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/tictactoe/puya.log

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/voting/puya.log

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions src/puya/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import attrs

from puya import template
from puya.algo_constants import MAINNET_TEAL_LANGUAGE_VERSION


Expand All @@ -27,14 +26,12 @@ class PuyaOptions:
debug_level: int = 1
optimization_level: int = 1
target_avm_version: int = MAINNET_TEAL_LANGUAGE_VERSION
cli_template_definitions: list[str] = attrs.field(factory=list)
cli_template_definitions: Mapping[str, int | bytes] = attrs.field(factory=dict)
template_vars_prefix: str = "TMPL_"
# TODO: the below is probably not scalable as a set of optimisation on/off flags,
# but it'll do for now
locals_coalescing_strategy: LocalsCoalescingStrategy = LocalsCoalescingStrategy.root_operand

@cached_property
def template_variables(self) -> Mapping[str, int | bytes]:
return template.parse_template_vars(
self.cli_template_definitions, self.template_vars_prefix
)
return {self.template_vars_prefix + k: v for k, v in self.cli_template_definitions.items()}
61 changes: 0 additions & 61 deletions src/puya/template.py

This file was deleted.

36 changes: 32 additions & 4 deletions src/puyapy/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import argparse
import typing
from collections.abc import Sequence
from importlib.metadata import version
from pathlib import Path

Expand All @@ -8,6 +10,7 @@

from puyapy.compile import compile_to_teal
from puyapy.options import PuyaPyOptions
from puyapy.template import parse_template_key_value


def main() -> None:
Expand Down Expand Up @@ -113,12 +116,11 @@ def main() -> None:
"--template-var",
dest="cli_template_definitions",
metavar="VAR=VALUE",
action="append",
action=_ParseAndStoreTemplateVar,
nargs="+",
help="Define template vars for use when assembling via --output-bytecode"
" should be specified without the prefix (see --template-vars-prefix), e.g."
" -T=SOME_INT=1234"
" -T=SOME_BYTES=0x1A2B"
' -T=SOME_STR=\\"hello\\"',
' -T SOME_INT=1234 SOME_BYTES=0x1A2B SOME_BOOL=True SOME_STR=\\"hello\\"',
)
parser.add_argument(
"--template-vars-prefix",
Expand Down Expand Up @@ -149,5 +151,31 @@ def main() -> None:
compile_to_teal(options)


class _ParseAndStoreTemplateVar(argparse.Action):
@typing.override
def __call__(
self,
parser: argparse.ArgumentParser,
namespace: argparse.Namespace,
values: str | Sequence[typing.Any] | None,
option_string: str | None = None,
) -> None:
mapping: dict[str, int | bytes] = dict(getattr(namespace, self.dest, {}))
lst = []
if isinstance(values, str):
lst = [values]
elif values:
for value in values:
assert isinstance(value, str)
lst.append(value)
for kv in lst:
try:
name, value = parse_template_key_value(kv)
except Exception as ex:
parser.error(str(ex))
mapping[name] = value
setattr(namespace, self.dest, mapping)


if __name__ == "__main__":
main()
Loading

0 comments on commit 6087dc2

Please sign in to comment.