コード例 #1
0
ファイル: application.py プロジェクト: Marlysys-SAS/jinjalive
def render():
    """

    """
    try:
        # Get POST data
        params = request.get_json()
        template = params['template'].replace("\t", " " * 4)
        raw_context = params['context']
        showwhitespaces = params.get('showwhitespaces', False)

        # Load context, first try with JSON, if it fails, try YAML
        try:
            context = json.loads(raw_context)
        except:
            context = yaml.safe_load(raw_context)

        # Render the template with variables
        output = jinja_render(context, template)

        # HTML-ize the output
        if showwhitespaces:
            output = output.replace(' ', '•')

        # Return rendered template, with a 200 status code
        return output, 200
    except Exception, e:
        traceback.print_exc()
        # Return error message, with a 500 status code
        return str(e), 500
コード例 #2
0
 def test_010_simple_variable(self):
     '''
     Test Jinja rendering success for a simple variable
     '''
     random_uuid = str(uuid.uuid4())
     context = {'uuid': random_uuid}
     template = "UUID={{ uuid }}"
     output = jinja_render(context, template)
     self.assertIn(random_uuid, output)
コード例 #3
0
 def test_040_nested_variable(self):
     '''
     Test Jinja rendering success for a nested variable
     '''
     random_uuids = [str(uuid.uuid4()) for _ in range(10)]
     context = {'host': {'uuids': random_uuids}}
     template = "{% for uuid in host.uuids %}{{ uuid }}{% endfor %}"
     output = jinja_render(context, template)
     for random_uuid in random_uuids:
         self.assertIn(random_uuid, output)
コード例 #4
0
 def test_030_list_variable_for(self):
     '''
     Test Jinja rendering success for a list variable (for loop)
     '''
     random_uuids = [str(uuid.uuid4()) for _ in range(10)]
     context = {'uuids': random_uuids}
     template = "{% for uuid in uuids %}{{ uuid }}{% endfor %}"
     output = jinja_render(context, template)
     for random_uuid in random_uuids:
         self.assertIn(random_uuid, output)
コード例 #5
0
 def test_020_list_variable_join(self):
     '''
     Test Jinja rendering success for a list variable (join)
     '''
     random_uuids = [str(uuid.uuid4()) for _ in range(10)]
     context = {'uuids': random_uuids}
     template = "UUIDS={{ uuids|join(' ') }}"
     output = jinja_render(context, template)
     for random_uuid in random_uuids:
         self.assertIn(random_uuid, output)
コード例 #6
0
ファイル: cli.py プロジェクト: Marlysys-SAS/jinjalive
    def process(self, context_filename, template_filename, output_filename):
        """
        The function executed by the main method.

        :param context_filename: Path of the context file, containing your variables.
        :type context_filename: str
        :param template_filename: Path of the Jinja template to be rendered.
        :type template_filename: str
        :param output_filename: Path of the rendered output file.
        :type output_filename: str

        :raises IOError: raised if given context or template file is not found.
        :raises UndefinedError: raised if a variable is used in the template file but is not defined in the context file.
        """
        # Start time (for duration display)
        start_time = datetime.now()

        # Check if context file exists
        if not os.path.exists(context_filename):
            raise IOError("Context file '%s' not found. Please check parameters." % os.path.abspath(context_filename))

        # Check if template file exists
        if not os.path.exists(template_filename):
            raise IOError("Template file '%s' not found. Please check parameters." % os.path.abspath(template_filename))

        # Display summary
        LOG.info("YAML context file:     %s" % os.path.abspath(context_filename))
        LOG.info("Jinja template file:   %s" % os.path.abspath(template_filename))

        # Loading context dictionary from YAML file
        with open(context_filename, "r") as yaml_file:
            context = yaml.load(yaml_file.read())

        # Read the template as a string
        with open(template_filename, "r") as template:
            template = template.read()

        # Render output
        rendered_output = jinja_render(context, template)
        LOG.info("%s OUTPUT %s" % ("-" * 36, "-" * 36))
        for line in rendered_output.splitlines():
            LOG.info(line)

        # Write rendered string in output file
        with open(output_filename, "wb") as output_file:
            output_file.write(rendered_output)

        LOG.info("-" * 80)
        LOG.info("Generation duration:   %s" % (datetime.now() - start_time))
        LOG.info("Generated output file: %s" % os.path.abspath(output_filename))