Esempio n. 1
0
    def test_render__kwargs_does_not_modify_context(self):
        """
        Test render(): passing **kwargs does not modify the passed context.

        """
        context = {}
        renderer = Renderer()
        renderer.render('Hi {{person}}', context=context, foo="bar")
        self.assertEquals(context, {})
Esempio n. 2
0
    def test_render__object(self):
        """
        Test rendering an object instance.

        """
        renderer = Renderer()

        say_hello = SayHello()
        actual = renderer.render(say_hello)
        self.assertEquals('Hello, World', actual)

        actual = renderer.render(say_hello, to='Mars')
        self.assertEquals('Hello, Mars', actual)
Esempio n. 3
0
    def test_render__nonascii_template(self):
        """
        Test passing a non-unicode template with non-ascii characters.

        """
        renderer = Renderer()
        template = "déf"

        # Check that decode_errors and default_encoding are both respected.
        renderer.decode_errors = 'ignore'
        renderer.default_encoding = 'ascii'
        self.assertEquals(renderer.render(template), "df")

        renderer.default_encoding = 'utf_8'
        self.assertEquals(renderer.render(template), u"déf")
Esempio n. 4
0
def pystache_render(file_in, file_out, config):
    """Render a pystache template. file_in is the input
    file path. file_out is the output file path. config is a
    dictionary with the context data.

    Note: this function will create any directories neccessary to
    enable writing of the output file.

    """
    renderer = pystache.renderer.Renderer()
    renderer.register_formatter('u', lambda x: x.upper())
    # Note: This can only be used on integer values
    renderer.register_formatter('hex', lambda x: str(hex(int(x))))

    with open(file_in, 'r') as inp:
        template_data = inp.read()

    try:
        parsed_template = pystache.parser.parse(template_data, name=file_in)
        data = renderer.render(parsed_template, config)
    except pystache.common.PystacheError as e:
        raise SystemBuildError("Error rendering template '{}'. {}.".format(
            e.location, str(e)))

    os.makedirs(os.path.dirname(file_out), exist_ok=True)

    with open(file_out, 'w') as outp:
        outp.write(data)
Esempio n. 5
0
def pystache_render(file_in, file_out, config):
    """Render a pystache template. file_in is the input
    file path. file_out is the output file path. config is a
    dictionary with the context data.

    Note: this function will create any directories neccessary to
    enable writing of the output file.

    """
    renderer = pystache.renderer.Renderer()
    renderer.register_formatter('u', lambda x: x.upper())
    # Note: This can only be used on integer values
    renderer.register_formatter('hex', lambda x: str(hex(int(x))))

    with open(file_in, 'r') as inp:
        template_data = inp.read()

    try:
        parsed_template = pystache.parser.parse(template_data, name=file_in)
        data = renderer.render(parsed_template, config)
    except pystache.common.PystacheError as e:
        raise SystemBuildError("Error rendering template '{}'. {}.".format(e.location, str(e)))

    os.makedirs(os.path.dirname(file_out), exist_ok=True)

    with open(file_out, 'w') as outp:
        outp.write(data)
Esempio n. 6
0
    def test_render__context_and_kwargs__precedence(self):
        """
        Test render(): **kwargs takes precedence over context.

        """
        renderer = Renderer()
        self.assertEquals(renderer.render('Hi {{person}}', {'person': 'Mom'}, person='Dad'), 'Hi Dad')
Esempio n. 7
0
    def test_render__context(self):
        """
        Test render(): passing a context.

        """
        renderer = Renderer()
        self.assertEquals(renderer.render('Hi {{person}}', {'person': 'Mom'}), 'Hi Mom')
Esempio n. 8
0
    def test_render__kwargs_and_no_context(self):
        """
        Test render(): passing **kwargs and no context.

        """
        renderer = Renderer()
        self.assertEquals(renderer.render('Hi {{person}}', person='Mom'), 'Hi Mom')
Esempio n. 9
0
    def test_render__context_and_kwargs(self):
        """
        Test render(): passing a context and **kwargs.

        """
        renderer = Renderer()
        template = 'Hi {{person1}} and {{person2}}'
        self.assertEquals(renderer.render(template, {'person1': 'Mom'}, person2='Dad'), 'Hi Mom and Dad')
Esempio n. 10
0
    def test_render__return_type(self):
        """
        Check that render() returns a string of type unicode.

        """
        renderer = Renderer()
        rendered = renderer.render('foo')
        self.assertEquals(type(rendered), unicode)
Esempio n. 11
0
    def test_render__view(self):
        """
        Test rendering a View instance.

        """
        renderer = Renderer()

        view = Simple()
        actual = renderer.render(view)
        self.assertEquals('Hi pizza!', actual)
Esempio n. 12
0
    def renders_impl(self, template_content, context, at_paths=None,
                     at_encoding=anytemplate.compat.ENCODING,
                     **kwargs):
        """
        Render given template string and return the result.

        :param template_content: Template content
        :param context: A dict or dict-like object to instantiate given
            template file
        :param at_paths: Template search paths
        :param at_encoding: Template encoding
        :param kwargs: Keyword arguments passed to the template engine to
            render templates with specific features enabled.

        :return: Rendered string
        """
        renderer = self._make_renderer(at_paths, at_encoding, **kwargs)
        ctxs = [] if context is None else [context]

        return renderer.render(template_content, *ctxs)
Esempio n. 13
0
 def test_render__non_ascii_character(self):
     renderer = Renderer()
     actual = renderer.render(u'Poincaré')
     self.assertEquals(actual, u'Poincaré')
Esempio n. 14
0
 def test_render__str(self):
     renderer = Renderer()
     actual = renderer.render('foo')
     self.assertEquals(actual, 'foo')
Esempio n. 15
0
 def test_render__unicode(self):
     renderer = Renderer()
     actual = renderer.render(u'foo')
     self.assertEquals(actual, u'foo')