py_ext/PythonFileCTNMixin.py
author Edouard Tisserant
Fri, 06 May 2022 11:01:07 +0200
changeset 3467 d8b9ed779728
parent 3096 fc5a0b1ece10
child 3750 f62625418bff
permissions -rw-r--r--
SVGHMI: fix test/projects/svghmi "unspecified" frequency that was actualy specified...
1511
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
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: 1503
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: 1503
diff changeset
     3
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
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: 1503
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: 1503
diff changeset
     6
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
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: 1503
diff changeset
     9
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
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: 1503
diff changeset
    11
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
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: 1503
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: 1503
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: 1503
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: 1503
diff changeset
    16
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
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: 1503
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: 1503
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: 1503
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: 1503
diff changeset
    21
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
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: 1503
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: 1503
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: 1503
diff changeset
    25
1853
47a3f39bead0 fix pylint warning "(relative-import) Relative import 'Y', should be 'X.Y'"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
    26
47a3f39bead0 fix pylint warning "(relative-import) Relative import 'Y', should be 'X.Y'"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
    27
from __future__ import absolute_import
1732
94ffe74e6895 clean-up: fix PEP8 E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    28
import os
94ffe74e6895 clean-up: fix PEP8 E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    29
import re
2434
07f48018b6f5 python3 support: pylint, W1612 # (unicode-builtin) unicode built-in referenced
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2418
diff changeset
    30
from builtins import str as text
07f48018b6f5 python3 support: pylint, W1612 # (unicode-builtin) unicode built-in referenced
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2418
diff changeset
    31
1680
6db967480b7d make run Beremiz and PLCOpen Editor, if full path contain non-lating
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    32
import util.paths as paths
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    33
from xmlclass import GenerateParserFromXSD
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    34
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    35
from CodeFileTreeNode import CodeFile
1853
47a3f39bead0 fix pylint warning "(relative-import) Relative import 'Y', should be 'X.Y'"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
    36
from py_ext.PythonEditor import PythonEditor
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    37
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
    38
2693
7ab2b5a18e65 Allow customization of generated py_ext OnChange calls, by adding a static method to PythonFileCTNMixin that can the be later hot patched.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2692
diff changeset
    39
7ab2b5a18e65 Allow customization of generated py_ext OnChange calls, by adding a static method to PythonFileCTNMixin that can the be later hot patched.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2692
diff changeset
    40
1097
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    41
class PythonFileCTNMixin(CodeFile):
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    42
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    43
    CODEFILE_NAME = "PyFile"
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    44
    SECTIONS_NAMES = [
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    45
        "globals",
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    46
        "init",
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    47
        "cleanup",
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    48
        "start",
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    49
        "stop"]
657
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    50
    EditorType = PythonEditor
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    51
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    52
    def __init__(self):
1097
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    53
        CodeFile.__init__(self)
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    54
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    55
        filepath = self.PythonFileName()
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    56
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    57
        if os.path.isfile(filepath):
1680
6db967480b7d make run Beremiz and PLCOpen Editor, if full path contain non-lating
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    58
            PythonParser = GenerateParserFromXSD(paths.AbsNeighbourFile(__file__, "py_ext_xsd.xsd"))
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    59
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    60
            xmlfile = open(filepath, 'r')
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    61
            pythonfile_xml = xmlfile.read()
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    62
            xmlfile.close()
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    63
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    64
            pythonfile_xml = pythonfile_xml.replace(
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    65
                'xmlns="http://www.w3.org/2001/XMLSchema"',
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    66
                'xmlns:xhtml="http://www.w3.org/1999/xhtml"')
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    67
            for cre, repl in [
2439
f0a040f1de1b Fix pep8 warning: W605 invalid escape sequence ?x?
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2434
diff changeset
    68
                    (re.compile(r"(?<!<xhtml:p>)(?:<!\[CDATA\[)"), "<xhtml:p><![CDATA["),
f0a040f1de1b Fix pep8 warning: W605 invalid escape sequence ?x?
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2434
diff changeset
    69
                    (re.compile(r"(?:]]>)(?!</xhtml:p>)"), "]]></xhtml:p>")]:
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    70
                pythonfile_xml = cre.sub(repl, pythonfile_xml)
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    71
1330
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1315
diff changeset
    72
            try:
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1315
diff changeset
    73
                python_code, error = PythonParser.LoadXMLString(pythonfile_xml)
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    74
                if error is None:
1330
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1315
diff changeset
    75
                    self.CodeFile.globals.setanyText(python_code.getanyText())
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1315
diff changeset
    76
                    os.remove(filepath)
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1315
diff changeset
    77
                    self.CreateCodeFileBuffer(False)
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1315
diff changeset
    78
                    self.OnCTNSave()
2418
5587c490a070 Use python 3 compatible exception syntax everywhere
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1902
diff changeset
    79
            except Exception as exc:
2434
07f48018b6f5 python3 support: pylint, W1612 # (unicode-builtin) unicode built-in referenced
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2418
diff changeset
    80
                error = text(exc)
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    81
1330
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1315
diff changeset
    82
            if error is not None:
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1315
diff changeset
    83
                self.GetCTRoot().logger.write_error(
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    84
                    _("Couldn't import old %s file.") % self.CTNName())
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    85
1097
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    86
    def CodeFileName(self):
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    87
        return os.path.join(self.CTNPath(), "pyfile.xml")
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    88
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    89
    def PythonFileName(self):
721
ecf4d203c4d4 refactoring
Edouard Tisserant
parents: 718
diff changeset
    90
        return os.path.join(self.CTNPath(), "py_ext.xml")
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    91
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
    92
    PreSectionsTexts = {}
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
    93
    PostSectionsTexts = {}
1751
c28db6f7616b clean-up: fix PEP8 E301 expected 1 blank line, found 0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1748
diff changeset
    94
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    95
    def GetSection(self, section):
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    96
        return self.PreSectionsTexts.get(section, "") + "\n" + \
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1160
diff changeset
    97
               getattr(self.CodeFile, section).getanyText() + "\n" + \
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    98
               self.PostSectionsTexts.get(section, "")
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
    99
2692
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   100
    def CTNGlobalInstances(self):
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   101
        variables = self.CodeFileVariables(self.CodeFile)
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   102
        ret = [(variable.getname(),
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   103
                variable.gettype(),
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   104
                variable.getinitial())
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   105
               for variable in variables]
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   106
        location_str = "_".join(map(str, self.GetCurrentLocation()))
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   107
        ret.append(("On_"+location_str+"_Change", "python_poll", ""))
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   108
        return ret
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   109
2693
7ab2b5a18e65 Allow customization of generated py_ext OnChange calls, by adding a static method to PythonFileCTNMixin that can the be later hot patched.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2692
diff changeset
   110
    @staticmethod
7ab2b5a18e65 Allow customization of generated py_ext OnChange calls, by adding a static method to PythonFileCTNMixin that can the be later hot patched.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2692
diff changeset
   111
    def GetVarOnChangeContent(var):
7ab2b5a18e65 Allow customization of generated py_ext OnChange calls, by adding a static method to PythonFileCTNMixin that can the be later hot patched.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2692
diff changeset
   112
        """
7ab2b5a18e65 Allow customization of generated py_ext OnChange calls, by adding a static method to PythonFileCTNMixin that can the be later hot patched.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2692
diff changeset
   113
        returns given variable onchange field
7ab2b5a18e65 Allow customization of generated py_ext OnChange calls, by adding a static method to PythonFileCTNMixin that can the be later hot patched.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2692
diff changeset
   114
        function is meant to allow customization 
7ab2b5a18e65 Allow customization of generated py_ext OnChange calls, by adding a static method to PythonFileCTNMixin that can the be later hot patched.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2692
diff changeset
   115
        """
7ab2b5a18e65 Allow customization of generated py_ext OnChange calls, by adding a static method to PythonFileCTNMixin that can the be later hot patched.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2692
diff changeset
   116
        return var.getonchange()
7ab2b5a18e65 Allow customization of generated py_ext OnChange calls, by adding a static method to PythonFileCTNMixin that can the be later hot patched.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2692
diff changeset
   117
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
   118
    def CTNGenerate_C(self, buildpath, locations):
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   119
        # location string for that CTN
1833
2269739dd098 fix unnecessary lambda and enable corresponding pylint check
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1777
diff changeset
   120
        location_str = "_".join(map(str, self.GetCurrentLocation()))
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   121
        configname = self.GetCTRoot().GetProjectConfigNames()[0]
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   122
1878
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1853
diff changeset
   123
        def _onchangecode(var):
2694
3225990eb33e py_ext: better handling of coma separated OnChange attribute for python safe global variables.
Edouard Tisserant
parents: 2693
diff changeset
   124
            result = []
3225990eb33e py_ext: better handling of coma separated OnChange attribute for python safe global variables.
Edouard Tisserant
parents: 2693
diff changeset
   125
            for onchangecall in self.GetVarOnChangeContent(var).split(','):
3225990eb33e py_ext: better handling of coma separated OnChange attribute for python safe global variables.
Edouard Tisserant
parents: 2693
diff changeset
   126
                onchangecall = onchangecall.strip()
3225990eb33e py_ext: better handling of coma separated OnChange attribute for python safe global variables.
Edouard Tisserant
parents: 2693
diff changeset
   127
                if onchangecall:
3225990eb33e py_ext: better handling of coma separated OnChange attribute for python safe global variables.
Edouard Tisserant
parents: 2693
diff changeset
   128
                    result.append(onchangecall + "('" + var.getname() + "')")
3225990eb33e py_ext: better handling of coma separated OnChange attribute for python safe global variables.
Edouard Tisserant
parents: 2693
diff changeset
   129
            return result
3225990eb33e py_ext: better handling of coma separated OnChange attribute for python safe global variables.
Edouard Tisserant
parents: 2693
diff changeset
   130
1878
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1853
diff changeset
   131
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1853
diff changeset
   132
        def _onchange(var):
2693
7ab2b5a18e65 Allow customization of generated py_ext OnChange calls, by adding a static method to PythonFileCTNMixin that can the be later hot patched.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2692
diff changeset
   133
            content = self.GetVarOnChangeContent(var)
7ab2b5a18e65 Allow customization of generated py_ext OnChange calls, by adding a static method to PythonFileCTNMixin that can the be later hot patched.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2692
diff changeset
   134
            return repr(content) if content else None
1878
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1853
diff changeset
   135
1447
d6b878525ceb Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
Edouard Tisserant
parents: 1436
diff changeset
   136
        pyextname = self.CTNName()
1878
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1853
diff changeset
   137
        varinfos = map(
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1853
diff changeset
   138
            lambda variable: {
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1853
diff changeset
   139
                "name": variable.getname(),
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1853
diff changeset
   140
                "desc": repr(variable.getdesc()),
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1853
diff changeset
   141
                "onchangecode": _onchangecode(variable),
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1853
diff changeset
   142
                "onchange": _onchange(variable),
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1853
diff changeset
   143
                "opts": repr(variable.getopts()),
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1853
diff changeset
   144
                "configname": configname.upper(),
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1853
diff changeset
   145
                "uppername": variable.getname().upper(),
2681
abc2088aac4e Py_ext: use non-derived type when generating C code for handling Python PLC globals.
Edouard Tisserant
parents: 2679
diff changeset
   146
                "IECtype": self.GetCTRoot().GetBaseType(variable.gettype()),
1901
e8cf68d69447 Enforced check_source.sh pep8 and pylint rules.
Edouard Tisserant
parents: 1897
diff changeset
   147
                "initial": repr(variable.getinitial()),
1878
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1853
diff changeset
   148
                "pyextname": pyextname
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1853
diff changeset
   149
            },
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1853
diff changeset
   150
            self.CodeFile.variables.variable)
2692
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   151
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   152
        onchange_var_count = len([None for varinfo in varinfos if varinfo["onchange"]])
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   153
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   154
        # python side PLC global variables access stub
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1766
diff changeset
   155
        globalstubs = "\n".join([
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1766
diff changeset
   156
            """\
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   157
_%(name)s_ctype, _%(name)s_unpack, _%(name)s_pack = \\
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   158
    TypeTranslator["%(IECtype)s"]
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   159
_PySafeGetPLCGlob_%(name)s = PLCBinary.__SafeGetPLCGlob_%(name)s
1145
203f4eff3313 Fixed PLC global var access from python. Added test in tests/python
Edouard Tisserant
parents: 1144
diff changeset
   160
_PySafeGetPLCGlob_%(name)s.restype = None
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   161
_PySafeGetPLCGlob_%(name)s.argtypes = [ctypes.POINTER(_%(name)s_ctype)]
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   162
_PySafeSetPLCGlob_%(name)s = PLCBinary.__SafeSetPLCGlob_%(name)s
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   163
_PySafeSetPLCGlob_%(name)s.restype = None
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   164
_PySafeSetPLCGlob_%(name)s.argtypes = [ctypes.POINTER(_%(name)s_ctype)]
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   165
_%(pyextname)sGlobalsDesc.append((
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   166
    "%(name)s",
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   167
    "%(IECtype)s",
1897
62f9cae9a25f Extend python<->PLC global variable description "GlobalDesc" to support Initial value setting.
Edouard Tisserant
parents: 1878
diff changeset
   168
    %(initial)s,
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   169
    %(desc)s,
1449
5f09fa31d7b0 Added __ext_name__ variable accessible from user python code, reflecting extension name. Fixed contend of OnChange field in PLCGlobalsDesc
Edouard Tisserant
parents: 1448
diff changeset
   170
    %(onchange)s,
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   171
    %(opts)s))
2697
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   172
""" % varinfo + ("""
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   173
_PyOnChangeCount_%(name)s = ctypes.c_uint.in_dll(PLCBinary,"__%(name)s_onchange_count")
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   174
_PyOnChangeFirst_%(name)s = _%(name)s_ctype.in_dll(PLCBinary,"__%(name)s_onchange_firstval")
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   175
_PyOnChangeLast_%(name)s = _%(name)s_ctype.in_dll(PLCBinary,"__%(name)s_onchange_lastval")
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   176
""" % varinfo if varinfo["onchange"] else "") for varinfo in varinfos])
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
   177
2692
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   178
        on_change_func_body = "\n".join(["""
2697
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   179
    if _PyOnChangeCount_%(name)s.value > 0:
2692
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   180
        # %(name)s
2694
3225990eb33e py_ext: better handling of coma separated OnChange attribute for python safe global variables.
Edouard Tisserant
parents: 2693
diff changeset
   181
        try:""" % varinfo + """
2692
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   182
            """ + """
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   183
            """.join(varinfo['onchangecode'])+"""
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   184
        except Exception as e:
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   185
            errors.append("%(name)s: "+str(e))
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   186
""" % varinfo for varinfo in varinfos if varinfo["onchange"]])
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
   187
        # Runtime calls (start, stop, init, and cleanup)
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
        rtcalls = ""
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
   189
        for section in self.SECTIONS_NAMES:
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
   190
            if section != "globals":
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
   191
                rtcalls += "def _runtime_%s_%s():\n" % (location_str, section)
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
   192
                sectiontext = self.GetSection(section).strip()
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
   193
                if sectiontext:
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
                    rtcalls += '    ' + \
1154
da9ccfceff31 Fixed build error with python <-> PLCglobals variables declared with no initial value
Edouard Tisserant
parents: 1148
diff changeset
   195
                        sectiontext.replace('\n', '\n    ')+"\n\n"
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
   196
                else:
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
   197
                    rtcalls += "    pass\n\n"
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   198
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   199
        globalsection = self.GetSection("globals")
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   200
1846
14b40afccd69 remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1833
diff changeset
   201
        loc_dict = {
14b40afccd69 remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1833
diff changeset
   202
            "pyextname": pyextname,
14b40afccd69 remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1833
diff changeset
   203
            "globalstubs": globalstubs,
14b40afccd69 remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1833
diff changeset
   204
            "globalsection": globalsection,
14b40afccd69 remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1833
diff changeset
   205
            "rtcalls": rtcalls,
2692
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   206
            "location_str": location_str,
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   207
            "on_change_func_body":on_change_func_body,
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   208
            "onchange_var_count": onchange_var_count
1846
14b40afccd69 remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1833
diff changeset
   209
        }
14b40afccd69 remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1833
diff changeset
   210
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   211
        PyFileContent = """\
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
   212
#!/usr/bin/env python
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
   213
# -*- coding: utf-8 -*-
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
   214
## Code generated by Beremiz python mixin confnode
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   215
##
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   216
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
   217
## Code for PLC global variable access
1902
2b7e2db31d81 Clarify licensing, and packaging of runtime only files :
Edouard Tisserant
parents: 1901
diff changeset
   218
from runtime.typemapping import TypeTranslator
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   219
import ctypes
2692
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   220
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   221
_PySafeGetChanges_%(pyextname)s = PLCBinary.PySafeGetChanges_%(location_str)s
2697
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   222
_PySafeGetChanges_%(pyextname)s.restype = None
2692
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   223
_PySafeGetChanges_%(pyextname)s.argtypes = None
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   224
1447
d6b878525ceb Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
Edouard Tisserant
parents: 1436
diff changeset
   225
_%(pyextname)sGlobalsDesc = []
1449
5f09fa31d7b0 Added __ext_name__ variable accessible from user python code, reflecting extension name. Fixed contend of OnChange field in PLCGlobalsDesc
Edouard Tisserant
parents: 1448
diff changeset
   226
__ext_name__ = "%(pyextname)s"
1452
7a2b344de8cf Fixed build of tests/python.
Edouard Tisserant
parents: 1449
diff changeset
   227
PLCGlobalsDesc.append(( "%(pyextname)s" , _%(pyextname)sGlobalsDesc ))
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   228
%(globalstubs)s
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   229
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
   230
## User code in "global" scope
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   231
%(globalsection)s
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   232
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
   233
## Beremiz python runtime calls
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   234
%(rtcalls)s
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   235
2692
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   236
def On_%(pyextname)s_Change():
2697
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   237
    _PySafeGetChanges_%(pyextname)s()
2692
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   238
    errors = []
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   239
%(on_change_func_body)s
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   240
    if len(errors)>0 :
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   241
        raise Exception("Exception in %(pyextname)s OnChange call:\\\\n" + "\\\\n".join(errors))
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   242
1449
5f09fa31d7b0 Added __ext_name__ variable accessible from user python code, reflecting extension name. Fixed contend of OnChange field in PLCGlobalsDesc
Edouard Tisserant
parents: 1448
diff changeset
   243
del __ext_name__
5f09fa31d7b0 Added __ext_name__ variable accessible from user python code, reflecting extension name. Fixed contend of OnChange field in PLCGlobalsDesc
Edouard Tisserant
parents: 1448
diff changeset
   244
1846
14b40afccd69 remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1833
diff changeset
   245
""" % loc_dict
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   246
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   247
        # write generated content to python file
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   248
        runtimefile_path = os.path.join(buildpath,
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1766
diff changeset
   249
                                        "runtime_%s.py" % location_str)
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
   250
        runtimefile = open(runtimefile_path, 'w')
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   251
        runtimefile.write(PyFileContent.encode('utf-8'))
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
   252
        runtimefile.close()
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
   253
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   254
        # C code for safe global variables access
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   255
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   256
        vardecfmt = """\
1154
da9ccfceff31 Fixed build error with python <-> PLCglobals variables declared with no initial value
Edouard Tisserant
parents: 1148
diff changeset
   257
extern  __IEC_%(IECtype)s_t %(configname)s__%(uppername)s;
da9ccfceff31 Fixed build error with python <-> PLCglobals variables declared with no initial value
Edouard Tisserant
parents: 1148
diff changeset
   258
IEC_%(IECtype)s __%(name)s_rbuffer = __INIT_%(IECtype)s;
da9ccfceff31 Fixed build error with python <-> PLCglobals variables declared with no initial value
Edouard Tisserant
parents: 1148
diff changeset
   259
IEC_%(IECtype)s __%(name)s_wbuffer;
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   260
long __%(name)s_rlock = 0;
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   261
long __%(name)s_wlock = 0;
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   262
int __%(name)s_wbuffer_written = 0;
1154
da9ccfceff31 Fixed build error with python <-> PLCglobals variables declared with no initial value
Edouard Tisserant
parents: 1148
diff changeset
   263
void __SafeGetPLCGlob_%(name)s(IEC_%(IECtype)s *pvalue){
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   264
    while(AtomicCompareExchange(&__%(name)s_rlock, 0, 1));
1145
203f4eff3313 Fixed PLC global var access from python. Added test in tests/python
Edouard Tisserant
parents: 1144
diff changeset
   265
    *pvalue = __%(name)s_rbuffer;
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   266
    AtomicCompareExchange((long*)&__%(name)s_rlock, 1, 0);
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   267
}
1160
e9e9d3193894 Fixed GCC warnings when building stub code for python PLC globals access
Edouard Tisserant
parents: 1154
diff changeset
   268
void __SafeSetPLCGlob_%(name)s(IEC_%(IECtype)s *value){
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   269
    while(AtomicCompareExchange(&__%(name)s_wlock, 0, 1));
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   270
    __%(name)s_wbuffer = *value;
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   271
    __%(name)s_wbuffer_written = 1;
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   272
    AtomicCompareExchange((long*)&__%(name)s_wlock, 1, 0);
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   273
}
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   274
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   275
"""
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   276
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   277
        vardeconchangefmt = """\
2697
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   278
unsigned int __%(name)s_rbuffer_written = 0;
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   279
IEC_%(IECtype)s __%(name)s_rbuffer_firstval;
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   280
IEC_%(IECtype)s __%(name)s_rbuffer_lastval;
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   281
unsigned int __%(name)s_onchange_count = 0;
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   282
IEC_%(IECtype)s __%(name)s_onchange_firstval;
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   283
IEC_%(IECtype)s __%(name)s_onchange_lastval;
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   284
"""
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   285
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   286
        varretfmt = """\
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   287
    if(!AtomicCompareExchange(&__%(name)s_wlock, 0, 1)){
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   288
        if(__%(name)s_wbuffer_written == 1){
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   289
            %(configname)s__%(uppername)s.value = __%(name)s_wbuffer;
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   290
            __%(name)s_wbuffer_written = 0;
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   291
        }
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   292
        AtomicCompareExchange((long*)&__%(name)s_wlock, 1, 0);
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   293
    }
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   294
"""
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   295
        varpubfmt = """\
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   296
    if(!AtomicCompareExchange(&__%(name)s_rlock, 0, 1)){
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   297
        __%(name)s_rbuffer = __GET_VAR(%(configname)s__%(uppername)s);
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   298
        AtomicCompareExchange((long*)&__%(name)s_rlock, 1, 0);
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   299
    }
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   300
"""
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   301
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   302
        varpubonchangefmt = """\
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   303
    if(!AtomicCompareExchange(&__%(name)s_rlock, 0, 1)){
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   304
        IEC_%(IECtype)s tmp = __GET_VAR(%(configname)s__%(uppername)s);
2679
2783906d5792 Runtime: STRING type for Python PLC Globals was making problem with strings, since generated code wasn't doing comparison properly.
Edouard Tisserant
parents: 2442
diff changeset
   305
        if(NE_%(IECtype)s(1, NULL, __%(name)s_rbuffer, tmp)){
2697
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   306
            if(__%(name)s_rbuffer_written == 0);
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   307
                __%(name)s_rbuffer_firstval = __%(name)s_rbuffer;
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   308
            __%(name)s_rbuffer_lastval = tmp;
2679
2783906d5792 Runtime: STRING type for Python PLC Globals was making problem with strings, since generated code wasn't doing comparison properly.
Edouard Tisserant
parents: 2442
diff changeset
   309
            __%(name)s_rbuffer = tmp;
2697
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   310
            /* count one more change */
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   311
            __%(name)s_rbuffer_written += 1;
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   312
            some_change_found = 1;
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   313
        }
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   314
        AtomicCompareExchange((long*)&__%(name)s_rlock, 1, 0);
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   315
    }
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   316
"""
2692
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   317
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   318
        varcollectchangefmt = """\
2697
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   319
    while(AtomicCompareExchange(&__%(name)s_rlock, 0, 1));
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   320
    __%(name)s_onchange_count = __%(name)s_rbuffer_written;
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   321
    __%(name)s_onchange_firstval = __%(name)s_rbuffer_firstval;
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   322
    __%(name)s_onchange_lastval = __%(name)s_rbuffer_lastval;
2692
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   323
    /* mark variable as unchanged */
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   324
    __%(name)s_rbuffer_written = 0;
2697
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   325
    AtomicCompareExchange((long*)&__%(name)s_rlock, 1, 0);
2692
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   326
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   327
"""
1452
7a2b344de8cf Fixed build of tests/python.
Edouard Tisserant
parents: 1449
diff changeset
   328
        vardec = "\n".join([(vardecfmt + vardeconchangefmt
1742
92932cd370a4 clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   329
                             if varinfo["onchange"] else vardecfmt) % varinfo
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   330
                            for varinfo in varinfos])
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   331
        varret = "\n".join([varretfmt % varinfo for varinfo in varinfos])
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   332
        varpub = "\n".join([(varpubonchangefmt if varinfo["onchange"] else
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   333
                             varpubfmt) % varinfo
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   334
                            for varinfo in varinfos])
2692
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   335
        varcollectchange = "\n".join([varcollectchangefmt % varinfo
1878
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1853
diff changeset
   336
                             for varinfo in varinfos if varinfo["onchange"]])
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   337
2692
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   338
        pysafe_pypoll_code = "On_"+pyextname+"_Change()"
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   339
1846
14b40afccd69 remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1833
diff changeset
   340
        loc_dict = {
14b40afccd69 remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1833
diff changeset
   341
            "vardec": vardec,
14b40afccd69 remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1833
diff changeset
   342
            "varret": varret,
14b40afccd69 remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1833
diff changeset
   343
            "varpub": varpub,
14b40afccd69 remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1833
diff changeset
   344
            "location_str": location_str,
2692
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   345
            "pysafe_pypoll_code": '"'+pysafe_pypoll_code+'"',
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   346
            "pysafe_pypoll_code_len": len(pysafe_pypoll_code),
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   347
            "varcollectchange": varcollectchange,
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   348
            "onchange_var_count": onchange_var_count
1846
14b40afccd69 remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1833
diff changeset
   349
        }
14b40afccd69 remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1833
diff changeset
   350
1452
7a2b344de8cf Fixed build of tests/python.
Edouard Tisserant
parents: 1449
diff changeset
   351
        # TODO : use config name obtained from model instead of default
7a2b344de8cf Fixed build of tests/python.
Edouard Tisserant
parents: 1449
diff changeset
   352
        # "config.h". User cannot change config name, but project imported
7a2b344de8cf Fixed build of tests/python.
Edouard Tisserant
parents: 1449
diff changeset
   353
        # or created in older beremiz vesion could use different name.
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   354
        PyCFileContent = """\
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   355
/*
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   356
 * Code generated by Beremiz py_ext confnode
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
   357
 * for safe global variables access
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
   358
 */
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
   359
#include "iec_types_all.h"
1448
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   360
#include "POUS.h"
20ff66dcc31d Added more columns in variable list of extensions' code editors - updating python's PLCGlobals from PLC logic can trigger asynchronous 'OnChange' python call
Edouard Tisserant
parents: 1447
diff changeset
   361
#include "config.h"
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   362
#include "beremiz.h"
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   363
2692
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   364
PYTHON_POLL* __%(location_str)s_notifier;
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   365
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   366
/* User variables reference */
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   367
%(vardec)s
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   368
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   369
/* Beremiz confnode functions */
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   370
int __init_%(location_str)s(int argc,char **argv){
2692
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   371
    __%(location_str)s_notifier = __GET_GLOBAL_ON_%(location_str)s_CHANGE();
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   372
    __SET_VAR(__%(location_str)s_notifier->,TRIG,,__BOOL_LITERAL(TRUE));
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   373
    __SET_VAR(__%(location_str)s_notifier->,CODE,,__STRING_LITERAL(%(pysafe_pypoll_code_len)d,%(pysafe_pypoll_code)s));
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   374
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   375
    return 0;
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   376
}
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   377
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   378
void __cleanup_%(location_str)s(void){
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   379
}
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   380
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   381
void __retrieve_%(location_str)s(void){
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   382
%(varret)s
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   383
}
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   384
2697
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   385
static int passing_changes_to_python = 0;
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   386
void __publish_%(location_str)s(void){
2697
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   387
    int some_change_found = 0;
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   388
%(varpub)s
2697
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   389
    passing_changes_to_python |= some_change_found;
2692
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   390
    // call python part if there was at least a change
2697
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   391
    if(passing_changes_to_python){
2692
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   392
        PYTHON_POLL_body__(__%(location_str)s_notifier);
2697
93333d206198 Python Safe Globals now have more reliable triggering of OnChange call. Added "Onchange" object to accessible runtime variables that let user python code see count of changes and first and last values.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2694
diff changeset
   393
        passing_changes_to_python &= !(__GET_VAR(__%(location_str)s_notifier->ACK,));
2692
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   394
    }
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   395
}
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   396
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   397
void* PySafeGetChanges_%(location_str)s(void){
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   398
%(varcollectchange)s
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   399
}
d4bede6cd3f1 PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2681
diff changeset
   400
1846
14b40afccd69 remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1833
diff changeset
   401
""" % loc_dict
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   402
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1732
diff changeset
   403
        Gen_PyCfile_path = os.path.join(buildpath, "PyCFile_%s.c" % location_str)
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   404
        pycfile = open(Gen_PyCfile_path, 'w')
1144
21475ee0e688 Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents: 1132
diff changeset
   405
        pycfile.write(PyCFileContent)
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
   406
        pycfile.close()
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   407
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1732
diff changeset
   408
        matiec_CFLAGS = '"-I%s"' % os.path.abspath(
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
   409
            self.GetCTRoot().GetIECLibPath())
1436
e15ca67197b9 py_ext user python code can now know about global variables shared with PLC. updated tests/python accordingly
Edouard Tisserant
parents: 1330
diff changeset
   410
1503
3a238c0c5993 fix regression introduced by changeset: 1431:28e9d479aa65 [Use '-p' command line option when running matiec (iec2c)]
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1475
diff changeset
   411
        return ([(Gen_PyCfile_path, matiec_CFLAGS)],
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
   412
                "",
1145
203f4eff3313 Fixed PLC global var access from python. Added test in tests/python
Edouard Tisserant
parents: 1144
diff changeset
   413
                True,
2442
b13f021c68a5 python3 support: pylint, W1607 # (file-builtin) file built-in referenced
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2439
diff changeset
   414
                ("runtime_%s.py" % location_str, open(runtimefile_path, "rb")))