c_ext/c_ext.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Thu, 10 Dec 2020 11:37:27 +0100
changeset 2697 93333d206198
parent 1853 47a3f39bead0
child 3750 f62625418bff
permissions -rw-r--r--
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.
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
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
     8
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
     9
# 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
    10
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
    11
# 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
    12
# 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
    13
# 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
    14
# 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
    15
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
    16
# 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
    17
# 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
    18
# 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
    19
# 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
    20
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1503
diff changeset
    21
# 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
    22
# 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
    23
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1119
diff changeset
    24
1853
47a3f39bead0 fix pylint warning "(relative-import) Relative import 'Y', should be 'X.Y'"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
    25
from __future__ import absolute_import
145
94855f7b08a9 Improving c_ext plugin by adding an XML file format for saving C files and an graphical interface for editing this file
lbessard
parents: 137
diff changeset
    26
import os
401
8106a853a7c7 Adding support for displaying plugins available variable into Beremiz plugin tree
laurent
parents: 361
diff changeset
    27
1853
47a3f39bead0 fix pylint warning "(relative-import) Relative import 'Y', should be 'X.Y'"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
    28
from c_ext.CFileEditor import CFileEditor
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1061
diff changeset
    29
from CodeFileTreeNode import CodeFile
630
91b2ae63ea3d Including external tools for editing plugin informations into Beremiz window
laurent
parents: 610
diff changeset
    30
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1735
diff changeset
    31
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1061
diff changeset
    32
class CFile(CodeFile):
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    33
    XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    34
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
145
94855f7b08a9 Improving c_ext plugin by adding an XML file format for saving C files and an graphical interface for editing this file
lbessard
parents: 137
diff changeset
    35
      <xsd:element name="CExtension">
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    36
        <xsd:complexType>
45
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    37
          <xsd:attribute name="CFLAGS" type="xsd:string" use="required"/>
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    38
          <xsd:attribute name="LDFLAGS" type="xsd:string" use="required"/>
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    39
        </xsd:complexType>
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    40
      </xsd:element>
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    41
    </xsd:schema>
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    42
    """
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1119
diff changeset
    43
    CODEFILE_NAME = "CFile"
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1119
diff changeset
    44
    SECTIONS_NAMES = [
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1119
diff changeset
    45
        "includes",
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1119
diff changeset
    46
        "globals",
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1119
diff changeset
    47
        "initFunction",
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1119
diff changeset
    48
        "cleanUpFunction",
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1119
diff changeset
    49
        "retrieveFunction",
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1119
diff changeset
    50
        "publishFunction"]
656
c1792dfc8c7e Fixing bug integrated plugin editors not closed when removing corresponding plugin
laurent
parents: 651
diff changeset
    51
    EditorType = CFileEditor
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
    52
781
cdc6393705ce Adding support using plcopeneditor bitmap library for icon request
laurent
parents: 742
diff changeset
    53
    def GetIconName(self):
cdc6393705ce Adding support using plcopeneditor bitmap library for icon request
laurent
parents: 742
diff changeset
    54
        return "Cfile"
738
413946c04c87 refactoring
laurent
parents: 734
diff changeset
    55
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1061
diff changeset
    56
    def CodeFileName(self):
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    57
        return os.path.join(self.CTNPath(), "cfile.xml")
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
    58
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    59
    def CTNGenerate_C(self, buildpath, locations):
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    60
        """
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    61
        Generate C code
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 675
diff changeset
    62
        @param current_location: Tupple containing confnode IEC location : %I0.0.4.5 => (0,0,4,5)
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    63
        @param locations: List of complete variables locations \
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    64
            [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...)
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    65
            "NAME" : name of the variable (generally "__IW0_1_2" style)
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    66
            "DIR" : direction "Q","I" or "M"
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    67
            "SIZE" : size "X", "B", "W", "D", "L"
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    68
            "LOC" : tuple of interger for IEC location (0,1,2,...)
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    69
            }, ...]
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    70
        @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    71
        """
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    72
        current_location = self.GetCurrentLocation()
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    73
        # define a unique name for the generated C file
401
8106a853a7c7 Adding support for displaying plugins available variable into Beremiz plugin tree
laurent
parents: 361
diff changeset
    74
        location_str = "_".join(map(str, current_location))
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
    75
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 675
diff changeset
    76
        text = "/* Code generated by Beremiz c_ext confnode */\n\n"
1114
1a97222ffee9 Fixed bug in generated code in c_ext file
Laurent Bessard
parents: 1097
diff changeset
    77
        text += "#include <stdio.h>\n\n"
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
    78
145
94855f7b08a9 Improving c_ext plugin by adding an XML file format for saving C files and an graphical interface for editing this file
lbessard
parents: 137
diff changeset
    79
        # Adding includes
94855f7b08a9 Improving c_ext plugin by adding an XML file format for saving C files and an graphical interface for editing this file
lbessard
parents: 137
diff changeset
    80
        text += "/* User includes */\n"
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1124
diff changeset
    81
        text += self.CodeFile.includes.getanyText().strip()
145
94855f7b08a9 Improving c_ext plugin by adding an XML file format for saving C files and an graphical interface for editing this file
lbessard
parents: 137
diff changeset
    82
        text += "\n"
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
    83
1114
1a97222ffee9 Fixed bug in generated code in c_ext file
Laurent Bessard
parents: 1097
diff changeset
    84
        text += '#include "iec_types_all.h"\n\n'
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
    85
145
94855f7b08a9 Improving c_ext plugin by adding an XML file format for saving C files and an graphical interface for editing this file
lbessard
parents: 137
diff changeset
    86
        # Adding variables
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1061
diff changeset
    87
        config = self.GetCTRoot().GetProjectConfigNames()[0]
145
94855f7b08a9 Improving c_ext plugin by adding an XML file format for saving C files and an graphical interface for editing this file
lbessard
parents: 137
diff changeset
    88
        text += "/* User variables reference */\n"
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1061
diff changeset
    89
        for variable in self.CodeFile.variables.variable:
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1061
diff changeset
    90
            var_infos = {
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1061
diff changeset
    91
                "name": variable.getname(),
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1061
diff changeset
    92
                "global": "%s__%s" % (config.upper(),
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1061
diff changeset
    93
                                      variable.getname().upper()),
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1061
diff changeset
    94
                "type": "__IEC_%s_t" % variable.gettype()}
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1061
diff changeset
    95
            text += "extern %(type)s %(global)s;\n" % var_infos
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1061
diff changeset
    96
            text += "#define %(name)s %(global)s.value\n" % var_infos
145
94855f7b08a9 Improving c_ext plugin by adding an XML file format for saving C files and an graphical interface for editing this file
lbessard
parents: 137
diff changeset
    97
        text += "\n"
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
    98
145
94855f7b08a9 Improving c_ext plugin by adding an XML file format for saving C files and an graphical interface for editing this file
lbessard
parents: 137
diff changeset
    99
        # Adding user global variables and routines
94855f7b08a9 Improving c_ext plugin by adding an XML file format for saving C files and an graphical interface for editing this file
lbessard
parents: 137
diff changeset
   100
        text += "/* User internal user variables and routines */\n"
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1124
diff changeset
   101
        text += self.CodeFile.globals.getanyText().strip()
1114
1a97222ffee9 Fixed bug in generated code in c_ext file
Laurent Bessard
parents: 1097
diff changeset
   102
        text += "\n"
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
   103
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 675
diff changeset
   104
        # Adding Beremiz confnode functions
1c23952dbde1 refactoring
Edouard Tisserant
parents: 675
diff changeset
   105
        text += "/* Beremiz confnode functions */\n"
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
   106
        text += "int __init_%s(int argc,char **argv)\n{\n" % location_str
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1124
diff changeset
   107
        text += self.CodeFile.initFunction.getanyText().strip()
1119
34db4294c177 Fixed generated code in c_ext file
Laurent Bessard
parents: 1114
diff changeset
   108
        text += "  return 0;\n}\n\n"
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
   109
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
   110
        text += "void __cleanup_%s(void)\n{\n" % location_str
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1124
diff changeset
   111
        text += self.CodeFile.cleanUpFunction.getanyText().strip()
145
94855f7b08a9 Improving c_ext plugin by adding an XML file format for saving C files and an graphical interface for editing this file
lbessard
parents: 137
diff changeset
   112
        text += "\n}\n\n"
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
   113
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
   114
        text += "void __retrieve_%s(void)\n{\n" % location_str
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1124
diff changeset
   115
        text += self.CodeFile.retrieveFunction.getanyText().strip()
145
94855f7b08a9 Improving c_ext plugin by adding an XML file format for saving C files and an graphical interface for editing this file
lbessard
parents: 137
diff changeset
   116
        text += "\n}\n\n"
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
   117
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
   118
        text += "void __publish_%s(void)\n{\n" % location_str
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1124
diff changeset
   119
        text += self.CodeFile.publishFunction.getanyText().strip()
145
94855f7b08a9 Improving c_ext plugin by adding an XML file format for saving C files and an graphical interface for editing this file
lbessard
parents: 137
diff changeset
   120
        text += "\n}\n\n"
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
   121
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
   122
        Gen_Cfile_path = os.path.join(buildpath, "CFile_%s.c" % location_str)
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   123
        cfile = open(Gen_Cfile_path, 'w')
145
94855f7b08a9 Improving c_ext plugin by adding an XML file format for saving C files and an graphical interface for editing this file
lbessard
parents: 137
diff changeset
   124
        cfile.write(text)
94855f7b08a9 Improving c_ext plugin by adding an XML file format for saving C files and an graphical interface for editing this file
lbessard
parents: 137
diff changeset
   125
        cfile.close()
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
   126
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
   127
        matiec_CFLAGS = '"-I%s"' % os.path.abspath(self.GetCTRoot().GetIECLibPath())
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
   128
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   129
        return [(Gen_Cfile_path, str(self.CExtension.getCFLAGS() + matiec_CFLAGS))], str(self.CExtension.getLDFLAGS()), True