예제 #1
0
    def test_condition(self):
        test_stream = io.StringIO(''.join([
            "First line.",
            "{{header}}",
            "{{#loop somearray item}}",
            "{{#if item eq term}}"
            "This is a {{item}}.",
            "{{/if}}",
            "{{/loop}}",
            "{{footer}}",
            "Last line."]))

        variables = {
            "header": "Hello!",
            "term": "banana",
            "somearray": ["apple", "banana", "citrus"],
            "footer": "That's it!"
        }

        engine = TemplatingEngine(variables)

        output = io.StringIO()
        engine.process(test_stream, output)

        expected = ''.join(["First line.",
                            "Hello!",
                            "This is a banana.",
                            "That's it!",
                            "Last line."])

        actual = output.getvalue()

        self.assertEqual(expected, actual)
예제 #2
0
    def test_template_symbols_only(self):
        test_stream = io.StringIO("{{#loop list item}}{{item}}{{/loop}}")

        variables = {"list": ["1", "2", "3"]}
        engine = TemplatingEngine(variables)

        output = io.StringIO()
        engine.process(test_stream, output)

        expected = "123"
        actual = output.getvalue()

        self.assertEqual(expected, actual)
예제 #3
0
    def test_empty(self):
        test_stream = io.StringIO("")

        variables = {}
        engine = TemplatingEngine(variables)

        output = io.StringIO()
        engine.process(test_stream, output)

        expected = ""
        actual = output.getvalue()

        self.assertEqual(expected, actual)
예제 #4
0
    def test_single_variable(self):
        test_stream = io.StringIO("a {{arg}} v")

        variables = {"arg": "test"}
        engine = TemplatingEngine(variables)

        output = io.StringIO()
        engine.process(test_stream, output)

        expected = "a test v"
        actual = output.getvalue()

        self.assertEqual(expected, actual)
예제 #5
0
    def test_loop_invalid_template_no_arguments(self):
        test_stream = io.StringIO("text with {{#loop var}} .")

        variables = {
            "var": ["variable", "variable1"]
        }

        engine = TemplatingEngine(variables)

        output = io.StringIO()
        engine.process(test_stream, output)

        expected = "text with {{#loop var}} ."

        actual = output.getvalue()

        self.assertEqual(expected, actual)
예제 #6
0
    def test_invalid_template(self):
        test_stream = io.StringIO("text with {{that is not template")

        variables = {
            "that": "variable"
        }

        engine = TemplatingEngine(variables)

        output = io.StringIO()
        engine.process(test_stream, output)

        expected = "text with {{that is not template"

        actual = output.getvalue()

        self.assertEqual(expected, actual)
예제 #7
0
    def test_nested_loop(self):
        test_stream = io.StringIO(''.join([
            "First line.",
            "{{header}}",
            "{{#loop somearray item}}",
                "This is a {{item}}.",
                "You can combine:",
                    "{{#loop food item2}}",
                        "{{item}} - {{item2}}"
                    "{{/loop}}"
            "{{/loop}}",
            "{{footer}}",
            "Last line."]))

        variables = {
            "header": "Hello!",
            "somearray": ["apple", "banana"],
            "food": ["chicken", "pie"],
            "footer": "That's it!"
        }

        engine = TemplatingEngine(variables)

        output = io.StringIO()
        engine.process(test_stream, output)

        expected = ''.join(["First line.",
                            "Hello!",
                            "This is a apple.",
                            "You can combine:"
                            "apple - chicken",
                            "apple - pie",
                            "This is a banana.",
                            "You can combine:",
                            "banana - chicken",
                            "banana - pie",
                            "That's it!",
                            "Last line."])

        actual = output.getvalue()

        self.assertEqual(expected, actual)
예제 #8
0
    def test_loop_invalid_template_no_arguments_raise_error(self):
        test_stream = io.StringIO("text with {{#loop var}} .")

        variables = {
            "var": ["variable", "variable1"]
        }

        engine = TemplatingEngine(variables, throw_invalid=True)

        output = io.StringIO()

        self.assertRaises(ValueError, engine.process, test_stream, output)
예제 #9
0
    def test_loop_different_template_symbols(self):
        test_stream = io.StringIO(''.join([
            "First line.",
            "<<header>>",
            "<<*loop somearray item>>",
            "This is a <<item>>.",
            "<<@loop>>",
            "<<footer>>",
            "Last line."]))

        variables = {
            "header": "Hello!",
            "somearray": ["apple", "banana", "citrus"],
            "footer": "That's it!"
        }

        template_opening = "<<"
        template_close = ">>"
        function_open = "*"
        function_close = "@"
        engine = TemplatingEngine(variables, template_opening, template_close,
                                  function_open, function_close)

        output = io.StringIO()
        engine.process(test_stream, output)

        expected = ''.join(["First line.",
                            "Hello!",
                            "This is a apple.",
                            "This is a banana.",
                            "This is a citrus.",
                            "That's it!",
                            "Last line."])

        actual = output.getvalue()

        self.assertEqual(expected, actual)
예제 #10
0
def main(argv):
    try:
        opts, args = getopt.getopt(argv,
                                   "hi:o:v:",
                                   ["input-file=",
                                    "output-file=",
                                    "variables=",
                                    "template-open=",
                                    "template-close=",
                                    "func-open=",
                                    "func-close=",
                                    "raise"])
    except getopt.GetoptError:
        print_help()
        sys.exit(2)

    input_file = ''
    output_file = ''
    variables_json = ''
    additional_params = {}

    for opt, arg in opts:
        if opt == '-h':
            print_help()
            sys.exit()
        elif opt in ("-i", "--input-file"):
            input_file = arg
        elif opt in ("-o", "--output-file"):
            output_file = arg
        elif opt in ("-v", "--variables"):
            variables_json = arg
        elif opt == '--template-open':
            additional_params['template_open'] = arg
        elif opt == '--template-close':
            additional_params['template_close'] = arg
        elif opt == '--func-open':
            additional_params['function_open'] = arg
        elif opt == '--func-close':
            additional_params['function_close'] = arg
        elif opt == '--raise':
            additional_params['throw_invalid'] = True
            
    if not input_file:
        print("Please provide input file with -i or --input-file")
        sys.exit(2)

    if not output_file:
        print("Please provide input file with -o or --output-file")
        sys.exit(2)

    if not variables_json:
        print("Please provide template variables as json with -v or --variables")
        sys.exit(2)

    print('Input file is ', input_file)
    print('Output file is ', output_file)
    print('Variables: ', variables_json)

    variables = json.loads(variables_json)
    additional_params['global_variables'] = variables

    engine = TemplatingEngine(**additional_params)

    with open(input_file, 'r', 5120, 'utf-8') as input_stream:
        with open(output_file, 'w', 5120, 'utf-8') as output_stream:
            engine.process(input_stream, output_stream)