py_ext/py_ext.py
author Edouard Tisserant <edouard@beremiz.fr>
Tue, 11 Feb 2025 15:59:23 +0100 (6 weeks ago)
changeset 4110 2d6446418d0d
parent 4105 79aa1772f491
permissions -rw-r--r--
PY_EXT: fix exception with CSV loading.
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
4105
79aa1772f491 Py_ext: add CSV write by String FB + refactoring
Edouard Tisserant <edouard@beremiz.fr>
parents: 4060
diff changeset
     4
# This file is part of Beremiz IDE
1511
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1132
diff changeset
     5
#
4105
79aa1772f491 Py_ext: add CSV write by String FB + refactoring
Edouard Tisserant <edouard@beremiz.fr>
parents: 4060
diff changeset
     6
# Copyright (C) 2013: 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
     7
# Copyright (C) 2017: Andrey Skvortsov
4105
79aa1772f491 Py_ext: add CSV write by String FB + refactoring
Edouard Tisserant <edouard@beremiz.fr>
parents: 4060
diff changeset
     8
# Copyright (C) 2025: Edouard TISSERANT
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.
1853
47a3f39bead0 fix pylint warning "(relative-import) Relative import 'Y', should be 'X.Y'"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
    11
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    12
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
    13
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
    14
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
    15
import util.paths as paths
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    16
3707
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    17
4105
79aa1772f491 Py_ext: add CSV write by String FB + refactoring
Edouard Tisserant <edouard@beremiz.fr>
parents: 4060
diff changeset
    18
pyext_python_lib_code = open(paths.AbsNeighbourFile(__file__, "py_ext_rt.py"), "r").read()
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    19
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
    20
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    21
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
    22
    def GetLibraryPath(self):
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    23
        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
    24
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    25
    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
    26
1680
6db967480b7d make run Beremiz and PLCOpen Editor, if full path contain non-lating
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    27
        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
    28
        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
    29
        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
    30
        plc_python_file.close()
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    31
        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
    32
        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
    33
            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
    34
                                                      "PYTHON_POLL"]:
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    35
                python_eval_fb_list.append(v)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    36
        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
    37
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    38
        # 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
    39
        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
    40
            "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
    41
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    42
        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
    43
        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
    44
        pythonfile.write(plc_python_code)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    45
        pythonfile.close()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    46
3707
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    47
        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
    48
        runtimefile = open(runtimefile_path, 'w')
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    49
        runtimefile.write(pyext_python_lib_code)
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    50
        runtimefile.close()
3c60c78dfa5d py_ext: add CSV file reading POUs in python extension library
Edouard Tisserant
parents: 1853
diff changeset
    51
        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
    52
                ("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
    53
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    54
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    55
class PythonFile(PythonFileCTNMixin):
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    56
781
cdc6393705ce Adding support using plcopeneditor bitmap library for icon request
laurent
parents: 742
diff changeset
    57
    def GetIconName(self):
4060
d2f5eb3c7d6e py_ext: fix CSV Writer
Edouard Tisserant <edouard@beremiz.fr>
parents: 4056
diff changeset
    58
        return "Pyfile"