示例#1
0
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)
示例#2
0
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."
示例#3
0
def test_missing_value():
    with pytest.raises(NameError):
        template("Hello, <% name %>.")
示例#4
0
def test_trivial():
    assert template("Hello world") == "Hello world"
    assert template("Hello, <% name %>.", name="Clarice") == "Hello, Clarice."
示例#5
0
def test_eval():
    assert template("Hello, <% name.upper() %>.",
                    name="Clarice") == "Hello, CLARICE."
示例#6
0
def test_bare():
    assert template("Hello, $name.", name='Joe') == "Hello, $name."