CodeFileTreeNode.py
author Edouard Tisserant
Sat, 21 Feb 2015 10:20:24 +0100
changeset 1449 5f09fa31d7b0
parent 1448 20ff66dcc31d
child 1511 91538d0c242c
permissions -rw-r--r--
Added __ext_name__ variable accessible from user python code, reflecting extension name. Fixed contend of OnChange field in PLCGlobalsDesc
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
     1
import os, re, traceback
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
     2
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
     3
from copy import deepcopy
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
     4
from lxml import etree
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
     5
from xmlclass import GenerateParserFromXSDstring
401
8106a853a7c7 Adding support for displaying plugins available variable into Beremiz plugin tree
laurent
parents: 361
diff changeset
     6
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
     7
from PLCControler import UndoBuffer
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
     8
from ConfigTreeNode import XSDSchemaErrorMessage
630
91b2ae63ea3d Including external tools for editing plugin informations into Beremiz window
laurent
parents: 610
diff changeset
     9
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    10
CODEFILE_XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
    11
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
    12
            xmlns:xhtml="http://www.w3.org/1999/xhtml">
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    13
  <xsd:element name="%(codefile_name)s">
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    14
    <xsd:complexType>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    15
      <xsd:sequence>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    16
        %(includes_section)s
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    17
        <xsd:element name="variables">
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    18
          <xsd:complexType>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    19
            <xsd:sequence>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    20
              <xsd:element name="variable" minOccurs="0" maxOccurs="unbounded">
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    21
                <xsd:complexType>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    22
                  <xsd:attribute name="name" type="xsd:string" use="required"/>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    23
                  <xsd:attribute name="type" type="xsd:string" use="required"/>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    24
                  <xsd:attribute name="class" use="optional">
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    25
                    <xsd:simpleType>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    26
                      <xsd:restriction base="xsd:string">
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    27
                        <xsd:enumeration value="input"/>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    28
                        <xsd:enumeration value="memory"/>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    29
                        <xsd:enumeration value="output"/>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    30
                      </xsd:restriction>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    31
                    </xsd:simpleType>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    32
                  </xsd:attribute>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    33
                  <xsd:attribute name="initial" type="xsd:string" use="optional" default=""/>
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: 1332
diff changeset
    34
                  <xsd:attribute name="desc" type="xsd:string" use="optional" default=""/>
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: 1332
diff changeset
    35
                  <xsd:attribute name="onchange" type="xsd:string" use="optional" default=""/>
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: 1332
diff changeset
    36
                  <xsd:attribute name="opts" type="xsd:string" use="optional" default=""/>
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    37
                </xsd:complexType>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    38
              </xsd:element>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    39
            </xsd:sequence>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    40
          </xsd:complexType>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    41
        </xsd:element>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    42
        %(sections)s
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    43
      </xsd:sequence>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    44
    </xsd:complexType>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    45
  </xsd:element>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    46
  <xsd:complexType name="CodeText">
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    47
    <xsd:annotation>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    48
      <xsd:documentation>Formatted text according to parts of XHTML 1.1</xsd:documentation>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    49
    </xsd:annotation>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    50
    <xsd:sequence>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    51
      <xsd:any namespace="http://www.w3.org/1999/xhtml" processContents="lax"/>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    52
    </xsd:sequence>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    53
  </xsd:complexType>
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    54
</xsd:schema>"""
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    55
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    56
SECTION_TAG_ELEMENT = "<xsd:element name=\"%s\" type=\"CodeText\"/>"
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
    57
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
    58
class CodeFile:
656
c1792dfc8c7e Fixing bug integrated plugin editors not closed when removing corresponding plugin
laurent
parents: 651
diff changeset
    59
    
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    60
    CODEFILE_NAME = "CodeFile"
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    61
    SECTIONS_NAMES = []
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    62
    
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    63
    def __init__(self):
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    64
        sections_str = {"codefile_name": self.CODEFILE_NAME}
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    65
        if "includes" in self.SECTIONS_NAMES:
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    66
            sections_str["includes_section"] = SECTION_TAG_ELEMENT % "includes"
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    67
        else:
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    68
            sections_str["includes_section"] = ""
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    69
        sections_str["sections"] = "\n".join(
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    70
            [SECTION_TAG_ELEMENT % name
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    71
             for name in self.SECTIONS_NAMES if name != "includes"])
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    72
        
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
    73
        self.CodeFileParser = GenerateParserFromXSDstring(
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    74
            CODEFILE_XSD % sections_str)
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
    75
        self.CodeFileVariables = etree.XPath("variables/variable")
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
    76
        
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
    77
        filepath = self.CodeFileName()
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
    78
        
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
        if os.path.isfile(filepath):
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
            xmlfile = open(filepath, 'r')
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
    81
            codefile_xml = xmlfile.read()
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
            xmlfile.close()
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
    83
            
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
    84
            codefile_xml = codefile_xml.replace(
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
    85
                '<%s>' % self.CODEFILE_NAME, 
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
    86
                '<%s xmlns:xhtml="http://www.w3.org/1999/xhtml">' % self.CODEFILE_NAME)
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
    87
            for cre, repl in [
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
    88
                (re.compile("(?<!<xhtml:p>)(?:<!\[CDATA\[)"), "<xhtml:p><![CDATA["),
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
    89
                (re.compile("(?:]]>)(?!</xhtml:p>)"), "]]></xhtml:p>")]:
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
    90
                codefile_xml = cre.sub(repl, codefile_xml)
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
    91
            
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
    92
            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
    93
                self.CodeFile, error = self.CodeFileParser.LoadXMLString(codefile_xml)
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
    94
                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
    95
                    self.GetCTRoot().logger.write_warning(
1332
ac7d39f4e376 Fixed error message printed when error in XML files is detected
Laurent Bessard
parents: 1330
diff changeset
    96
                        XSDSchemaErrorMessage % ((self.CODEFILE_NAME,) + error))
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
    97
                self.CreateCodeFileBuffer(True)
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
    98
            except Exception, exc:
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
    99
                self.GetCTRoot().logger.write_error(_("Couldn't load confnode parameters %s :\n %s") % (CTNName, unicode(exc)))
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
   100
                self.GetCTRoot().logger.write_error(traceback.format_exc())
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
   101
        else:
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
   102
            self.CodeFile = self.CodeFileParser.CreateRoot()
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
   103
            self.CreateCodeFileBuffer(False)
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   104
            self.OnCTNSave()
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
   105
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
   106
    def GetBaseTypes(self):
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   107
        return self.GetCTRoot().GetBaseTypes()
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
   108
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
   109
    def GetDataTypes(self, basetypes = False):
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
   110
        return self.GetCTRoot().GetDataTypes(basetypes=basetypes)
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
   111
1146
510d1ea1f6c1 Fixed variable panel in CodeFileEditor
Laurent Bessard
parents: 1124
diff changeset
   112
    def GenerateNewName(self, format, start_idx):
510d1ea1f6c1 Fixed variable panel in CodeFileEditor
Laurent Bessard
parents: 1124
diff changeset
   113
        return self.GetCTRoot().GenerateNewName(
510d1ea1f6c1 Fixed variable panel in CodeFileEditor
Laurent Bessard
parents: 1124
diff changeset
   114
            None, None, format, start_idx,
1153
5bdd82497925 Fixed bug in New variable name generated in CodeFileEditor Variable Panel
Laurent Bessard
parents: 1146
diff changeset
   115
            dict([(var.getname().upper(), True) 
1146
510d1ea1f6c1 Fixed variable panel in CodeFileEditor
Laurent Bessard
parents: 1124
diff changeset
   116
                  for var in self.CodeFile.variables.getvariable()]))
510d1ea1f6c1 Fixed variable panel in CodeFileEditor
Laurent Bessard
parents: 1124
diff changeset
   117
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
   118
    def SetVariables(self, variables):
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
   119
        self.CodeFile.variables.setvariable([])
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
        for var in variables:
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
   121
            variable = self.CodeFileParser.CreateElement("variable", "variables")
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
   122
            variable.setname(var["Name"])
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
   123
            variable.settype(var["Type"])
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
   124
            variable.setinitial(var["Initial"])
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: 1332
diff changeset
   125
            variable.setdesc(var["Description"])
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: 1332
diff changeset
   126
            variable.setonchange(var["OnChange"])
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: 1332
diff changeset
   127
            variable.setopts(var["Options"])
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
   128
            self.CodeFile.variables.appendvariable(variable)
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
   129
    
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
   130
    def GetVariables(self):
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
   131
        datas = []
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
   132
        for var in self.CodeFileVariables(self.CodeFile):
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
   133
            datas.append({"Name" : var.getname(), 
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
   134
                          "Type" : var.gettype(), 
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: 1332
diff changeset
   135
                          "Initial" : var.getinitial(),
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: 1332
diff changeset
   136
                          "Description" : var.getdesc(),
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: 1332
diff changeset
   137
                          "OnChange"    : var.getonchange(),
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: 1332
diff changeset
   138
                          "Options"     : var.getopts(),
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: 1332
diff changeset
   139
                         })
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
   140
        return datas
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
   141
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
   142
    def SetTextParts(self, parts):
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
   143
        for section in self.SECTIONS_NAMES:
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
   144
            section_code = parts.get(section)
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
   145
            if section_code is not None:
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
   146
                getattr(self.CodeFile, section).setanyText(section_code)
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
   147
    
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
   148
    def GetTextParts(self):
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
   149
        return dict([(section, getattr(self.CodeFile, section).getanyText())
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
   150
                     for section in self.SECTIONS_NAMES])
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1100
diff changeset
   151
            
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   152
    def CTNTestModified(self):
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
   153
        return self.ChangesToSave or not self.CodeFileIsSaved()    
630
91b2ae63ea3d Including external tools for editing plugin informations into Beremiz window
laurent
parents: 610
diff changeset
   154
1061
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 953
diff changeset
   155
    def OnCTNSave(self, from_project_path=None):
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
   156
        filepath = self.CodeFileName()
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
   157
        
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
   158
        xmlfile = open(filepath,"w")
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
   159
        xmlfile.write(etree.tostring(
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
   160
            self.CodeFile, 
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
   161
            pretty_print=True, 
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
   162
            xml_declaration=True, 
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
   163
            encoding='utf-8'))
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
   164
        xmlfile.close()
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
   165
        
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
   166
        self.MarkCodeFileAsSaved()
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   167
        return True
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   168
1095
a73fde048749 Move C_ext file for merging with py_ext
Laurent Bessard
parents: 1061
diff changeset
   169
    def CTNGlobalInstances(self):
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: 1332
diff changeset
   170
        variables = self.CodeFileVariables(self.CodeFile)
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: 1332
diff changeset
   171
        ret =  [(variable.getname(),
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
   172
                 variable.gettype(),
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: 1332
diff changeset
   173
                 variable.getinitial()) 
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: 1332
diff changeset
   174
                for variable in variables]
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: 1332
diff changeset
   175
        ret.extend([("On"+variable.getname()+"Change", "python_poll", "")
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: 1332
diff changeset
   176
                for variable in variables
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: 1332
diff changeset
   177
                if variable.getonchange()])
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: 1332
diff changeset
   178
        return ret
656
c1792dfc8c7e Fixing bug integrated plugin editors not closed when removing corresponding plugin
laurent
parents: 651
diff changeset
   179
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
   180
#-------------------------------------------------------------------------------
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
   181
#                      Current Buffering Management Functions
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
   182
#-------------------------------------------------------------------------------
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
   183
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
   184
    """
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
   185
    Return a copy of the codefile model
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
   186
    """
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
   187
    def Copy(self, model):
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
   188
        return deepcopy(model)
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
   189
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
   190
    def CreateCodeFileBuffer(self, saved):
658
94417ab25510 Fixing some issues in c_ext plugin
laurent
parents: 656
diff changeset
   191
        self.Buffering = False
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
   192
        self.CodeFileBuffer = UndoBuffer(self.CodeFileParser.Dumps(self.CodeFile), saved)
651
cbeb769b0a56 Adding support for unifying grid table control elements
laurent
parents: 630
diff changeset
   193
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
   194
    def BufferCodeFile(self):
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
   195
        self.CodeFileBuffer.Buffering(self.CodeFileParser.Dumps(self.CodeFile))
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
   196
    
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
   197
    def StartBuffering(self):
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
   198
        self.Buffering = True
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
   199
        
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
   200
    def EndBuffering(self):
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
   201
        if self.Buffering:
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
   202
            self.CodeFileBuffer.Buffering(self.CodeFileParser.Dumps(self.CodeFile))
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
   203
            self.Buffering = False
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
   204
    
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
   205
    def MarkCodeFileAsSaved(self):
651
cbeb769b0a56 Adding support for unifying grid table control elements
laurent
parents: 630
diff changeset
   206
        self.EndBuffering()
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
   207
        self.CodeFileBuffer.CurrentSaved()
651
cbeb769b0a56 Adding support for unifying grid table control elements
laurent
parents: 630
diff changeset
   208
    
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
   209
    def CodeFileIsSaved(self):
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
   210
        return self.CodeFileBuffer.IsCurrentSaved() and not self.Buffering
658
94417ab25510 Fixing some issues in c_ext plugin
laurent
parents: 656
diff changeset
   211
        
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
   212
    def LoadPrevious(self):
651
cbeb769b0a56 Adding support for unifying grid table control elements
laurent
parents: 630
diff changeset
   213
        self.EndBuffering()
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
   214
        self.CodeFile = self.CodeFileParser.Loads(self.CodeFileBuffer.Previous())
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
   215
    
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
   216
    def LoadNext(self):
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1153
diff changeset
   217
        self.CodeFile = self.CodeFileParser.Loads(self.CodeFileBuffer.Next())
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
   218
    
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
   219
    def GetBufferState(self):
1096
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
   220
        first = self.CodeFileBuffer.IsFirst() and not self.Buffering
c9ace6a881c9 Fixed CFileEditor replacing folding panels by variable panel and STC
Laurent Bessard
parents: 1095
diff changeset
   221
        last = self.CodeFileBuffer.IsLast()
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
   222
        return not first, not last
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
   223