py_ext/py_ext.py
author Dino Kosic <44305363+kraskrom@users.noreply.github.com>
Mon, 02 Dec 2024 11:33:19 +0100
changeset 4056 4b2de1a0fbf9
parent 3750 f62625418bff
child 4060 d2f5eb3c7d6e
permissions -rw-r--r--
Extend HMI:JsonTable, create Edit CSV POU, create example, add some doc (#41)

* Extend Jsontable widget, create Edit CSV POU, create example for both

* Add doc for HMI:Image and HMI:JsonTable

* Expand example to include HMI:Image
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]
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    65
    except IndexError:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    66
        return "#ROW_NOT_FOUND"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    67
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    68
    try:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    69
        return row[colidx]
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    70
    except IndexError:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    71
        return "#COL_NOT_FOUND"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    72
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    73
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    74
csv_str_files = {}
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    75
def CSVRdStr(fname, rowname, colname):
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    76
    \"\"\"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    77
    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
    78
    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
    79
    \"\"\"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    80
    global csv_str_files
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    81
    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
    82
    if entry is None:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    83
        data = dict()
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    84
        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
    85
            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
    86
        except IOError:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    87
            return "#FILE_NOT_FOUND"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    88
        try:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    89
            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
    90
            csvfile.seek(0)
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    91
            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
    92
            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
    93
            for row in reader:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    94
                data[row[0]] = row[1:]
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    95
        except csv.Error:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    96
            return "#CSV_ERROR"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    97
        finally:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    98
            csvfile.close()
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    99
        csv_str_files[fname] = (headers, data)
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   100
    else:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   101
        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
   102
3707
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   103
    try:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   104
        row = data[rowname]
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   105
    except KeyError:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   106
        return "#ROW_NOT_FOUND"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   107
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   108
    try:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   109
        colidx = headers[colname]
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   110
    except KeyError:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   111
        return "#COL_NOT_FOUND"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   112
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   113
    try:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   114
        return row[colidx]
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   115
    except IndexError:
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   116
        return "#COL_NOT_FOUND"
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   117
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
   118
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
   119
csv_int_files = {}
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
def CSVWrInt(fname, rowidx, colidx, content, save):
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
   121
    \"\"\"
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
    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
   123
    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
   124
    \"\"\"
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
    if save > 0:
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
   126
        global csv_int_files
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
   127
        data = csv_int_files.get(fname, None)
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
   128
        if data is None:
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
   129
            data = list()
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
   130
            try:
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
   131
                csvfile = open(fname, 'rt', encoding='utf-8')
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
   132
            except IOError:
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
   133
                return "#FILE_NOT_FOUND"
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
   134
            try:
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
   135
                dialect = csv.Sniffer().sniff(csvfile.read(1024))
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
   136
                csvfile.seek(0)
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
   137
                reader = csv.reader(csvfile, dialect)
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
   138
                for row in reader:
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
   139
                    data.append(row)
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
   140
            except csv.Error as e:
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
   141
                return "#CSV_ERROR"
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
   142
            finally:
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
   143
                csvfile.close()
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
   144
            csv_int_files[fname] = 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
   145
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
   146
        try:
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
   147
            row = data[rowidx]
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
   148
        except IndexError:
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
   149
            return "#ROW_NOT_FOUND"
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
   150
    
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
   151
        try:
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
   152
            row[colidx] = content
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
        except IndexError:
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
   154
            return "#COL_NOT_FOUND"
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
   155
            
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
   156
        wfile = open(fname, 'rt+', encoding='utf-8')
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
   157
        wdialect = csv.Sniffer().sniff(wfile.read(1024))
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
   158
        wfile.seek(0)
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
   159
        writer = csv.writer(wfile, wdialect)
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
   160
        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
   161
            writer.writerow(row)
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
   162
        wfile.truncate()
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
        wfile.close()
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
   164
        
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
   165
    return "#SUCCESS"
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
   166
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
   167
3707
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   168
def pyext_csv_reload():
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   169
    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
   170
    csv_int_files.clear()
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   171
    csv_str_files.clear()
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   172
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   173
"""
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   174
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
   175
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
   176
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
   177
    def GetLibraryPath(self):
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
   178
        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
   179
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
   180
    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
   181
1680
6db967480b7d make run Beremiz and PLCOpen Editor, if full path contain non-lating
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
   182
        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
   183
        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
   184
        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
   185
        plc_python_file.close()
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   186
        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
   187
        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
   188
            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
   189
                                                      "PYTHON_POLL"]:
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   190
                python_eval_fb_list.append(v)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   191
        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
   192
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   193
        # 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
   194
        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
   195
            "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
   196
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
   197
        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
   198
        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
   199
        pythonfile.write(plc_python_code)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   200
        pythonfile.close()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
   201
3707
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   202
        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
   203
        runtimefile = open(runtimefile_path, 'w')
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   204
        runtimefile.write(pyext_python_lib_code)
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   205
        runtimefile.close()
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
   206
        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
   207
                ("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
   208
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
   209
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
   210
class PythonFile(PythonFileCTNMixin):
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
   211
781
cdc6393705ce Adding support using plcopeneditor bitmap library for icon request
laurent
parents: 742
diff changeset
   212
    def GetIconName(self):
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
   213
        return "Pyfile"