py_ext/py_ext.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Sun, 08 Dec 2024 15:26:26 +0100
changeset 4065 d876242447da
parent 4060 d2f5eb3c7d6e
permissions -rw-r--r--
IDE: fix exception when changing or resizing comments in FBD
1511
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
     1
#!/usr/bin/env python
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
     2
# -*- coding: utf-8 -*-
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
     3
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
     4
# This file is part of Beremiz, a Integrated Development Environment for
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
     5
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
     6
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
     7
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
1680
6db967480b7d make run Beremiz and PLCOpen Editor, if full path contain non-lating
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
     8
# Copyright (C) 2017: Andrey Skvortsov
1511
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
     9
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
    10
# See COPYING file for copyrights details.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
    11
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
    12
# This program is free software; you can redistribute it and/or
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
    13
# modify it under the terms of the GNU General Public License
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
    14
# as published by the Free Software Foundation; either version 2
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
    15
# of the License, or (at your option) any later version.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
    16
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
    17
# This program is distributed in the hope that it will be useful,
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
    18
# but WITHOUT ANY WARRANTY; without even the implied warranty of
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
    19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
    20
# GNU General Public License for more details.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
    21
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
    22
# You should have received a copy of the GNU General Public License
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
    23
# along with this program; if not, write to the Free Software
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
    24
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
    25
1853
47a3f39bead0 fix pylint warning "(relative-import) Relative import 'Y', should be 'X.Y'"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
    26
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    27
import os
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    28
from POULibrary import POULibrary
1853
47a3f39bead0 fix pylint warning "(relative-import) Relative import 'Y', should be 'X.Y'"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
    29
from py_ext.PythonFileCTNMixin import PythonFileCTNMixin
1680
6db967480b7d make run Beremiz and PLCOpen Editor, if full path contain non-lating
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    30
import util.paths as paths
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    31
3707
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    32
pyext_python_lib_code = """
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    33
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    34
import csv
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    35
from collections import OrderedDict
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    36
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    37
csv_int_files = {}
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    38
def CSVRdInt(fname, rowidx, colidx):
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    39
    \"\"\"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    40
    Return value at row/column pointed by integer indexes
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    41
    Assumes data starts at first row and first column, no headers.
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    42
    \"\"\"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    43
    global csv_int_files
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    44
    data = csv_int_files.get(fname, None)
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    45
    if data is None:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    46
        data = list()
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    47
        try:
4056
4b2de1a0fbf9 Extend HMI:JsonTable, create Edit CSV POU, create example, add some doc (#41)
Dino Kosic <44305363+kraskrom@users.noreply.github.com>
parents: 3750
diff changeset
    48
            csvfile = open(fname, 'rt', encoding='utf-8')
3707
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    49
        except IOError:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    50
            return "#FILE_NOT_FOUND"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    51
        try:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    52
            dialect = csv.Sniffer().sniff(csvfile.read(1024))
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    53
            csvfile.seek(0)
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    54
            reader = csv.reader(csvfile, dialect)
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    55
            for row in reader:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    56
                data.append(row)
4056
4b2de1a0fbf9 Extend HMI:JsonTable, create Edit CSV POU, create example, add some doc (#41)
Dino Kosic <44305363+kraskrom@users.noreply.github.com>
parents: 3750
diff changeset
    57
        except csv.Error as e:
3707
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    58
            return "#CSV_ERROR"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    59
        finally:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    60
            csvfile.close()
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    61
        csv_int_files[fname] = data
4056
4b2de1a0fbf9 Extend HMI:JsonTable, create Edit CSV POU, create example, add some doc (#41)
Dino Kosic <44305363+kraskrom@users.noreply.github.com>
parents: 3750
diff changeset
    62
3707
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    63
    try:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    64
        row = data[rowidx]
4060
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
    65
        if not row and rowidx == len(data)-1:
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
    66
            raise IndexError
3707
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    67
    except IndexError:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    68
        return "#ROW_NOT_FOUND"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    69
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    70
    try:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    71
        return row[colidx]
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    72
    except IndexError:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    73
        return "#COL_NOT_FOUND"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    74
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    75
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    76
csv_str_files = {}
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    77
def CSVRdStr(fname, rowname, colname):
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    78
    \"\"\"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    79
    Return value at row/column pointed by a pair of names as string
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    80
    Assumes first row is column headers and first column is row name.
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    81
    \"\"\"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    82
    global csv_str_files
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    83
    entry = csv_str_files.get(fname, None)
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    84
    if entry is None:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    85
        data = dict()
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    86
        try:
4056
4b2de1a0fbf9 Extend HMI:JsonTable, create Edit CSV POU, create example, add some doc (#41)
Dino Kosic <44305363+kraskrom@users.noreply.github.com>
parents: 3750
diff changeset
    87
            csvfile = open(fname, 'rt', encoding='utf-8')
3707
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    88
        except IOError:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    89
            return "#FILE_NOT_FOUND"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    90
        try:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    91
            dialect = csv.Sniffer().sniff(csvfile.read(1024))
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    92
            csvfile.seek(0)
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    93
            reader = csv.reader(csvfile, dialect)
4056
4b2de1a0fbf9 Extend HMI:JsonTable, create Edit CSV POU, create example, add some doc (#41)
Dino Kosic <44305363+kraskrom@users.noreply.github.com>
parents: 3750
diff changeset
    94
            headers = dict([(name, index) for index, name in enumerate(reader.__next__()[1:])])
3707
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    95
            for row in reader:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    96
                data[row[0]] = row[1:]
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    97
        except csv.Error:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    98
            return "#CSV_ERROR"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    99
        finally:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   100
            csvfile.close()
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   101
        csv_str_files[fname] = (headers, data)
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   102
    else:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   103
        headers, data = entry
4056
4b2de1a0fbf9 Extend HMI:JsonTable, create Edit CSV POU, create example, add some doc (#41)
Dino Kosic <44305363+kraskrom@users.noreply.github.com>
parents: 3750
diff changeset
   104
3707
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   105
    try:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   106
        row = data[rowname]
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   107
    except KeyError:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   108
        return "#ROW_NOT_FOUND"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   109
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   110
    try:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   111
        colidx = headers[colname]
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   112
    except KeyError:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   113
        return "#COL_NOT_FOUND"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   114
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   115
    try:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   116
        return row[colidx]
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   117
    except IndexError:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   118
        return "#COL_NOT_FOUND"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   119
4056
4b2de1a0fbf9 Extend HMI:JsonTable, create Edit CSV POU, create example, add some doc (#41)
Dino Kosic <44305363+kraskrom@users.noreply.github.com>
parents: 3750
diff changeset
   120
4060
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   121
def CSVWrInt(fname, rowidx, colidx, content):
4056
4b2de1a0fbf9 Extend HMI:JsonTable, create Edit CSV POU, create example, add some doc (#41)
Dino Kosic <44305363+kraskrom@users.noreply.github.com>
parents: 3750
diff changeset
   122
    \"\"\"
4b2de1a0fbf9 Extend HMI:JsonTable, create Edit CSV POU, create example, add some doc (#41)
Dino Kosic <44305363+kraskrom@users.noreply.github.com>
parents: 3750
diff changeset
   123
    Update value at row/column pointed by integer indexes
4b2de1a0fbf9 Extend HMI:JsonTable, create Edit CSV POU, create example, add some doc (#41)
Dino Kosic <44305363+kraskrom@users.noreply.github.com>
parents: 3750
diff changeset
   124
    Assumes data starts at first row and first column, no headers.
4b2de1a0fbf9 Extend HMI:JsonTable, create Edit CSV POU, create example, add some doc (#41)
Dino Kosic <44305363+kraskrom@users.noreply.github.com>
parents: 3750
diff changeset
   125
    \"\"\"
4060
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   126
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   127
    global csv_int_files
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   128
    dialect = None
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   129
    data = csv_int_files.get(fname, None)
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   130
    if data is None:
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   131
        data = list()
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   132
        try:
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   133
            csvfile = open(fname, 'rt', encoding='utf-8')
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   134
        except IOError:
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   135
            return "#FILE_NOT_FOUND"
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   136
        try:
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   137
            dialect = csv.Sniffer().sniff(csvfile.read(1024))
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   138
            csvfile.seek(0)
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   139
            reader = csv.reader(csvfile, dialect)
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   140
            for row in reader:
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   141
                data.append(row)
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   142
        except csv.Error as e:
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   143
            return "#CSV_ERROR"
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   144
        finally:
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   145
            csvfile.close()
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   146
        csv_int_files[fname] = data
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   147
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   148
    try:
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   149
        if rowidx == len(data):
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   150
            row = []
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   151
            data.append(row)
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   152
        else:
4056
4b2de1a0fbf9 Extend HMI:JsonTable, create Edit CSV POU, create example, add some doc (#41)
Dino Kosic <44305363+kraskrom@users.noreply.github.com>
parents: 3750
diff changeset
   153
            row = data[rowidx]
4060
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   154
    except IndexError:
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   155
        return "#ROW_NOT_FOUND"
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   156
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   157
    try:
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   158
        if rowidx > 0 and colidx >= len(data[0]):
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   159
            raise IndexError
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   160
        if colidx >= len(row):
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   161
            row.extend([""] * (colidx - len(row)) + [content])
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   162
        else:
4056
4b2de1a0fbf9 Extend HMI:JsonTable, create Edit CSV POU, create example, add some doc (#41)
Dino Kosic <44305363+kraskrom@users.noreply.github.com>
parents: 3750
diff changeset
   163
            row[colidx] = content
4060
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   164
    except IndexError:
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   165
        return "#COL_NOT_FOUND"
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   166
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   167
    try:
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   168
        wfile = open(fname, 'wt')
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   169
        writer = csv.writer(wfile) if not(dialect) else csv.writer(wfile, dialect)
4056
4b2de1a0fbf9 Extend HMI:JsonTable, create Edit CSV POU, create example, add some doc (#41)
Dino Kosic <44305363+kraskrom@users.noreply.github.com>
parents: 3750
diff changeset
   170
        for row in data:
4b2de1a0fbf9 Extend HMI:JsonTable, create Edit CSV POU, create example, add some doc (#41)
Dino Kosic <44305363+kraskrom@users.noreply.github.com>
parents: 3750
diff changeset
   171
            writer.writerow(row)
4060
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   172
    finally:
4056
4b2de1a0fbf9 Extend HMI:JsonTable, create Edit CSV POU, create example, add some doc (#41)
Dino Kosic <44305363+kraskrom@users.noreply.github.com>
parents: 3750
diff changeset
   173
        wfile.close()
4060
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   174
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   175
    return "OK"
4056
4b2de1a0fbf9 Extend HMI:JsonTable, create Edit CSV POU, create example, add some doc (#41)
Dino Kosic <44305363+kraskrom@users.noreply.github.com>
parents: 3750
diff changeset
   176
4b2de1a0fbf9 Extend HMI:JsonTable, create Edit CSV POU, create example, add some doc (#41)
Dino Kosic <44305363+kraskrom@users.noreply.github.com>
parents: 3750
diff changeset
   177
3707
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   178
def pyext_csv_reload():
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   179
    global csv_int_files, csv_str_files
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   180
    csv_int_files.clear()
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   181
    csv_str_files.clear()
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   182
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   183
"""
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   184
4056
4b2de1a0fbf9 Extend HMI:JsonTable, create Edit CSV POU, create example, add some doc (#41)
Dino Kosic <44305363+kraskrom@users.noreply.github.com>
parents: 3750
diff changeset
   185
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
   186
class PythonLibrary(POULibrary):
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
   187
    def GetLibraryPath(self):
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
   188
        return paths.AbsNeighbourFile(__file__, "pous.xml")
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   189
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
   190
    def Generate_C(self, buildpath, varlist, IECCFLAGS):
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
   191
1680
6db967480b7d make run Beremiz and PLCOpen Editor, if full path contain non-lating
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
   192
        plc_python_filepath = paths.AbsNeighbourFile(__file__, "plc_python.c")
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   193
        plc_python_file = open(plc_python_filepath, 'r')
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   194
        plc_python_code = plc_python_file.read()
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   195
        plc_python_file.close()
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   196
        python_eval_fb_list = []
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
   197
        for v in varlist:
1132
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   198
            if v["vartype"] == "FB" and v["type"] in ["PYTHON_EVAL",
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   199
                                                      "PYTHON_POLL"]:
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   200
                python_eval_fb_list.append(v)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   201
        python_eval_fb_count = max(1, len(python_eval_fb_list))
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
   202
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   203
        # prepare python code
1132
28f96aa9c070 Rewrote py_ext and wxglade generators in a clean factored way, added C skeleton for python access to PLC global vars
Edouard Tisserant
parents: 1124
diff changeset
   204
        plc_python_code = plc_python_code % {
1746
45d6f5fba016 clean-up: fix PEP8 E202 whitespace before ')'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   205
            "python_eval_fb_count": python_eval_fb_count}
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
   206
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
   207
        Gen_Pythonfile_path = os.path.join(buildpath, "py_ext.c")
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   208
        pythonfile = open(Gen_Pythonfile_path, 'w')
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   209
        pythonfile.write(plc_python_code)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   210
        pythonfile.close()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
   211
3707
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   212
        runtimefile_path = os.path.join(buildpath, "runtime_00_pyext.py")
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   213
        runtimefile = open(runtimefile_path, 'w')
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   214
        runtimefile.write(pyext_python_lib_code)
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   215
        runtimefile.close()
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   216
        return ((["py_ext"], [(Gen_Pythonfile_path, IECCFLAGS)], True), "",
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   217
                ("runtime_00_pyext.py", open(runtimefile_path, "rb")))
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   218
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
   219
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
   220
class PythonFile(PythonFileCTNMixin):
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
   221
781
cdc6393705ce Adding support using plcopeneditor bitmap library for icon request
laurent
parents: 742
diff changeset
   222
    def GetIconName(self):
4060
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
   223
        return "Pyfile"