# This file is part of Beremiz, a Integrated Development Environment for
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
# Copyright (C) 2017: Andrey Skvortsov
# 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 __future__ import absolute_import
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, 'rb')
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:
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, 'rb')
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)
colidx = headers[colname]
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, 'rb')
dialect = csv.Sniffer().sniff(csvfile.read(1024))
reader = csv.reader(csvfile, dialect)
csv_int_files[fname] = data
if rowidx > 0 and colidx >= len(data[0]):
row.extend([""] * (colidx - len(row)) + [content])
wfile = open(fname, 'wb')
writer = csv.writer(wfile) if not(dialect) else csv.writer(wfile, dialect)
global csv_int_files, csv_str_files
class PythonLibrary(POULibrary):
def GetLibraryPath(self):
return paths.AbsNeighbourFile(__file__, "pous.xml")
def Generate_C(self, buildpath, varlist, IECCFLAGS):
plc_python_filepath = paths.AbsNeighbourFile(__file__, "plc_python.c")
plc_python_file = open(plc_python_filepath, 'r')
plc_python_code = plc_python_file.read()
if v["vartype"] == "FB" and v["type"] in ["PYTHON_EVAL",
python_eval_fb_list.append(v)
python_eval_fb_count = max(1, len(python_eval_fb_list))
plc_python_code = plc_python_code % {
"python_eval_fb_count": python_eval_fb_count}
Gen_Pythonfile_path = os.path.join(buildpath, "py_ext.c")
pythonfile = open(Gen_Pythonfile_path, 'w')
pythonfile.write(plc_python_code)
runtimefile_path = os.path.join(buildpath, "runtime_00_pyext.py")
runtimefile = open(runtimefile_path, 'w')
runtimefile.write(pyext_python_lib_code)
return ((["py_ext"], [(Gen_Pythonfile_path, IECCFLAGS)], True), "",
("runtime_00_pyext.py", open(runtimefile_path, "rb")))
class PythonFile(PythonFileCTNMixin):