--- a/py_ext/pous.xml Fri Feb 07 10:52:09 2025 +0100
+++ b/py_ext/pous.xml Fri Feb 07 11:21:15 2025 +0100
@@ -1,7 +1,7 @@
<?xml version='1.0' encoding='utf-8'?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.plcopen.org/xml/tc6_0201" xmlns:xhtml="http://www.w3.org/1999/xhtml" xsi:schemaLocation="http://www.plcopen.org/xml/tc6_0201">
<fileHeader companyName="Beremiz" productName="Beremiz" productVersion="0.0" creationDateTime="2008-12-14T16:53:26"/>
- <contentHeader name="Beremiz non-standard POUs library" modificationDateTime="2024-12-06T15:13:47">
+ <contentHeader name="Beremiz non-standard POUs library" modificationDateTime="2025-02-03T14:57:54"> @@ -17,6 +17,512 @@
+ <pou name="csv_write_by_string" pouType="functionBlock"> + <variable name="ERROR"> + <variable name="RESULT"> + <variable name="FILE_NAME"> + <variable name="COLUMN"> + <variable name="CONTENT"> + <variable name="py_eval0"> + <derived name="python_eval"/> + <variable name="R_TRIG1"> + <derived name="R_TRIG"/> + <variable name="R_TRIG2"> + <derived name="R_TRIG"/> + <variable name="csv_refresh0"> + <derived name="csv_refresh"/> + <inVariable localId="8" executionOrderId="0" height="27" width="112" negated="false"> + <position x="384" y="128"/> + <relPosition x="112" y="16"/> + <expression>'CSVWrStr("'</expression> + <inVariable localId="52" executionOrderId="0" height="32" width="112" negated="false"> + <position x="216" y="296"/> + <relPosition x="112" y="16"/> + <expression>CONTENT</expression> + <comment localId="29" height="40" width="232"> + <position x="64" y="32"/> + <xhtml:p><![CDATA[Generate python code line]]></xhtml:p> + <block localId="40" width="104" height="80" typeName="python_eval" instanceName="py_eval0" executionOrderId="0"> + <position x="552" y="480"/> + <variable formalParameter="TRIG"> + <relPosition x="0" y="32"/> + <connection refLocalId="46" formalParameter="Q"> + <position x="552" y="512"/> + <position x="360" y="512"/> + <variable formalParameter="CODE"> + <relPosition x="0" y="64"/> + <connection refLocalId="41"> + <position x="552" y="544"/> + <position x="520" y="544"/> + <variable formalParameter="ACK"> + <relPosition x="104" y="32"/> + <variable formalParameter="RESULT"> + <relPosition x="104" y="64"/> + <continuation name="Code" localId="41" height="24" width="128"> + <position x="392" y="528"/> + <relPosition x="128" y="16"/> + <inVariable localId="42" height="24" width="64" executionOrderId="0" negated="false"> + <position x="208" y="496"/> + <relPosition x="64" y="16"/> + <expression>SAVE</expression> + <outVariable localId="43" height="32" width="40" executionOrderId="0" negated="false"> + <position x="736" y="400"/> + <relPosition x="0" y="16"/> + <connection refLocalId="40" formalParameter="ACK"> + <position x="736" y="416"/> + <position x="688" y="416"/> + <position x="688" y="512"/> + <position x="656" y="512"/> + <expression>ACK</expression> + <outVariable localId="44" height="24" width="64" executionOrderId="0" negated="false"> + <position x="688" y="584"/> + <relPosition x="0" y="8"/> + <connection refLocalId="40" formalParameter="RESULT"> + <position x="688" y="592"/> + <position x="672" y="592"/> + <position x="672" y="544"/> + <position x="656" y="544"/> + <expression>RESULT</expression> + <block localId="46" typeName="R_TRIG" instanceName="R_TRIG1" executionOrderId="0" height="48" width="64"> + <position x="296" y="480"/> + <variable formalParameter="CLK"> + <relPosition x="0" y="32"/> + <connection refLocalId="42"> + <position x="296" y="512"/> + <position x="272" y="512"/> + <variable formalParameter="Q"> + <relPosition x="64" y="32"/> + <block localId="33" typeName="LEFT" executionOrderId="0" height="64" width="56"> + <position x="736" y="512"/> + <variable formalParameter="IN"> + <relPosition x="0" y="32"/> + <connection refLocalId="40" formalParameter="RESULT"> + <position x="736" y="544"/> + <position x="656" y="544"/> + <variable formalParameter="L"> + <relPosition x="0" y="56"/> + <connection refLocalId="35"> + <position x="736" y="568"/> + <position x="724" y="568"/> + <position x="724" y="560"/> + <position x="712" y="560"/> + <variable formalParameter="OUT"> + <relPosition x="56" y="32"/> + <block localId="34" typeName="EQ" executionOrderId="0" height="72" width="64"> + <position x="880" y="512"/> + <variable formalParameter="IN1"> + <relPosition x="0" y="32"/> + <connection refLocalId="33" formalParameter="OUT"> + <position x="880" y="544"/> + <position x="792" y="544"/> + <variable formalParameter="IN2"> + <relPosition x="0" y="56"/> + <connection refLocalId="36"> + <position x="880" y="568"/> + <position x="848" y="568"/> + <variable formalParameter="OUT"> + <relPosition x="64" y="32"/> + <inVariable localId="35" executionOrderId="0" height="24" width="24" negated="false"> + <position x="688" y="552"/> + <relPosition x="24" y="8"/> + <expression>1</expression> + <inVariable localId="36" executionOrderId="0" height="32" width="40" negated="false"> + <position x="808" y="552"/> + <relPosition x="40" y="16"/> + <expression>'#'</expression> + <block localId="37" typeName="R_TRIG" instanceName="R_TRIG2" executionOrderId="0" height="48" width="64"> + <position x="736" y="456"/> + <variable formalParameter="CLK"> + <relPosition x="0" y="32"/> + <connection refLocalId="40" formalParameter="ACK"> + <position x="736" y="488"/> + <position x="688" y="488"/> + <position x="688" y="512"/> + <position x="656" y="512"/> + <variable formalParameter="Q"> + <relPosition x="64" y="32"/> + <block localId="39" typeName="AND" executionOrderId="0" height="72" width="64"> + <position x="984" y="456"/> + <variable formalParameter="IN1"> + <relPosition x="0" y="32"/> + <connection refLocalId="37" formalParameter="Q"> + <position x="984" y="488"/> + <position x="800" y="488"/> + <variable formalParameter="IN2" negated="true"> + <relPosition x="0" y="56"/> + <connection refLocalId="34" formalParameter="OUT"> + <position x="984" y="512"/> + <position x="974" y="512"/> + <position x="974" y="544"/> + <position x="944" y="544"/> + <variable formalParameter="OUT"> + <relPosition x="64" y="32"/> + <block localId="53" typeName="csv_refresh" instanceName="csv_refresh0" executionOrderId="0" width="104" height="48"> + <position x="1112" y="456"/> + <variable formalParameter="TRIG"> + <relPosition x="0" y="32"/> + <connection refLocalId="39" formalParameter="OUT"> + <position x="1112" y="488"/> + <position x="1048" y="488"/> + <outVariable localId="54" executionOrderId="0" width="56" height="32" negated="false"> + <position x="1096" y="528"/> + <relPosition x="0" y="16"/> + <connection refLocalId="34" formalParameter="OUT"> + <position x="1096" y="544"/> + <position x="944" y="544"/> + <expression>ERROR</expression> + <block localId="7" typeName="CONCAT" executionOrderId="0" height="240" width="67"> + <position x="536" y="112"/> + <variable formalParameter="IN1"> + <relPosition x="0" y="32"/> + <connection refLocalId="8"> + <position x="536" y="144"/> + <position x="496" y="144"/> + <variable formalParameter="IN2"> + <relPosition x="0" y="56"/> + <connection refLocalId="2"> + <position x="536" y="168"/> + <position x="328" y="168"/> + <variable formalParameter="IN3"> + <relPosition x="0" y="80"/> + <connection refLocalId="10"> + <position x="536" y="192"/> + <position x="496" y="192"/> + <variable formalParameter="IN4"> + <relPosition x="0" y="104"/> + <connection refLocalId="3"> + <position x="536" y="216"/> + <position x="328" y="216"/> + <variable formalParameter="IN5"> + <relPosition x="0" y="128"/> + <connection refLocalId="12"> + <position x="536" y="240"/> + <position x="496" y="240"/> + <variable formalParameter="IN6"> + <relPosition x="0" y="152"/> + <connection refLocalId="4"> + <position x="536" y="264"/> + <position x="328" y="264"/> + <variable formalParameter="IN7"> + <relPosition x="0" y="176"/> + <connection refLocalId="1"> + <position x="536" y="288"/> + <position x="496" y="288"/> + <variable formalParameter="IN8"> + <relPosition x="0" y="200"/> + <connection refLocalId="52"> + <position x="536" y="312"/> + <position x="328" y="312"/> + <variable formalParameter="IN9"> + <relPosition x="0" y="224"/> + <connection refLocalId="14"> + <position x="536" y="336"/> + <position x="496" y="336"/> + <variable formalParameter="OUT"> + <relPosition x="67" y="32"/> + <inVariable localId="2" executionOrderId="0" height="32" width="112" negated="false"> + <position x="216" y="152"/> + <relPosition x="112" y="16"/> + <expression>FILE_NAME</expression> + <inVariable localId="10" executionOrderId="0" height="24" width="112" negated="false"> + <position x="384" y="184"/> + <relPosition x="112" y="8"/> + <expression>'","'</expression> + <inVariable localId="3" executionOrderId="0" height="32" width="112" negated="false"> + <position x="216" y="200"/> + <relPosition x="112" y="16"/> + <expression>ROW</expression> + <inVariable localId="12" executionOrderId="0" height="24" width="112" negated="false"> + <position x="384" y="224"/> + <relPosition x="112" y="16"/> + <expression>'","'</expression> + <inVariable localId="4" executionOrderId="0" height="32" width="112" negated="false"> + <position x="216" y="248"/> + <relPosition x="112" y="16"/> + <expression>COLUMN</expression> + <inVariable localId="14" executionOrderId="0" height="24" width="112" negated="false"> + <position x="384" y="320"/> + <relPosition x="112" y="16"/> + <expression>'")'</expression> + <connector name="Code" localId="19" height="24" width="128"> + <position x="656" y="136"/> + <relPosition x="0" y="8"/> + <connection refLocalId="7" formalParameter="OUT"> + <position x="656" y="144"/> + <position x="603" y="144"/> + <inVariable localId="1" executionOrderId="0" height="24" width="112" negated="false"> + <position x="384" y="272"/> + <relPosition x="112" y="16"/> + <expression>'","'</expression> <pou name="_csv_update" pouType="functionBlock">
@@ -1058,7 +1564,7 @@
<relPosition x="176" y="16"/>
- <expression>'pyext_csv_reload()'</expression>
+ <expression>'CSVReload()'</expression> <block localId="15" typeName="csv_refresh" instanceName="csv_refresh0" executionOrderId="0" width="104" height="64">
<position x="568" y="32"/>
@@ -1930,9 +2436,9 @@
<inVariable localId="8" executionOrderId="0" height="24" width="160" negated="false">
- <position x="352" y="112"/>
+ <position x="352" y="104"/> - <relPosition x="160" y="8"/>
+ <relPosition x="160" y="16"/> <expression>'CSVWrInt("'</expression>
@@ -1944,9 +2450,9 @@
<expression>FILE_NAME</expression>
<inVariable localId="10" executionOrderId="0" height="24" width="112" negated="false">
- <position x="504" y="152"/>
+ <position x="504" y="160"/> - <relPosition x="112" y="16"/>
+ <relPosition x="112" y="8"/> <expression>'",'</expression>
@@ -1958,9 +2464,9 @@
<expression>ROW</expression>
<inVariable localId="12" executionOrderId="0" height="24" width="112" negated="false">
- <position x="504" y="208"/>
+ <position x="504" y="200"/> - <relPosition x="112" y="8"/>
+ <relPosition x="112" y="16"/> <expression>','</expression>
@@ -1972,9 +2478,9 @@
<expression>COLUMN</expression>
<inVariable localId="51" executionOrderId="0" height="24" width="112" negated="false">
- <position x="504" y="256"/>
+ <position x="504" y="248"/> - <relPosition x="112" y="8"/>
+ <relPosition x="112" y="16"/> <expression>',"'</expression>
@@ -1986,18 +2492,12 @@
<expression>CONTENT</expression>
<inVariable localId="14" executionOrderId="0" height="24" width="112" negated="false">
- <position x="504" y="304"/>
+ <position x="504" y="296"/> <relPosition x="112" y="16"/>
<expression>'")'</expression>
- <comment localId="28" height="48" width="520">
- <position x="48" y="400"/>
- <xhtml:p><![CDATA[Execute python code on change or globally when CSV is updated]]></xhtml:p>
<comment localId="29" height="40" width="232">
<position x="64" y="32"/>
@@ -2005,9 +2505,9 @@
<connector name="Code" localId="30" height="24" width="128">
- <position x="856" y="112"/>
+ <position x="856" y="104"/> - <relPosition x="0" y="8"/>
+ <relPosition x="0" y="16"/> <connection refLocalId="7" formalParameter="OUT">
<position x="856" y="120"/>
<position x="752" y="120"/>
--- a/py_ext/py_ext.py Fri Feb 07 10:52:09 2025 +0100
+++ b/py_ext/py_ext.py Fri Feb 07 11:21:15 2025 +0100
@@ -1,186 +1,21 @@
-# This file is part of Beremiz, a Integrated Development Environment for
-# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
+# This file is part of Beremiz IDE -# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
+# Copyright (C) 2013: Laurent BESSARD # Copyright (C) 2017: Andrey Skvortsov
+# Copyright (C) 2025: Edouard TISSERANT # See COPYING file for copyrights details.
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from POULibrary import POULibrary
from py_ext.PythonFileCTNMixin import PythonFileCTNMixin
import util.paths as paths
-pyext_python_lib_code = """
-from collections import OrderedDict
-def CSVRdInt(fname, rowidx, colidx):
- Return value at row/column pointed by integer indexes
- Assumes data starts at first row and first column, no headers.
- data = csv_int_files.get(fname, None)
- csvfile = open(fname, 'rt', encoding='utf-8')
- return "#FILE_NOT_FOUND"
- dialect = csv.Sniffer().sniff(csvfile.read(1024))
- reader = csv.reader(csvfile, dialect)
- csv_int_files[fname] = data
- if not row and rowidx == len(data)-1:
- return "#ROW_NOT_FOUND"
- return "#COL_NOT_FOUND"
-def CSVRdStr(fname, rowname, colname):
- Return value at row/column pointed by a pair of names as string
- Assumes first row is column headers and first column is row name.
- entry = csv_str_files.get(fname, None)
- csvfile = open(fname, 'rt', encoding='utf-8')
- return "#FILE_NOT_FOUND"
- dialect = csv.Sniffer().sniff(csvfile.read(1024))
- reader = csv.reader(csvfile, dialect)
- headers = dict([(name, index) for index, name in enumerate(reader.__next__()[1:])])
- csv_str_files[fname] = (headers, data)
- return "#ROW_NOT_FOUND"
- colidx = headers[colname]
- return "#COL_NOT_FOUND"
- return "#COL_NOT_FOUND"
-def CSVWrInt(fname, rowidx, colidx, content):
- Update value at row/column pointed by integer indexes
- Assumes data starts at first row and first column, no headers.
- data = csv_int_files.get(fname, None)
- csvfile = open(fname, 'rt', encoding='utf-8')
- return "#FILE_NOT_FOUND"
- dialect = csv.Sniffer().sniff(csvfile.read(1024))
- reader = csv.reader(csvfile, dialect)
- csv_int_files[fname] = data
- if rowidx == len(data):
- return "#ROW_NOT_FOUND"
- if rowidx > 0 and colidx >= len(data[0]):
- row.extend([""] * (colidx - len(row)) + [content])
- return "#COL_NOT_FOUND"
- wfile = open(fname, 'wt')
- writer = csv.writer(wfile) if not(dialect) else csv.writer(wfile, dialect)
- global csv_int_files, csv_str_files
+pyext_python_lib_code = open(paths.AbsNeighbourFile(__file__, "py_ext_rt.py"), "r").read() class PythonLibrary(POULibrary):
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/py_ext/py_ext_rt.py Fri Feb 07 11:21:15 2025 +0100
@@ -0,0 +1,297 @@
+# This file is part of Beremiz Runtime +# Copyright (C) 2013: Laurent BESSARD +# Copyright (C) 2017: Andrey Skvortsov +# Copyright (C) 2025: Edouard Tisserant +# See COPYING file for copyrights details. +from collections import OrderedDict + def __init__(self, *args): +def _CSV_int_Load(fname): + entry = csv_int_files.get(fname, None) + csvfile = open(fname, 'rt', encoding='utf-8') + dialect = csv.Sniffer().sniff(csvfile.read(1024)) + reader = csv.reader(csvfile, dialect) + entry = Entry(fname, dialect, data) + csv_int_files[fname] = entry +def _CSV_str_Load(fname): + entry = csv_str_files.get(fname, None) + csvfile = open(fname, 'rt', encoding='utf-8') + dialect = csv.Sniffer().sniff(csvfile.read(1024)) + reader = csv.reader(csvfile, dialect) + first_row = reader.__next__() + col_headers = OrderedDict([(name, index+1) for index, name + in enumerate(first_row[1:])]) + max_row_len = len(first_row) + row_headers = OrderedDict() + for index, row in enumerate(reader): + row_headers[row[0]] = index+1 + max_row_len = max(max_row_len, len(row)) + entry = Entry(fname, dialect, col_headers, row_headers, max_row_len, data) + csv_str_files[fname] = entry +def _CSV_str_Create(fname): + data = [[]] # start with an empty row, acounting for header row + col_headers = OrderedDict() + row_headers = OrderedDict() + max_row_len = 1 # set to one initialy, accounting for header column + entry = Entry(fname, dialect, col_headers, row_headers, max_row_len, data) + csv_str_files[fname] = entry +def _CSV_Save_data(fname, dialect, data): + wfile = open(fname, 'wt') + writer = csv.writer(wfile) if not(dialect) else csv.writer(wfile, dialect) +def _CSV_int_Save(entry): + fname, dialect, data = entry() + _CSV_Save_data(fname, dialect, data) +def _CSV_str_Save(entry): + fname, dialect, col_headers, row_headers, max_row_len, data = entry() + _CSV_Save_data(fname, dialect, data) +_already_registered_cb = False +def _CSV_OnIdle_callback(): + global _already_registered_cb, cvs_int_changed, cvs_str_changed + _already_registered_cb = False + while len(cvs_int_changed): + entry = cvs_int_changed.pop() + while len(cvs_str_changed): + entry = cvs_str_changed.pop() +def _CSV_register_OnIdle_callback(): + global _already_registered_cb + if not _already_registered_cb: + OnIdle.append(_CSV_OnIdle_callback) + _already_registered_cb = True +def _CSV_int_modified(entry): + cvs_int_changed.add(entry) + _CSV_register_OnIdle_callback() +def _CSV_str_modified(entry): + cvs_str_changed.add(entry) + _CSV_register_OnIdle_callback() +def CSVRdInt(fname, rowidx, colidx): + Return value at row/column pointed by integer indexes + Assumes data starts at first row and first column, no headers. + _fname, _dialect, data = _CSV_int_Load(fname)() + return "#FILE_NOT_FOUND" + if not row and rowidx == len(data)-1: + return "#ROW_NOT_FOUND" + return "#COL_NOT_FOUND" +def CSVRdStr(fname, rowname, colname): + Return value at row/column pointed by a pair of names as string + Assumes first row is column headers and first column is row name. + return "#INVALID_COLUMN" + fname, dialect, col_headers, row_headers, max_row_len, data = _CSV_str_Load(fname)() + return "#FILE_NOT_FOUND" + rowidx = row_headers[rowname] + return "#ROW_NOT_FOUND" + colidx = col_headers[colname] + return "#COL_NOT_FOUND" + return data[rowidx][colidx] + return "#COL_NOT_FOUND" +def CSVWrInt(fname, rowidx, colidx, content): + Update value at row/column pointed by integer indexes + Assumes data starts at first row and first column, no headers. + entry = _CSV_int_Load(fname) + return "#FILE_NOT_FOUND" + fname, dialect, data = entry() + if rowidx == len(data): + return "#ROW_NOT_FOUND" + if rowidx > 0 and colidx >= len(data[0]): + row.extend([""] * (colidx - len(row)) + [content]) + return "#COL_NOT_FOUND" + _CSV_int_modified(entry) +def CSVWrStr(fname, rowname, colname, content): + Update value at row/column pointed by a pair of names as string. + Assumes first row is column headers and first column is row name. + return "#INVALID_COLUMN" + entry = _CSV_str_Load(fname) + entry = _CSV_str_Create(fname) + fname, dialect, col_headers, row_headers, max_row_len, data = entry() + rowidx = row_headers[rowname] + # create a new row with appropriate header + row_headers[rowname] = rowidx + colidx = col_headers[colname] + # adjust col headers content + first_row += [""]*(max_row_len - len(first_row)) + [rowname] + colidx = col_headers[colname] = max_row_len + max_row_len = max_row_len + 1 + row += [""]*(colidx - len(row)) + [content] + _CSV_str_modified(entry) + global csv_int_files, csv_str_files, cvs_int_changed, cvs_str_changed + # Force saving modified CSV files + cvs_int_changed.clear() + cvs_str_changed.clear()