beremiz

py_ext: fix CSV Writer

18 months ago, Edouard Tisserant
d2f5eb3c7d6e
py_ext: fix CSV Writer

fix POU logic :
- SAVE is a BOOL
- invocation of py_eval on rising edge of SAVE
- remove save python argument

fix python:
- use no encoding for file open (python2)
- re-use detected dialect if any
- use no "rt+" and truncate since no need to re-sniff dialect for output file
- return "OK" instead of "#SUCCESS", preventing POU logic to ACK result
- support creating new line if writing just after last line
- support appending data on short rows

fix example:
- use a HMI:Button to trigger CSV write instead of HMI:Input +1
- reload CSVs on on each new CSV opened in file browser
- add display of CSV write output
include yslt_noindent.yml2
in xsl decl svgtmpl(match, xmlns="http://www.w3.org/2000/svg") alias template;
istylesheet
/* From Inkscape */
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
/* Namespace to invoke python code */
xmlns:ns="beremiz"
extension-element-prefixes="ns func exsl regexp str dyn"
exclude-result-prefixes="ns func exsl regexp str dyn" {
const "hmi_elements", "//svg:*[starts-with(@inkscape:label, 'HMI:')]";
const "widgetparams", "ns:GetWidgetParams()";
const "indexed_hmitree", "/.."; // compatibility with parse_labels.ysl2
include parse_labels.ysl2
const "_parsed_widgets" apply "$hmi_elements", mode="parselabel";
const "parsed_widgets","exsl:node-set($_parsed_widgets)";
const "svg_widget", "$parsed_widgets/widget[1]";
const "svg_widget_type", "$svg_widget/@type";
const "svg_widget_path", "$svg_widget/@path";
const "svg_widget_count", "count($parsed_widgets/widget)";
// Templates to change label paths(s)
template "@* | node()", mode="replace_params" {
xsl:copy apply "@* | node()", mode="replace_params";
}
template "arg", mode="replace_params";
template "path", mode="replace_params";
template "widget", mode="replace_params" {
xsl:copy {
apply "@* | node()", mode="replace_params";
copy "$widgetparams/*";
};
}
// all attribs are usually copied
svgtmpl "@*", mode="inline_svg" xsl:copy;
// except labels, ignored
svgtmpl "@inkscape:label[starts-with(., 'HMI:')]", mode="inline_svg";
template "node()", mode="inline_svg" xsl:copy {
// in case this node widget's main element inject label
if "@id = $svg_widget/@id" {
const "substituted_widget" apply "$svg_widget", mode="replace_params";
const "substituted_widget_ns", "exsl:node-set($substituted_widget)";
const "new_label" apply "$substituted_widget_ns", mode="genlabel";
attrib "inkscape:label" > «$new_label»
}
// all nodes are copied as well
apply "@* | node()", mode="inline_svg";
}
template "/" {
comment > Widget dropped in Inkscape from Beremiz
choose {
when "$svg_widget_count < 1"
error > No widget detected on selected SVG
when "$svg_widget_count > 1"
error > Multiple widget DnD not yet supported
}
apply "/", mode="inline_svg";
}
}