andrej@1511: #!/usr/bin/env python andrej@1511: # -*- coding: utf-8 -*- andrej@1511: andrej@1511: # This file is part of Beremiz, a Integrated Development Environment for andrej@1511: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival. andrej@1511: # andrej@1511: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD andrej@1680: # Copyright (C) 2017: Andrey Skvortsov andrej@1511: # andrej@1511: # See COPYING file for copyrights details. andrej@1511: # andrej@1511: # This program is free software; you can redistribute it and/or andrej@1511: # modify it under the terms of the GNU General Public License andrej@1511: # as published by the Free Software Foundation; either version 2 andrej@1511: # of the License, or (at your option) any later version. andrej@1511: # andrej@1511: # This program is distributed in the hope that it will be useful, andrej@1511: # but WITHOUT ANY WARRANTY; without even the implied warranty of andrej@1511: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the andrej@1511: # GNU General Public License for more details. andrej@1511: # andrej@1511: # You should have received a copy of the GNU General Public License andrej@1511: # along with this program; if not, write to the Free Software andrej@1511: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. andrej@1511: andrej@1853: laurent@366: import os Edouard@728: from POULibrary import POULibrary andrej@1853: from py_ext.PythonFileCTNMixin import PythonFileCTNMixin andrej@1680: import util.paths as paths laurent@366: Edouard@3707: pyext_python_lib_code = """ Edouard@3707: Edouard@3707: import csv Edouard@3707: from collections import OrderedDict Edouard@3707: Edouard@3707: csv_int_files = {} Edouard@3707: def CSVRdInt(fname, rowidx, colidx): Edouard@3707: \"\"\" Edouard@3707: Return value at row/column pointed by integer indexes Edouard@3707: Assumes data starts at first row and first column, no headers. Edouard@3707: \"\"\" Edouard@3707: global csv_int_files Edouard@3707: data = csv_int_files.get(fname, None) Edouard@3707: if data is None: Edouard@3707: data = list() Edouard@3707: try: 44305363+kraskrom@4056: csvfile = open(fname, 'rt', encoding='utf-8') Edouard@3707: except IOError: Edouard@3707: return "#FILE_NOT_FOUND" Edouard@3707: try: Edouard@3707: dialect = csv.Sniffer().sniff(csvfile.read(1024)) Edouard@3707: csvfile.seek(0) Edouard@3707: reader = csv.reader(csvfile, dialect) Edouard@3707: for row in reader: Edouard@3707: data.append(row) 44305363+kraskrom@4056: except csv.Error as e: Edouard@3707: return "#CSV_ERROR" Edouard@3707: finally: Edouard@3707: csvfile.close() Edouard@3707: csv_int_files[fname] = data 44305363+kraskrom@4056: Edouard@3707: try: Edouard@3707: row = data[rowidx] edouard@4060: if not row and rowidx == len(data)-1: edouard@4060: raise IndexError Edouard@3707: except IndexError: Edouard@3707: return "#ROW_NOT_FOUND" Edouard@3707: Edouard@3707: try: Edouard@3707: return row[colidx] Edouard@3707: except IndexError: Edouard@3707: return "#COL_NOT_FOUND" Edouard@3707: Edouard@3707: Edouard@3707: csv_str_files = {} Edouard@3707: def CSVRdStr(fname, rowname, colname): Edouard@3707: \"\"\" Edouard@3707: Return value at row/column pointed by a pair of names as string Edouard@3707: Assumes first row is column headers and first column is row name. Edouard@3707: \"\"\" Edouard@3707: global csv_str_files Edouard@3707: entry = csv_str_files.get(fname, None) Edouard@3707: if entry is None: Edouard@3707: data = dict() Edouard@3707: try: 44305363+kraskrom@4056: csvfile = open(fname, 'rt', encoding='utf-8') Edouard@3707: except IOError: Edouard@3707: return "#FILE_NOT_FOUND" Edouard@3707: try: Edouard@3707: dialect = csv.Sniffer().sniff(csvfile.read(1024)) Edouard@3707: csvfile.seek(0) Edouard@3707: reader = csv.reader(csvfile, dialect) 44305363+kraskrom@4056: headers = dict([(name, index) for index, name in enumerate(reader.__next__()[1:])]) Edouard@3707: for row in reader: Edouard@3707: data[row[0]] = row[1:] Edouard@3707: except csv.Error: Edouard@3707: return "#CSV_ERROR" Edouard@3707: finally: Edouard@3707: csvfile.close() Edouard@3707: csv_str_files[fname] = (headers, data) Edouard@3707: else: Edouard@3707: headers, data = entry 44305363+kraskrom@4056: Edouard@3707: try: Edouard@3707: row = data[rowname] Edouard@3707: except KeyError: Edouard@3707: return "#ROW_NOT_FOUND" Edouard@3707: Edouard@3707: try: Edouard@3707: colidx = headers[colname] Edouard@3707: except KeyError: Edouard@3707: return "#COL_NOT_FOUND" Edouard@3707: Edouard@3707: try: Edouard@3707: return row[colidx] Edouard@3707: except IndexError: Edouard@3707: return "#COL_NOT_FOUND" Edouard@3707: 44305363+kraskrom@4056: edouard@4060: def CSVWrInt(fname, rowidx, colidx, content): 44305363+kraskrom@4056: \"\"\" 44305363+kraskrom@4056: Update value at row/column pointed by integer indexes 44305363+kraskrom@4056: Assumes data starts at first row and first column, no headers. 44305363+kraskrom@4056: \"\"\" edouard@4060: edouard@4060: global csv_int_files edouard@4060: dialect = None edouard@4060: data = csv_int_files.get(fname, None) edouard@4060: if data is None: edouard@4060: data = list() edouard@4060: try: edouard@4060: csvfile = open(fname, 'rt', encoding='utf-8') edouard@4060: except IOError: edouard@4060: return "#FILE_NOT_FOUND" edouard@4060: try: edouard@4060: dialect = csv.Sniffer().sniff(csvfile.read(1024)) edouard@4060: csvfile.seek(0) edouard@4060: reader = csv.reader(csvfile, dialect) edouard@4060: for row in reader: edouard@4060: data.append(row) edouard@4060: except csv.Error as e: edouard@4060: return "#CSV_ERROR" edouard@4060: finally: edouard@4060: csvfile.close() edouard@4060: csv_int_files[fname] = data edouard@4060: edouard@4060: try: edouard@4060: if rowidx == len(data): edouard@4060: row = [] edouard@4060: data.append(row) edouard@4060: else: 44305363+kraskrom@4056: row = data[rowidx] edouard@4060: except IndexError: edouard@4060: return "#ROW_NOT_FOUND" edouard@4060: edouard@4060: try: edouard@4060: if rowidx > 0 and colidx >= len(data[0]): edouard@4060: raise IndexError edouard@4060: if colidx >= len(row): edouard@4060: row.extend([""] * (colidx - len(row)) + [content]) edouard@4060: else: 44305363+kraskrom@4056: row[colidx] = content edouard@4060: except IndexError: edouard@4060: return "#COL_NOT_FOUND" edouard@4060: edouard@4060: try: edouard@4060: wfile = open(fname, 'wt') edouard@4060: writer = csv.writer(wfile) if not(dialect) else csv.writer(wfile, dialect) 44305363+kraskrom@4056: for row in data: 44305363+kraskrom@4056: writer.writerow(row) edouard@4060: finally: 44305363+kraskrom@4056: wfile.close() edouard@4060: edouard@4060: return "OK" 44305363+kraskrom@4056: 44305363+kraskrom@4056: Edouard@3707: def pyext_csv_reload(): Edouard@3707: global csv_int_files, csv_str_files Edouard@3707: csv_int_files.clear() Edouard@3707: csv_str_files.clear() Edouard@3707: Edouard@3707: """ andrej@1736: 44305363+kraskrom@4056: Edouard@728: class PythonLibrary(POULibrary): Edouard@728: def GetLibraryPath(self): andrej@1730: return paths.AbsNeighbourFile(__file__, "pous.xml") laurent@366: Edouard@728: def Generate_C(self, buildpath, varlist, IECCFLAGS): andrej@1730: andrej@1680: plc_python_filepath = paths.AbsNeighbourFile(__file__, "plc_python.c") laurent@366: plc_python_file = open(plc_python_filepath, 'r') laurent@366: plc_python_code = plc_python_file.read() laurent@366: plc_python_file.close() laurent@366: python_eval_fb_list = [] Edouard@728: for v in varlist: Edouard@1132: if v["vartype"] == "FB" and v["type"] in ["PYTHON_EVAL", Edouard@1132: "PYTHON_POLL"]: laurent@366: python_eval_fb_list.append(v) laurent@366: python_eval_fb_count = max(1, len(python_eval_fb_list)) andrej@1730: laurent@366: # prepare python code Edouard@1132: plc_python_code = plc_python_code % { andrej@1746: "python_eval_fb_count": python_eval_fb_count} andrej@1730: Edouard@728: Gen_Pythonfile_path = os.path.join(buildpath, "py_ext.c") andrej@1740: pythonfile = open(Gen_Pythonfile_path, 'w') laurent@366: pythonfile.write(plc_python_code) laurent@366: pythonfile.close() andrej@1730: Edouard@3707: runtimefile_path = os.path.join(buildpath, "runtime_00_pyext.py") Edouard@3707: runtimefile = open(runtimefile_path, 'w') Edouard@3707: runtimefile.write(pyext_python_lib_code) Edouard@3707: runtimefile.close() Edouard@3707: return ((["py_ext"], [(Gen_Pythonfile_path, IECCFLAGS)], True), "", Edouard@3707: ("runtime_00_pyext.py", open(runtimefile_path, "rb"))) Edouard@3707: Edouard@728: Edouard@728: class PythonFile(PythonFileCTNMixin): andrej@1730: laurent@781: def GetIconName(self): edouard@4060: return "Pyfile"