def fmnt_print(*argv, **env): ''' formant print [filename] --some-arg='Some value' ''' positional, flag_args = parse(argv) assert len(positional) <= 1, "Expect one positional argument (filename)" text = read_file(*positional) try: print(template(text, flag_args, plugins.registry, env)) except NameError as ne: import re name = re.fullmatch(r"name '(\w+)' is not defined", str(ne)) if name: ne = name.group(1) print(f'Missing parameter: {ne}', file=sys.stderr)
def test_manual_sources(): src1 = {"title": "SRC1 title", "body": "SRC1 body"} src2 = {"title": "SRC2 title", "author": "SRC2 author"} tmpl = "<% title %>, by <% author %>, says <% body %>." # Confirm that the priority order is kwargs, src1, src2... srcN with pytest.raises(NameError): template(tmpl) assert template(tmpl, src1, author="...") == "SRC1 title, by ..., says SRC1 body." assert template(tmpl, src2, body="...") == "SRC2 title, by SRC2 author, says ...." assert template(tmpl, src1, src2) == "SRC1 title, by SRC2 author, says SRC1 body." assert template( tmpl, src1, src2, body='custom body') == "SRC1 title, by SRC2 author, says custom body."
def test_missing_value(): with pytest.raises(NameError): template("Hello, <% name %>.")
def test_trivial(): assert template("Hello world") == "Hello world" assert template("Hello, <% name %>.", name="Clarice") == "Hello, Clarice."
def test_eval(): assert template("Hello, <% name.upper() %>.", name="Clarice") == "Hello, CLARICE."
def test_bare(): assert template("Hello, $name.", name='Joe') == "Hello, $name."