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
// overrides yslt's output function to set CDATA
decl output(method, cdata-section-elements="xhtml:script");
// helper to emit some content to internal namespaces
decl emit(*name) alias - {
*name;
template *name {
// value "ns:ProgressStart(name())";
|
| /* «local-name()» */
|
content;
|
// value "ns:ProgressEnd(name())";
}
};
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"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
/* Internal namespaces to allow emit code/content from anywhere */
xmlns:debug="debug"
xmlns:preamble="preamble"
xmlns:declarations="declarations"
xmlns:definitions="definitions"
xmlns:epilogue="epilogue"
xmlns:cssdefs="cssdefs"
/* 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 debug preamble epilogue declarations definitions" {
const "svg", "/svg:svg";
const "hmi_elements", "//svg:*[starts-with(@inkscape:label, 'HMI:')]";
include hmi_tree.ysl2
include geometry.ysl2
include lists.ysl2
include detachable_pages.ysl2
include inline_svg.ysl2
include i18n.ysl2
include widgets_common.ysl2
include widget_*.ysl2
template "/" {
comment > Made with SVGHMI. https://beremiz.org
// all debug output from included definitions, as comments
// comment apply "document('')/*/debug:*";
html xmlns="http://www.w3.org/1999/xhtml"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" {
head {
style type="text/css" media="screen" {
value "ns:GetFonts()";
apply "document('')/*/cssdefs:*";
}
}
// prevents user selection by mouse click / touch and drag
// prevents pinch zoom and other accidental panning panning with touch devices
body style="margin:0;overflow:hidden;user-select:none;touch-action:none;" {
// Inline SVG
copy "$result_svg";
script{
include text pythonic.js
| \n//\n//\n// Early independent declarations \n//\n//
apply "document('')/*/preamble:*";
| \n//\n//\n// Declarations depending on preamble \n//\n//
apply "document('')/*/declarations:*";
| \n//\n//\n// Order independent declaration and code \n//\n//
apply "document('')/*/definitions:*";
| \n//\n//\n// Statements that needs to be at the end \n//\n//
apply "document('')/*/epilogue:*";
include text sprintf.js
include text svghmi.js
| \n//\n//\n// Declarations from SVG scripts (inkscape document properties) \n//\n//
foreach "/svg:svg/svg:script" {
|
| /* «@id» */
| «text()»
}
}
}
}
}
}