# coding=utf8
# Copyright (c) 2016-2017, System Engineering Software Society
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the System Engineering Software Society nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.
# IN NO EVENT SHALL SYSTEM ENGINEERING SOFTWARE SOCIETY BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from __future__ import (print_function, division, unicode_literals,
absolute_import)
import jinja2
from sympathy.api import node as synode
from sympathy.api.nodeconfig import Port, Ports, Tag, Tags
[docs]class ConcatenateTexts(synode.Node):
"""Concatenate two texts"""
name = 'Concatenate texts'
nodeid = 'org.sysess.sympathy.texts.concatenatetexts'
author = 'Magnus Sandén <magnus.sanden@combine.se>'
copyright = "(C) 2016 System Engineering Software Society"
version = '1.0'
icon = 'concatenate.svg'
tags = Tags(Tag.DataProcessing.Text)
inputs = Ports([Port.Text('Text 1', name='in1'),
Port.Text('Text 2', name='in2')])
outputs = Ports([Port.Text('Concatenated text', name='out')])
def execute(self, node_context):
in1 = node_context.input['in1'].get()
in2 = node_context.input['in2'].get()
node_context.output['out'].set(in1 + in2)
[docs]class Jinja2Template(synode.Node):
"""Create and render a jinja2 template. See `Jinja2 <http://jinja.pocoo.org/>`_ for full syntax of the template engine.
Use {{column name}} for accessing the first row of a column, or use 'arg' inside a jinja for-loop to access full table.
Example of iterating over each column::
{% for name in arg.column_names() %}
The column name is: {{name}}
The column data is: {% for value in arg.col(name).data %} {{value}} {% endfor %}
{% endfor %}
Example of iterating over each row::
{% for row in arg.to_rows() %}
{% for value in row %} {{value}} {% endfor %}
{% endfor %}
"""
name = 'Jinja2 template'
nodeid = 'org.sysess.sympathy.texts.jinja2template'
author = 'Magnus Sandén <magnus.sanden@combine.se'
copyright = "(C) 2016 System Engineering Software Society"
version = '0.1'
icon = 'jinja_template.svg'
description = 'Create and render a jinja2 template. Use "{{column name}}" for access the first row of columns, or use "arg" inside a jinja for-loop to access full table. See help for full syntax and examples'
tags = Tags(Tag.DataProcessing.Text)
parameters = synode.parameters()
jinja_code_editor = synode.Util.code_editor(language="jinja").value()
parameters.set_string(
'template', label="Template:", description='Enter template here',
editor=jinja_code_editor)
inputs = Ports([Port.Table('Input data', name='in')])
outputs = Ports([Port.Text('Rendered Template', name='out')])
def execute(self, node_context):
infile = node_context.input['in']
outfile = node_context.output['out']
parameters = node_context.parameters
template_string = parameters['template'].value
jinja_env = jinja2.Environment()
template = jinja_env.from_string(template_string)
# Add metadata to template
metadata = {'arg': infile}
for col in infile.cols():
metadata[col.name] = col.data[0].tolist()
rendered_template = template.render(metadata)
outfile.set(rendered_template)