ConfigTreeNode.py
author Edouard Tisserant
Sat, 19 May 2012 12:41:48 +0200
changeset 731 4fc681ed0c61
parent 728 e0424e96e3fd
child 734 5c42cafaee15
permissions -rw-r--r--
refecored library extension machanism
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
     1
"""
725
31dade089db5 refactoring
Edouard Tisserant
parents: 722
diff changeset
     2
Config Tree Node base class.
31dade089db5 refactoring
Edouard Tisserant
parents: 722
diff changeset
     3
31dade089db5 refactoring
Edouard Tisserant
parents: 722
diff changeset
     4
- A Beremiz project is organized in a tree each node derivate from ConfigTreeNode
31dade089db5 refactoring
Edouard Tisserant
parents: 722
diff changeset
     5
- Project tree organization match filesystem organization of project directory.
31dade089db5 refactoring
Edouard Tisserant
parents: 722
diff changeset
     6
- Each node of the tree have its own xml configuration, whose grammar is defined for each node type, as XSD
31dade089db5 refactoring
Edouard Tisserant
parents: 722
diff changeset
     7
- ... TODO : document
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
     8
"""
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
     9
725
31dade089db5 refactoring
Edouard Tisserant
parents: 722
diff changeset
    10
import os,traceback,types
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    11
import shutil
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    12
from xml.dom import minidom
126
034979cb02b0 add use of docutils for pdf
greg
parents: 125
diff changeset
    13
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    14
from xmlclass import GenerateClassesFromXSDstring
725
31dade089db5 refactoring
Edouard Tisserant
parents: 722
diff changeset
    15
from util import opjimg, GetClassImporter
31dade089db5 refactoring
Edouard Tisserant
parents: 722
diff changeset
    16
31dade089db5 refactoring
Edouard Tisserant
parents: 722
diff changeset
    17
from PLCControler import PLCControler, LOCATION_CONFNODE
274
8628f3dd0979 Adding support for defining plugin library as a plcopen xml file in plugin folder
greg
parents: 273
diff changeset
    18
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    19
_BaseParamsClass = GenerateClassesFromXSDstring("""<?xml version="1.0" encoding="ISO-8859-1" ?>
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    20
        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    21
          <xsd:element name="BaseParams">
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    22
            <xsd:complexType>
86
f0a9d74e3b26 Adding support for the new version of xmlclass
lbessard
parents: 82
diff changeset
    23
              <xsd:attribute name="Name" type="xsd:string" use="optional" default="__unnamed__"/>
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
    24
              <xsd:attribute name="IEC_Channel" type="xsd:integer" use="required"/>
86
f0a9d74e3b26 Adding support for the new version of xmlclass
lbessard
parents: 82
diff changeset
    25
              <xsd:attribute name="Enabled" type="xsd:boolean" use="optional" default="true"/>
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    26
            </xsd:complexType>
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    27
          </xsd:element>
86
f0a9d74e3b26 Adding support for the new version of xmlclass
lbessard
parents: 82
diff changeset
    28
        </xsd:schema>""")["BaseParams"]
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    29
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    30
NameTypeSeparator = '@'
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    31
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
    32
class ConfigTreeNode:
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    33
    """
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
    34
    This class is the one that define confnodes.
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    35
    """
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    36
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    37
    XSD = None
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    38
    CTNChildrenTypes = []
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    39
    CTNMaxCount = None
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
    40
    ConfNodeMethods = []
274
8628f3dd0979 Adding support for defining plugin library as a plcopen xml file in plugin folder
greg
parents: 273
diff changeset
    41
    LibraryControler = None
656
c1792dfc8c7e Fixing bug integrated plugin editors not closed when removing corresponding plugin
laurent
parents: 655
diff changeset
    42
    EditorType = None
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    43
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    44
    def _AddParamsMembers(self):
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    45
        self.CTNParams = None
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
    46
        if self.XSD:
411
8261c8f1e365 Bug on Debug trying to start (and stop) before PLC started fixed.
laurent
parents: 403
diff changeset
    47
            self.Classes = GenerateClassesFromXSDstring(self.XSD)
8261c8f1e365 Bug on Debug trying to start (and stop) before PLC started fixed.
laurent
parents: 403
diff changeset
    48
            Classes = [(name, XSDclass) for name, XSDclass in self.Classes.items() if XSDclass.IsBaseClass]
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
    49
            if len(Classes) == 1:
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
    50
                name, XSDclass = Classes[0]
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
    51
                obj = XSDclass()
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    52
                self.CTNParams = (name, obj)
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
    53
                setattr(self, name, obj)
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
    54
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
    55
    def __init__(self):
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    56
        # Create BaseParam 
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    57
        self.BaseParams = _BaseParamsClass()
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
    58
        self.MandatoryParams = ("BaseParams", self.BaseParams)
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
    59
        self._AddParamsMembers()
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    60
        self.Children = {}
656
c1792dfc8c7e Fixing bug integrated plugin editors not closed when removing corresponding plugin
laurent
parents: 655
diff changeset
    61
        self._View = None
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
    62
        # copy ConfNodeMethods so that it can be later customized
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
    63
        self.ConfNodeMethods = [dic.copy() for dic in self.ConfNodeMethods]
325
f2604900bf25 Bug preventing loading STLibrary when adding a plugin fixed
lbessard
parents: 321
diff changeset
    64
        
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    65
    def ConfNodeBaseXmlFilePath(self, CTNName=None):
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    66
        return os.path.join(self.CTNPath(CTNName), "baseconfnode.xml")
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    67
    
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    68
    def ConfNodeXmlFilePath(self, CTNName=None):
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    69
        return os.path.join(self.CTNPath(CTNName), "confnode.xml")
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
    70
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
    71
    def ConfNodePath(self):
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    72
        return os.path.join(self.CTNParent.ConfNodePath(), self.CTNType)
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    73
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    74
    def CTNPath(self,CTNName=None):
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    75
        if not CTNName:
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    76
            CTNName = self.CTNName()
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    77
        return os.path.join(self.CTNParent.CTNPath(),
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    78
                            CTNName + NameTypeSeparator + self.CTNType)
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    79
    
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    80
    def CTNName(self):
675
44b35c27e9ff Adding support for quickly open recent projects in file menu
laurent
parents: 674
diff changeset
    81
        return self.BaseParams.getName()
44b35c27e9ff Adding support for quickly open recent projects in file menu
laurent
parents: 674
diff changeset
    82
    
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    83
    def CTNEnabled(self):
675
44b35c27e9ff Adding support for quickly open recent projects in file menu
laurent
parents: 674
diff changeset
    84
        return self.BaseParams.getEnabled()
44b35c27e9ff Adding support for quickly open recent projects in file menu
laurent
parents: 674
diff changeset
    85
    
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    86
    def CTNFullName(self):
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    87
        parent = self.CTNParent.CTNFullName()
656
c1792dfc8c7e Fixing bug integrated plugin editors not closed when removing corresponding plugin
laurent
parents: 655
diff changeset
    88
        if parent != "":
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    89
            return parent + "." + self.CTNName()
656
c1792dfc8c7e Fixing bug integrated plugin editors not closed when removing corresponding plugin
laurent
parents: 655
diff changeset
    90
        return self.BaseParams.getName()
c1792dfc8c7e Fixing bug integrated plugin editors not closed when removing corresponding plugin
laurent
parents: 655
diff changeset
    91
    
652
eb2d9f2b3567 Adding support for loading specific POUs library in LPCBeremiz
laurent
parents: 639
diff changeset
    92
    def GetIconPath(self, name):
eb2d9f2b3567 Adding support for loading specific POUs library in LPCBeremiz
laurent
parents: 639
diff changeset
    93
        return opjimg(name)
eb2d9f2b3567 Adding support for loading specific POUs library in LPCBeremiz
laurent
parents: 639
diff changeset
    94
    
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    95
    def CTNTestModified(self):
118
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
    96
        return self.ChangesToSave
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
    97
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
    98
    def ProjectTestModified(self):
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
    99
        """
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   100
        recursively check modified status
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   101
        """
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   102
        if self.CTNTestModified():
118
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   103
            return True
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   104
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   105
        for CTNChild in self.IterChildren():
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   106
            if CTNChild.ProjectTestModified():
118
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   107
                return True
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   108
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   109
        return False
699
6ff64cadb1ff Adding support for executing python scripts on remote runtime
laurent
parents: 696
diff changeset
   110
    
6ff64cadb1ff Adding support for executing python scripts on remote runtime
laurent
parents: 696
diff changeset
   111
    def RemoteExec(self, script, **kwargs):
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   112
        return self.CTNParent.RemoteExec(script, **kwargs)
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   113
    
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   114
    def OnCTNSave(self):
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   115
        #Default, do nothing and return success
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   116
        return True
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   117
19
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   118
    def GetParamsAttributes(self, path = None):
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   119
        if path:
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   120
            parts = path.split(".", 1)
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   121
            if self.MandatoryParams and parts[0] == self.MandatoryParams[0]:
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   122
                return self.MandatoryParams[1].getElementInfos(parts[0], parts[1])
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   123
            elif self.CTNParams and parts[0] == self.CTNParams[0]:
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   124
                return self.CTNParams[1].getElementInfos(parts[0], parts[1])
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   125
        else:
19
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   126
            params = []
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   127
            if self.CTNParams:
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   128
                params.append(self.CTNParams[1].getElementInfos(self.CTNParams[0]))
19
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   129
            return params
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   130
        
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   131
    def SetParamsAttribute(self, path, value):
118
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   132
        self.ChangesToSave = True
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   133
        # Filter IEC_Channel and Name, that have specific behavior
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   134
        if path == "BaseParams.IEC_Channel":
443
34c9788bd933 Adding support for updating located variables when changing plugin IEC_Channel
laurent
parents: 440
diff changeset
   135
            old_leading = ".".join(map(str, self.GetCurrentLocation()))
34c9788bd933 Adding support for updating located variables when changing plugin IEC_Channel
laurent
parents: 440
diff changeset
   136
            new_value = self.FindNewIEC_Channel(value)
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   137
            new_leading = ".".join(map(str, self.CTNParent.GetCurrentLocation() + (new_value,)))
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   138
            self.GetCTRoot().UpdateProjectVariableLocation(old_leading, new_leading)
443
34c9788bd933 Adding support for updating located variables when changing plugin IEC_Channel
laurent
parents: 440
diff changeset
   139
            return new_value, True
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   140
        elif path == "BaseParams.Name":
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   141
            res = self.FindNewName(value)
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   142
            self.CTNRequestSave()
118
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   143
            return res, True
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   144
        
19
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   145
        parts = path.split(".", 1)
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   146
        if self.MandatoryParams and parts[0] == self.MandatoryParams[0]:
73257cea38bd Adding Plugin params visualization with basic controls
lbessard
parents: 18
diff changeset
   147
            self.MandatoryParams[1].setElementValue(parts[1], value)
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   148
        elif self.CTNParams and parts[0] == self.CTNParams[0]:
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   149
            self.CTNParams[1].setElementValue(parts[1], value)
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   150
        return value, False
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   151
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   152
    def CTNMakeDir(self):
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   153
        os.mkdir(self.CTNPath())
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   154
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   155
    def CTNRequestSave(self):
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   156
        if self.GetCTRoot().CheckProjectPathPerm(False):
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   157
            # If confnode do not have corresponding directory
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   158
            ctnpath = self.CTNPath()
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   159
            if not os.path.isdir(ctnpath):
427
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 423
diff changeset
   160
                # Create it
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   161
                os.mkdir(ctnpath)
427
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 423
diff changeset
   162
    
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   163
            # generate XML for base XML parameters controller of the confnode
427
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 423
diff changeset
   164
            if self.MandatoryParams:
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   165
                BaseXMLFile = open(self.ConfNodeBaseXmlFilePath(),'w')
427
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 423
diff changeset
   166
                BaseXMLFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
430
5981ad8547f5 Allowing unicode characters to be used in comments
laurent
parents: 428
diff changeset
   167
                BaseXMLFile.write(self.MandatoryParams[1].generateXMLText(self.MandatoryParams[0], 0).encode("utf-8"))
427
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 423
diff changeset
   168
                BaseXMLFile.close()
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 423
diff changeset
   169
            
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   170
            # generate XML for XML parameters controller of the confnode
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   171
            if self.CTNParams:
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   172
                XMLFile = open(self.ConfNodeXmlFilePath(),'w')
427
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 423
diff changeset
   173
                XMLFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   174
                XMLFile.write(self.CTNParams[1].generateXMLText(self.CTNParams[0], 0).encode("utf-8"))
427
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 423
diff changeset
   175
                XMLFile.close()
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 423
diff changeset
   176
            
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   177
            # Call the confnode specific OnCTNSave method
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   178
            result = self.OnCTNSave()
427
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 423
diff changeset
   179
            if not result:
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   180
                return _("Error while saving \"%s\"\n")%self.CTNPath()
427
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 423
diff changeset
   181
    
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   182
            # mark confnode as saved
427
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 423
diff changeset
   183
            self.ChangesToSave = False
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   184
            # go through all children and do the same
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   185
            for CTNChild in self.IterChildren():
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   186
                result = CTNChild.CTNRequestSave()
427
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 423
diff changeset
   187
                if result:
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 423
diff changeset
   188
                    return result
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   189
        return None
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   190
    
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   191
    def CTNImport(self, src_CTNPath):
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   192
        shutil.copytree(src_CTNPath, self.CTNPath)
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   193
        return True
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   194
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   195
    def CTNGenerate_C(self, buildpath, locations):
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   196
        """
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   197
        Generate C code
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   198
        @param locations: List of complete variables locations \
22
9a0c535c3272 Pleliminary build process -- C code generation
etisserant
parents: 20
diff changeset
   199
            [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...)
9a0c535c3272 Pleliminary build process -- C code generation
etisserant
parents: 20
diff changeset
   200
            "NAME" : name of the variable (generally "__IW0_1_2" style)
9a0c535c3272 Pleliminary build process -- C code generation
etisserant
parents: 20
diff changeset
   201
            "DIR" : direction "Q","I" or "M"
9a0c535c3272 Pleliminary build process -- C code generation
etisserant
parents: 20
diff changeset
   202
            "SIZE" : size "X", "B", "W", "D", "L"
9a0c535c3272 Pleliminary build process -- C code generation
etisserant
parents: 20
diff changeset
   203
            "LOC" : tuple of interger for IEC location (0,1,2,...)
9a0c535c3272 Pleliminary build process -- C code generation
etisserant
parents: 20
diff changeset
   204
            }, ...]
18
0fac6d621a24 Base build mechanism layout.
etisserant
parents: 17
diff changeset
   205
        @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
0fac6d621a24 Base build mechanism layout.
etisserant
parents: 17
diff changeset
   206
        """
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   207
        self.GetCTRoot().logger.write_warning(".".join(map(lambda x:str(x), self.GetCurrentLocation())) + " -> Nothing to do\n")
51
c31c55601556 Added project linking, and plugin init,cleanup,retrive and publish method calls in main
etisserant
parents: 49
diff changeset
   208
        return [],"",False
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   209
    
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   210
    def _Generate_C(self, buildpath, locations):
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   211
        # Generate confnodes [(Cfiles, CFLAGS)], LDFLAGS, DoCalls, extra_files
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   212
        # extra_files = [(fname,fobject), ...]
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   213
        gen_result = self.CTNGenerate_C(buildpath, locations)
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   214
        CTNCFilesAndCFLAGS, CTNLDFLAGS, DoCalls = gen_result[:3]
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   215
        extra_files = gen_result[3:]
361
331d698e1118 Adding support for internationalization
laurent
parents: 356
diff changeset
   216
        # if some files have been generated put them in the list with their location
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   217
        if CTNCFilesAndCFLAGS:
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   218
            LocationCFilesAndCFLAGS = [(self.GetCurrentLocation(), CTNCFilesAndCFLAGS, DoCalls)]
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   219
        else:
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   220
            LocationCFilesAndCFLAGS = []
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   221
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   222
        # confnode asks for some LDFLAGS
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   223
        if CTNLDFLAGS:
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   224
            # LDFLAGS can be either string
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   225
            if type(CTNLDFLAGS)==type(str()):
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   226
                LDFLAGS=[CTNLDFLAGS]
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   227
            #or list of strings
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   228
            elif type(CTNLDFLAGS)==type(list()):
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   229
                LDFLAGS=CTNLDFLAGS[:]
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   230
        else:
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   231
            LDFLAGS=[]
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   232
        
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   233
        # recurse through all children, and stack their results
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   234
        for CTNChild in self.IECSortedChildren():
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   235
            new_location = CTNChild.GetCurrentLocation()
24
585d5b387b6a Working CanOpen OD generation
etisserant
parents: 23
diff changeset
   236
            # How deep are we in the tree ?
585d5b387b6a Working CanOpen OD generation
etisserant
parents: 23
diff changeset
   237
            depth=len(new_location)
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   238
            _LocationCFilesAndCFLAGS, _LDFLAGS, _extra_files = \
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   239
                CTNChild._Generate_C(
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   240
                    #keep the same path
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   241
                    buildpath,
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   242
                    # filter locations that start with current IEC location
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   243
                    [loc for loc in locations if loc["LOC"][0:depth] == new_location ])
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   244
            # stack the result
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   245
            LocationCFilesAndCFLAGS += _LocationCFilesAndCFLAGS
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   246
            LDFLAGS += _LDFLAGS
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   247
            extra_files += _extra_files
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   248
        
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   249
        return LocationCFilesAndCFLAGS, LDFLAGS, extra_files
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   250
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   251
    def IterChildren(self):
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   252
        for CTNType, Children in self.Children.items():
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   253
            for CTNInstance in Children:
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   254
                yield CTNInstance
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   255
    
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   256
    def IECSortedChildren(self):
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   257
        # reorder children by IEC_channels
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   258
        ordered = [(chld.BaseParams.getIEC_Channel(),chld) for chld in self.IterChildren()]
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   259
        if ordered:
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   260
            ordered.sort()
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   261
            return zip(*ordered)[1]
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   262
        else:
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   263
            return []
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   264
    
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   265
    def _GetChildBySomething(self, something, toks):
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   266
        for CTNInstance in self.IterChildren():
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   267
            # if match component of the name
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   268
            if getattr(CTNInstance.BaseParams, something) == toks[0]:
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   269
                # if Name have other components
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   270
                if len(toks) >= 2:
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   271
                    # Recurse in order to find the latest object
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   272
                    return CTNInstance._GetChildBySomething( something, toks[1:])
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   273
                # No sub name -> found
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   274
                return CTNInstance
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   275
        # Not found
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   276
        return None
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   277
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   278
    def GetChildByName(self, Name):
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   279
        if Name:
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   280
            toks = Name.split('.')
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   281
            return self._GetChildBySomething("Name", toks)
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   282
        else:
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   283
            return self
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   284
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   285
    def GetChildByIECLocation(self, Location):
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   286
        if Location:
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   287
            return self._GetChildBySomething("IEC_Channel", Location)
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   288
        else:
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   289
            return self
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   290
    
23
e007d9d466d7 minor fixes
etisserant
parents: 22
diff changeset
   291
    def GetCurrentLocation(self):
24
585d5b387b6a Working CanOpen OD generation
etisserant
parents: 23
diff changeset
   292
        """
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   293
        @return:  Tupple containing confnode IEC location of current confnode : %I0.0.4.5 => (0,0,4,5)
24
585d5b387b6a Working CanOpen OD generation
etisserant
parents: 23
diff changeset
   294
        """
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   295
        return self.CTNParent.GetCurrentLocation() + (self.BaseParams.getIEC_Channel(),)
23
e007d9d466d7 minor fixes
etisserant
parents: 22
diff changeset
   296
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   297
    def GetCurrentName(self):
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   298
        """
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   299
        @return:  String "ParentParentName.ParentName.Name"
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   300
        """
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   301
        return  self.CTNParent._GetCurrentName() + self.BaseParams.getName()
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   302
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   303
    def _GetCurrentName(self):
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   304
        """
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   305
        @return:  String "ParentParentName.ParentName.Name."
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 41
diff changeset
   306
        """
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   307
        return  self.CTNParent._GetCurrentName() + self.BaseParams.getName() + "."
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   308
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   309
    def GetCTRoot(self):
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   310
        return self.CTNParent.GetCTRoot()
23
e007d9d466d7 minor fixes
etisserant
parents: 22
diff changeset
   311
97
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   312
    def GetFullIEC_Channel(self):
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   313
        return ".".join([str(i) for i in self.GetCurrentLocation()]) + ".x"
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   314
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   315
    def GetLocations(self):
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   316
        location = self.GetCurrentLocation()
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   317
        return [loc for loc in self.CTNParent.GetLocations() if loc["LOC"][0:len(location)] == location]
97
9c6fdf60ad2e Beremiz layout changed
lbessard
parents: 96
diff changeset
   318
401
8106a853a7c7 Adding support for displaying plugins available variable into Beremiz plugin tree
laurent
parents: 396
diff changeset
   319
    def GetVariableLocationTree(self):
8106a853a7c7 Adding support for displaying plugins available variable into Beremiz plugin tree
laurent
parents: 396
diff changeset
   320
        '''
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   321
        This function is meant to be overridden by confnodes.
401
8106a853a7c7 Adding support for displaying plugins available variable into Beremiz plugin tree
laurent
parents: 396
diff changeset
   322
8106a853a7c7 Adding support for displaying plugins available variable into Beremiz plugin tree
laurent
parents: 396
diff changeset
   323
        It should returns an list of dictionaries
8106a853a7c7 Adding support for displaying plugins available variable into Beremiz plugin tree
laurent
parents: 396
diff changeset
   324
        
8106a853a7c7 Adding support for displaying plugins available variable into Beremiz plugin tree
laurent
parents: 396
diff changeset
   325
        - IEC_type is an IEC type like BOOL/BYTE/SINT/...
8106a853a7c7 Adding support for displaying plugins available variable into Beremiz plugin tree
laurent
parents: 396
diff changeset
   326
        - location is a string of this variable's location, like "%IX0.0.0"
8106a853a7c7 Adding support for displaying plugins available variable into Beremiz plugin tree
laurent
parents: 396
diff changeset
   327
        '''
8106a853a7c7 Adding support for displaying plugins available variable into Beremiz plugin tree
laurent
parents: 396
diff changeset
   328
        children = []
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   329
        for child in self.IECSortedChildren():
402
984e238e63d0 Bugs on displaying plugin available variables in PluginTree fixed
laurent
parents: 401
diff changeset
   330
            children.append(child.GetVariableLocationTree())
984e238e63d0 Bugs on displaying plugin available variables in PluginTree fixed
laurent
parents: 401
diff changeset
   331
        return {"name": self.BaseParams.getName(),
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   332
                "type": LOCATION_CONFNODE,
402
984e238e63d0 Bugs on displaying plugin available variables in PluginTree fixed
laurent
parents: 401
diff changeset
   333
                "location": self.GetFullIEC_Channel(),
984e238e63d0 Bugs on displaying plugin available variables in PluginTree fixed
laurent
parents: 401
diff changeset
   334
                "children": children}
401
8106a853a7c7 Adding support for displaying plugins available variable into Beremiz plugin tree
laurent
parents: 396
diff changeset
   335
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   336
    def FindNewName(self, DesiredName):
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   337
        """
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   338
        Changes Name to DesiredName if available, Name-N if not.
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   339
        @param DesiredName: The desired Name (string)
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   340
        """
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   341
        # Get Current Name
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   342
        CurrentName = self.BaseParams.getName()
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   343
        # Do nothing if no change
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   344
        #if CurrentName == DesiredName: return CurrentName
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   345
        # Build a list of used Name out of parent's Children
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   346
        AllNames=[]
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   347
        for CTNInstance in self.CTNParent.IterChildren():
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   348
            if CTNInstance != self:
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   349
                AllNames.append(CTNInstance.BaseParams.getName())
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   350
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   351
        # Find a free name, eventually appending digit
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   352
        res = DesiredName
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   353
        suffix = 1
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   354
        while res in AllNames:
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   355
            res = "%s-%d"%(DesiredName, suffix)
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   356
            suffix += 1
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   357
        
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   358
        # Get old path
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   359
        oldname = self.CTNPath()
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   360
        # Check previous confnode existance
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   361
        dontexist = self.BaseParams.getName() == "__unnamed__"
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   362
        # Set the new name
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   363
        self.BaseParams.setName(res)
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   364
        # Rename confnode dir if exist
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   365
        if not dontexist:
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   366
            shutil.move(oldname, self.CTNPath())
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   367
        # warn user he has two left hands
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   368
        if DesiredName != res:
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   369
            self.GetCTRoot().logger.write_warning(_("A child names \"%s\" already exist -> \"%s\"\n")%(DesiredName,res))
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   370
        return res
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   371
683
57aa9da845d5 Fix wrong panel size making strange background in topology plugin element
laurent
parents: 677
diff changeset
   372
    def GetAllChannels(self):
57aa9da845d5 Fix wrong panel size making strange background in topology plugin element
laurent
parents: 677
diff changeset
   373
        AllChannels=[]
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   374
        for CTNInstance in self.CTNParent.IterChildren():
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   375
            if CTNInstance != self:
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   376
                AllChannels.append(CTNInstance.BaseParams.getIEC_Channel())
683
57aa9da845d5 Fix wrong panel size making strange background in topology plugin element
laurent
parents: 677
diff changeset
   377
        AllChannels.sort()
57aa9da845d5 Fix wrong panel size making strange background in topology plugin element
laurent
parents: 677
diff changeset
   378
        return AllChannels
57aa9da845d5 Fix wrong panel size making strange background in topology plugin element
laurent
parents: 677
diff changeset
   379
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   380
    def FindNewIEC_Channel(self, DesiredChannel):
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   381
        """
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   382
        Changes IEC Channel number to DesiredChannel if available, nearest available if not.
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   383
        @param DesiredChannel: The desired IEC channel (int)
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   384
        """
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   385
        # Get Current IEC channel
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   386
        CurrentChannel = self.BaseParams.getIEC_Channel()
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   387
        # Do nothing if no change
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   388
        #if CurrentChannel == DesiredChannel: return CurrentChannel
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   389
        # Build a list of used Channels out of parent's Children
683
57aa9da845d5 Fix wrong panel size making strange background in topology plugin element
laurent
parents: 677
diff changeset
   390
        AllChannels = self.GetAllChannels()
57aa9da845d5 Fix wrong panel size making strange background in topology plugin element
laurent
parents: 677
diff changeset
   391
        
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   392
        # Now, try to guess the nearest available channel
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   393
        res = DesiredChannel
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   394
        while res in AllChannels: # While channel not free
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   395
            if res < CurrentChannel: # Want to go down ?
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   396
                res -=  1 # Test for n-1
33
59b84ab7bf8b Enhanced bahavior of plugin tree representation when changing IEC channel
etisserant
parents: 29
diff changeset
   397
                if res < 0 :
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   398
                    self.GetCTRoot().logger.write_warning(_("Cannot find lower free IEC channel than %d\n")%CurrentChannel)
33
59b84ab7bf8b Enhanced bahavior of plugin tree representation when changing IEC channel
etisserant
parents: 29
diff changeset
   399
                    return CurrentChannel # Can't go bellow 0, do nothing
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   400
            else : # Want to go up ?
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   401
                res +=  1 # Test for n-1
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   402
        # Finally set IEC Channel
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   403
        self.BaseParams.setIEC_Channel(res)
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   404
        return res
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   405
716
180e4a7d945c Adding search field for finding function or function block in library tree
laurent
parents: 715
diff changeset
   406
    def _OpenView(self, name=None):
656
c1792dfc8c7e Fixing bug integrated plugin editors not closed when removing corresponding plugin
laurent
parents: 655
diff changeset
   407
        if self.EditorType is not None and self._View is None:
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   408
            app_frame = self.GetCTRoot().AppFrame
656
c1792dfc8c7e Fixing bug integrated plugin editors not closed when removing corresponding plugin
laurent
parents: 655
diff changeset
   409
            
c1792dfc8c7e Fixing bug integrated plugin editors not closed when removing corresponding plugin
laurent
parents: 655
diff changeset
   410
            self._View = self.EditorType(app_frame.TabsOpened, self, app_frame)
c1792dfc8c7e Fixing bug integrated plugin editors not closed when removing corresponding plugin
laurent
parents: 655
diff changeset
   411
            
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   412
            app_frame.EditProjectElement(self._View, self.CTNName())
715
135566ab0807 Adding support for automatically saving and restoring state of frame or project perspective
laurent
parents: 707
diff changeset
   413
            
135566ab0807 Adding support for automatically saving and restoring state of frame or project perspective
laurent
parents: 707
diff changeset
   414
            return self._View
135566ab0807 Adding support for automatically saving and restoring state of frame or project perspective
laurent
parents: 707
diff changeset
   415
        return None
675
44b35c27e9ff Adding support for quickly open recent projects in file menu
laurent
parents: 674
diff changeset
   416
44b35c27e9ff Adding support for quickly open recent projects in file menu
laurent
parents: 674
diff changeset
   417
    def OnCloseEditor(self, view):
44b35c27e9ff Adding support for quickly open recent projects in file menu
laurent
parents: 674
diff changeset
   418
        if self._View == view:
44b35c27e9ff Adding support for quickly open recent projects in file menu
laurent
parents: 674
diff changeset
   419
            self._View = None
656
c1792dfc8c7e Fixing bug integrated plugin editors not closed when removing corresponding plugin
laurent
parents: 655
diff changeset
   420
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   421
    def OnCTNClose(self):
656
c1792dfc8c7e Fixing bug integrated plugin editors not closed when removing corresponding plugin
laurent
parents: 655
diff changeset
   422
        if self._View is not None:
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   423
            app_frame = self.GetCTRoot().AppFrame
656
c1792dfc8c7e Fixing bug integrated plugin editors not closed when removing corresponding plugin
laurent
parents: 655
diff changeset
   424
            if app_frame is not None:
c1792dfc8c7e Fixing bug integrated plugin editors not closed when removing corresponding plugin
laurent
parents: 655
diff changeset
   425
                app_frame.DeletePage(self._View)
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   426
        return True
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   427
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   428
    def _doRemoveChild(self, CTNInstance):
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   429
        # Remove all children of child
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   430
        for SubCTNInstance in CTNInstance.IterChildren():
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   431
            CTNInstance._doRemoveChild(SubCTNInstance)
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   432
        # Call the OnCloseMethod
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   433
        CTNInstance.OnCTNClose()
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   434
        # Delete confnode dir
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   435
        shutil.rmtree(CTNInstance.CTNPath())
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   436
        # Remove child of Children
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   437
        self.Children[CTNInstance.CTNType].remove(CTNInstance)
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   438
        # Forget it... (View have to refresh)
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   439
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   440
    def CTNRemove(self):
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   441
        # Fetch the confnode
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   442
        #CTNInstance = self.GetChildByName(CTNName)
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   443
        # Ask to his parent to remove it
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   444
        self.CTNParent._doRemoveChild(self)
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   445
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   446
    def CTNAddChild(self, CTNName, CTNType, IEC_Channel=0):
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   447
        """
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   448
        Create the confnodes that may be added as child to this node self
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   449
        @param CTNType: string desining the confnode class name (get name from CTNChildrenTypes)
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   450
        @param CTNName: string for the name of the confnode instance
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   451
        """
720
6be032177e2a refactoring
Edouard Tisserant
parents: 718
diff changeset
   452
        # reorganize self.CTNChildrenTypes tuples from (name, CTNClass, Help)
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   453
        # to ( name, (CTNClass, Help)), an make a dict
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   454
        transpose = zip(*self.CTNChildrenTypes)
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   455
        CTNChildrenTypes = dict(zip(transpose[0],zip(transpose[1],transpose[2])))
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   456
        # Check that adding this confnode is allowed
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   457
        try:
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   458
            CTNClass, CTNHelp = CTNChildrenTypes[CTNType]
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   459
        except KeyError:
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   460
            raise Exception, _("Cannot create child %s of type %s ")%(CTNName, CTNType)
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   461
        
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   462
        # if CTNClass is a class factory, call it. (prevent unneeded imports)
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   463
        if type(CTNClass) == types.FunctionType:
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   464
            CTNClass = CTNClass()
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   465
        
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   466
        # Eventualy Initialize child instance list for this class of confnode
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   467
        ChildrenWithSameClass = self.Children.setdefault(CTNType, list())
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   468
        # Check count
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   469
        if getattr(CTNClass, "CTNMaxCount", None) and len(ChildrenWithSameClass) >= CTNClass.CTNMaxCount:
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   470
            raise Exception, _("Max count (%d) reached for this confnode of type %s ")%(CTNClass.CTNMaxCount, CTNType)
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   471
        
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   472
        # create the final class, derived of provided confnode and template
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   473
        class FinalCTNClass(CTNClass, ConfigTreeNode):
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   474
            """
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   475
            ConfNode class is derivated into FinalCTNClass before being instanciated
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   476
            This way __init__ is overloaded to ensure ConfigTreeNode.__init__ is called 
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   477
            before CTNClass.__init__, and to do the file related stuff.
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   478
            """
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   479
            def __init__(_self):
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   480
                # self is the parent
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   481
                _self.CTNParent = self
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   482
                # Keep track of the confnode type name
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   483
                _self.CTNType = CTNType
106
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   484
                # remind the help string, for more fancy display
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   485
                _self.CTNHelp = CTNHelp
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   486
                # Call the base confnode template init - change XSD into class members
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   487
                ConfigTreeNode.__init__(_self)
29
282380dea497 Major improvements, plugin renaming and secured name/IEC channel attribution, various fixes on PlugTemplate
etisserant
parents: 25
diff changeset
   488
                # check name is unique
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   489
                NewCTNName = _self.FindNewName(CTNName)
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   490
                # If dir have already be made, and file exist
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   491
                if os.path.isdir(_self.CTNPath(NewCTNName)): #and os.path.isfile(_self.ConfNodeXmlFilePath(CTNName)):
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   492
                    #Load the confnode.xml file into parameters members
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   493
                    _self.LoadXMLParams(NewCTNName)
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   494
                    # Basic check. Better to fail immediately.
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   495
                    if (_self.BaseParams.getName() != NewCTNName):
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   496
                        raise Exception, _("Project tree layout do not match confnode.xml %s!=%s ")%(NewCTNName, _self.BaseParams.getName())
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   497
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   498
                    # Now, self.CTNPath() should be OK
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   499
                    
15
7a473efc4530 More precise design for plugins.... to be continued...
etisserant
parents: 14
diff changeset
   500
                    # Check that IEC_Channel is not already in use.
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   501
                    _self.FindNewIEC_Channel(_self.BaseParams.getIEC_Channel())
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   502
                    # Call the confnode real __init__
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   503
                    if getattr(CTNClass, "__init__", None):
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   504
                        CTNClass.__init__(_self)
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   505
                    #Load and init all the children
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   506
                    _self.LoadChildren()
118
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   507
                    #just loaded, nothing to saved
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   508
                    _self.ChangesToSave = False
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   509
                else:
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   510
                    # If confnode do not have corresponding file/dirs - they will be created on Save
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   511
                    _self.CTNMakeDir()
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   512
                    # Find an IEC number
417
a895ae50b737 Adding support for declaring PluginRoot outside of Beremiz
laurent
parents: 415
diff changeset
   513
                    _self.FindNewIEC_Channel(IEC_Channel)
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   514
                    # Call the confnode real __init__
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   515
                    if getattr(CTNClass, "__init__", None):
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   516
                        CTNClass.__init__(_self)
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   517
                    _self.CTNRequestSave()
118
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   518
                    #just created, must be saved
185d0d371ea4 Refuse close if PLC running.
etisserant
parents: 115
diff changeset
   519
                    _self.ChangesToSave = True
403
ae4a85291441 Removing memory leak while closing PluginsRoot
laurent
parents: 402
diff changeset
   520
                
77
7de69369373e Adding file with generated master in build folder and a button for editing it with objdictedit
lbessard
parents: 75
diff changeset
   521
            def _getBuildPath(_self):
7de69369373e Adding file with generated master in build folder and a button for editing it with objdictedit
lbessard
parents: 75
diff changeset
   522
                return self._getBuildPath()
7de69369373e Adding file with generated master in build folder and a button for editing it with objdictedit
lbessard
parents: 75
diff changeset
   523
            
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   524
        # Create the object out of the resulting class
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   525
        newConfNodeOpj = FinalCTNClass()
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   526
        # Store it in CTNgedChils
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   527
        ChildrenWithSameClass.append(newConfNodeOpj)
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   528
        
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   529
        return newConfNodeOpj
403
ae4a85291441 Removing memory leak while closing PluginsRoot
laurent
parents: 402
diff changeset
   530
    
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   531
    def ClearChildren(self):
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   532
        for child in self.IterChildren():
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   533
            child.ClearChildren()
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   534
        self.Children = {}
403
ae4a85291441 Removing memory leak while closing PluginsRoot
laurent
parents: 402
diff changeset
   535
    
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   536
    def LoadXMLParams(self, CTNName = None):
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   537
        methode_name = os.path.join(self.CTNPath(CTNName), "methods.py")
105
434aed8dc58d Added ability to override plugin methods with arbitrary python code (methods.py) when loading plugins
etisserant
parents: 97
diff changeset
   538
        if os.path.isfile(methode_name):
434aed8dc58d Added ability to override plugin methods with arbitrary python code (methods.py) when loading plugins
etisserant
parents: 97
diff changeset
   539
            execfile(methode_name)
274
8628f3dd0979 Adding support for defining plugin library as a plcopen xml file in plugin folder
greg
parents: 273
diff changeset
   540
        
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   541
        # Get the base xml tree
20
d3cb5020997b Beremiz plugins definitions.
etisserant
parents: 19
diff changeset
   542
        if self.MandatoryParams:
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   543
            try:
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   544
                basexmlfile = open(self.ConfNodeBaseXmlFilePath(CTNName), 'r')
106
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   545
                basetree = minidom.parse(basexmlfile)
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   546
                self.MandatoryParams[1].loadXMLTree(basetree.childNodes[0])
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   547
                basexmlfile.close()
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   548
            except Exception, exc:
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   549
                self.GetCTRoot().logger.write_error(_("Couldn't load confnode base parameters %s :\n %s") % (CTNName, str(exc)))
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   550
                self.GetCTRoot().logger.write_error(traceback.format_exc())
17
ee8cb104dbe0 First commit of Beremiz new version with plugin support
lbessard
parents: 16
diff changeset
   551
        
14
eb9fdd316a40 More precise design for plugins.... to be continued...
etisserant
parents: 13
diff changeset
   552
        # Get the xml tree
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   553
        if self.CTNParams:
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   554
            try:
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   555
                xmlfile = open(self.ConfNodeXmlFilePath(CTNName), 'r')
106
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   556
                tree = minidom.parse(xmlfile)
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   557
                self.CTNParams[1].loadXMLTree(tree.childNodes[0])
106
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   558
                xmlfile.close()
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   559
            except Exception, exc:
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   560
                self.GetCTRoot().logger.write_error(_("Couldn't load confnode parameters %s :\n %s") % (CTNName, str(exc)))
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   561
                self.GetCTRoot().logger.write_error(traceback.format_exc())
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   562
        
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   563
    def LoadChildren(self):
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   564
        # Iterate over all CTNName@CTNType in confnode directory, and try to open them
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   565
        for CTNDir in os.listdir(self.CTNPath()):
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   566
            if os.path.isdir(os.path.join(self.CTNPath(), CTNDir)) and \
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   567
               CTNDir.count(NameTypeSeparator) == 1:
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   568
                pname, ptype = CTNDir.split(NameTypeSeparator)
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   569
                try:
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   570
                    self.CTNAddChild(pname, ptype)
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   571
                except Exception, exc:
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   572
                    self.GetCTRoot().logger.write_error(_("Could not add child \"%s\", type %s :\n%s\n")%(pname, ptype, str(exc)))
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   573
                    self.GetCTRoot().logger.write_error(traceback.format_exc())
13
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents:
diff changeset
   574
109
f27ca37b6e7a Added enable/disable of plugin method buttons. Fixed alpha graying problem with disabled buttons. Updated debug dialog message with bug report path
etisserant
parents: 108
diff changeset
   575
    def EnableMethod(self, method, value):
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   576
        for d in self.ConfNodeMethods:
109
f27ca37b6e7a Added enable/disable of plugin method buttons. Fixed alpha graying problem with disabled buttons. Updated debug dialog message with bug report path
etisserant
parents: 108
diff changeset
   577
            if d["method"]==method:
f27ca37b6e7a Added enable/disable of plugin method buttons. Fixed alpha graying problem with disabled buttons. Updated debug dialog message with bug report path
etisserant
parents: 108
diff changeset
   578
                d["enabled"]=value
f27ca37b6e7a Added enable/disable of plugin method buttons. Fixed alpha graying problem with disabled buttons. Updated debug dialog message with bug report path
etisserant
parents: 108
diff changeset
   579
                return True
f27ca37b6e7a Added enable/disable of plugin method buttons. Fixed alpha graying problem with disabled buttons. Updated debug dialog message with bug report path
etisserant
parents: 108
diff changeset
   580
        return False
f27ca37b6e7a Added enable/disable of plugin method buttons. Fixed alpha graying problem with disabled buttons. Updated debug dialog message with bug report path
etisserant
parents: 108
diff changeset
   581
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   582
    def ShowMethod(self, method, value):
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   583
        for d in self.ConfNodeMethods:
203
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   584
            if d["method"]==method:
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   585
                d["shown"]=value
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   586
                return True
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   587
        return False
cb9901076a21 Added concepts :
etisserant
parents: 202
diff changeset
   588
623
8cdb533c3c7a A few new keyboard shortcuts : F4=stop, F5=run, F5=transfer, F7=connect
Edouard Tisserant
parents: 619
diff changeset
   589
    def CallMethod(self, method):
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 716
diff changeset
   590
        for d in self.ConfNodeMethods:
623
8cdb533c3c7a A few new keyboard shortcuts : F4=stop, F5=run, F5=transfer, F7=connect
Edouard Tisserant
parents: 619
diff changeset
   591
            if d["method"]==method and d.get("enabled", True) and d.get("shown", True):
8cdb533c3c7a A few new keyboard shortcuts : F4=stop, F5=run, F5=transfer, F7=connect
Edouard Tisserant
parents: 619
diff changeset
   592
                getattr(self, method)()
8cdb533c3c7a A few new keyboard shortcuts : F4=stop, F5=run, F5=transfer, F7=connect
Edouard Tisserant
parents: 619
diff changeset
   593