Beispiel #1
0
    def test_unicode_templating(self):
        # Unicode template and unicode substitution
        unicode_template_string = u'my name is 指 and my value is $var'
        unicode_variables = {'var': u'漢'}
        normal_variables = {'var': u'bob'}
        substituted = Parser.safe_substitute_unicode_template(unicode_template_string, unicode_variables)
        self.assertEqual(u'my name is 指 and my value is 漢', substituted)

        # Normal template and unicode substitution
        normal_template_string = 'my normal name is blah and my unicode name is $var'
        substituted = Parser.safe_substitute_unicode_template(normal_template_string, unicode_variables)
        self.assertEqual(u'my normal name is blah and my unicode name is 漢', substituted)

        # Unicode template and normal substitution
        substituted = Parser.safe_substitute_unicode_template(unicode_template_string, normal_variables)
        self.assertEqual(u'my name is 指 and my value is bob', substituted)
Beispiel #2
0
    def get_content(self, context=None):
        """ Does all context binding and pathing to get content, templated out """

        if self.is_file:
            path = self.content
            if self.is_template_path and context:
                path = string.Template(path).safe_substitute(
                    context.get_values())
            with open(path, 'r') as f:
                data = f.read()

            if self.is_template_content and context:
                return string.Template(data).safe_substitute(context.get_values())
            else:
                return data
        else:
            if self.is_template_content and context:
                return Parser.safe_substitute_unicode_template(self.content, context.get_values())
            else:
                return self.content
Beispiel #3
0
 def test_safe_substitute_unicode_template(self):
     result = Parser.safe_substitute_unicode_template("This is $x test", {'x': 'unit'})
     self.assertEqual("This is unit test", result)