ProjectController.py
changeset 728 e0424e96e3fd
parent 726 ae63ccc29444
child 730 4480b7da6bea
equal deleted inserted replaced
727:3edd2f19bce2 728:e0424e96e3fd
     1 """
     1 """
     2 Base definitions for beremiz confnodes
     2 Beremiz Project Controller 
     3 """
     3 """
     4 
     4 
     5 import os,sys,traceback
     5 import os,sys,traceback
     6 import time
     6 import time
     7 import features
     7 import features
    59                 </xsd:choice>
    59                 </xsd:choice>
    60               </xsd:complexType>
    60               </xsd:complexType>
    61             </xsd:element>
    61             </xsd:element>
    62           </xsd:sequence>
    62           </xsd:sequence>
    63           <xsd:attribute name="URI_location" type="xsd:string" use="optional" default=""/>
    63           <xsd:attribute name="URI_location" type="xsd:string" use="optional" default=""/>
    64           <xsd:attribute name="Enable_ConfNodes" type="xsd:boolean" use="optional" default="true"/>
    64           <xsd:attribute name="Disable_Extensions" type="xsd:boolean" use="optional" default="false"/>
       
    65           """+"\n".join(['<xsd:attribute name="Enable_'+lib.rsplit('.',1)[-1]+'" type="xsd:boolean" use="optional" default="true"/>' for lib in features.libraries])+"""
    65         </xsd:complexType>
    66         </xsd:complexType>
    66       </xsd:element>
    67       </xsd:element>
    67     </xsd:schema>
    68     </xsd:schema>
    68     """
    69     """
    69 
    70 
    82         self.IECdebug_datas = {}
    83         self.IECdebug_datas = {}
    83         self.IECdebug_lock = Lock()
    84         self.IECdebug_lock = Lock()
    84 
    85 
    85         self.DebugTimer=None
    86         self.DebugTimer=None
    86         self.ResetIECProgramsAndVariables()
    87         self.ResetIECProgramsAndVariables()
    87         
       
    88         #This method are not called here... but in NewProject and OpenProject
       
    89         #self._AddParamsMembers()
       
    90         #self.Children = {}
       
    91 
    88 
    92         # In both new or load scenario, no need to save
    89         # In both new or load scenario, no need to save
    93         self.ChangesToSave = False
    90         self.ChangesToSave = False
    94         # root have no parent
    91         # root have no parent
    95         self.CTNParent = None
    92         self.CTNParent = None
   102         self.DebugThread = None
    99         self.DebugThread = None
   103         self.debug_break = False
   100         self.debug_break = False
   104         self.previous_plcstate = None
   101         self.previous_plcstate = None
   105         # copy ConfNodeMethods so that it can be later customized
   102         # copy ConfNodeMethods so that it can be later customized
   106         self.ConfNodeMethods = [dic.copy() for dic in self.ConfNodeMethods]
   103         self.ConfNodeMethods = [dic.copy() for dic in self.ConfNodeMethods]
   107         self.LoadSTLibrary()
   104 
       
   105     def LoadLibraries(self):
       
   106         self.Libraries = []
       
   107         TypeStack=[]
       
   108         for clsname in features.libraries:
       
   109             if getattr(self.BeremizRoot, "Enable_"+clsname.rsplit('.',1)[-1]):
       
   110                 Lib = GetClassImporter(clsname)()(TypeStack)
       
   111                 TypeStack.append(Lib.GetTypes())
       
   112                 self.Libraries.append(Lib)
   108 
   113 
   109     def __del__(self):
   114     def __del__(self):
   110         if self.DebugTimer:
   115         if self.DebugTimer:
   111             self.DebugTimer.cancel()
   116             self.DebugTimer.cancel()
   112         self.KillDebugThread()
   117         self.KillDebugThread()
   129             self.AppFrame.Unbind(wx.EVT_TIMER, self.StatusTimer)
   134             self.AppFrame.Unbind(wx.EVT_TIMER, self.StatusTimer)
   130             self.StatusTimer = None
   135             self.StatusTimer = None
   131             self.AppFrame = None
   136             self.AppFrame = None
   132         
   137         
   133         self.logger = logger
   138         self.logger = logger
   134 
       
   135     def ConfNodeLibraryFilePath(self):
       
   136         return os.path.join(os.path.split(__file__)[0], "pous.xml")
       
   137 
   139 
   138     def CTNTestModified(self):
   140     def CTNTestModified(self):
   139          return self.ChangesToSave or not self.ProjectIsSaved()
   141          return self.ChangesToSave or not self.ProjectIsSaved()
   140 
   142 
   141     def CTNFullName(self):
   143     def CTNFullName(self):
   309                 if dosave:
   311                 if dosave:
   310                     self.SaveProject()
   312                     self.SaveProject()
   311                 self._setBuildPath(self.BuildPath)
   313                 self._setBuildPath(self.BuildPath)
   312                 return True
   314                 return True
   313         return False
   315         return False
       
   316 
       
   317     def GetLibrariesTypes(self):
       
   318         self.LoadLibraries()
       
   319         return [ lib.GetTypes() for lib in self.Libraries ]
       
   320 
       
   321     def GetLibrariesSTCode(self):
       
   322         return "\n".join([ lib.GetSTCode() for lib in self.Libraries ])
       
   323 
       
   324     def GetLibrariesCCode(self, buildpath):
       
   325         self.GetIECProgramsAndVariables()
       
   326         LibIECCflags = '"-I%s"'%os.path.abspath(self.GetIECLibPath())
       
   327         LocatedCCodeAndFlags=[]
       
   328         Extras=[]
       
   329         for lib in self.Libraries:
       
   330             res=lib.Generate_C(buildpath,self._VariablesList,LibIECCflags)  
       
   331             LocatedCCodeAndFlags.append(res[:2])
       
   332             if len(res)>2:
       
   333                 Extras.append(res[2:])
       
   334         return map(list,zip(*LocatedCCodeAndFlags))+[tuple(Extras)]
   314     
   335     
   315     # Update PLCOpenEditor ConfNode Block types from loaded confnodes
   336     # Update PLCOpenEditor ConfNode Block types from loaded confnodes
   316     def RefreshConfNodesBlockLists(self):
   337     def RefreshConfNodesBlockLists(self):
   317         if getattr(self, "Children", None) is not None:
   338         if getattr(self, "Children", None) is not None:
   318             self.ClearConfNodeTypes()
   339             self.ClearConfNodeTypes()
   319             self.AddConfNodeTypesList(self.ConfNodesTypesFactory())
   340             self.AddConfNodeTypesList(self.GetLibrariesTypes())
   320         if self.AppFrame is not None:
   341         if self.AppFrame is not None:
   321             self.AppFrame.RefreshLibraryPanel()
   342             self.AppFrame.RefreshLibraryPanel()
   322             self.AppFrame.RefreshEditor()
   343             self.AppFrame.RefreshEditor()
   323     
   344     
   324     # Update a PLCOpenEditor Pou variable location
   345     # Update a PLCOpenEditor Pou variable location
   447             # Failed !
   468             # Failed !
   448             self.logger.write_error(_("Error in ST/IL/SFC code generator :\n%s\n")%errors[0])
   469             self.logger.write_error(_("Error in ST/IL/SFC code generator :\n%s\n")%errors[0])
   449             return False
   470             return False
   450         plc_file = open(self._getIECcodepath(), "w")
   471         plc_file = open(self._getIECcodepath(), "w")
   451         # Add ST Library from confnodes
   472         # Add ST Library from confnodes
   452         plc_file.write(self.ConfNodesSTLibraryFactory())
   473         plc_file.write(self.GetLibrariesSTCode())
   453         if os.path.isfile(self._getIECrawcodepath()):
   474         if os.path.isfile(self._getIECrawcodepath()):
   454             plc_file.write(open(self._getIECrawcodepath(), "r").read())
   475             plc_file.write(open(self._getIECrawcodepath(), "r").read())
   455             plc_file.write("\n")
   476             plc_file.write("\n")
   456         plc_file.close()
   477         plc_file.close()
   457         plc_file = open(self._getIECcodepath(), "r")
   478         plc_file = open(self._getIECcodepath(), "r")
   706         # in retreive, publish, init, cleanup
   727         # in retreive, publish, init, cleanup
   707         locstrs = map(lambda x:"_".join(map(str,x)),
   728         locstrs = map(lambda x:"_".join(map(str,x)),
   708            [loc for loc,Cfiles,DoCalls in self.LocationCFilesAndCFLAGS if loc and DoCalls])
   729            [loc for loc,Cfiles,DoCalls in self.LocationCFilesAndCFLAGS if loc and DoCalls])
   709 
   730 
   710         # Generate main, based on template
   731         # Generate main, based on template
   711         if self.BeremizRoot.getEnable_ConfNodes():
   732         if not self.BeremizRoot.getDisable_Extensions():
   712             plc_main_code = targets.code("plc_common_main") % {
   733             plc_main_code = targets.code("plc_common_main") % {
   713                 "calls_prototypes":"\n".join([(
   734                 "calls_prototypes":"\n".join([(
   714                       "int __init_%(s)s(int argc,char **argv);\n"+
   735                       "int __init_%(s)s(int argc,char **argv);\n"+
   715                       "void __cleanup_%(s)s(void);\n"+
   736                       "void __cleanup_%(s)s(void);\n"+
   716                       "void __retrieve_%(s)s(void);\n"+
   737                       "void __retrieve_%(s)s(void);\n"+
   772         # CSV file generated by IEC2C compiler.
   793         # CSV file generated by IEC2C compiler.
   773         self.ResetIECProgramsAndVariables()
   794         self.ResetIECProgramsAndVariables()
   774         
   795         
   775         # Generate C code and compilation params from confnode hierarchy
   796         # Generate C code and compilation params from confnode hierarchy
   776         try:
   797         try:
   777             self.LocationCFilesAndCFLAGS, self.LDFLAGS, ExtraFiles = self._Generate_C(
   798             CTNLocationCFilesAndCFLAGS, CTNLDFLAGS, CTNExtraFiles = self._Generate_C(
   778                 buildpath, 
   799                 buildpath, 
   779                 self.PLCGeneratedLocatedVars)
   800                 self.PLCGeneratedLocatedVars)
   780         except Exception, exc:
   801         except Exception, exc:
   781             self.logger.write_error(_("Runtime extensions C code generation failed !\n"))
   802             self.logger.write_error(_("Runtime extensions C code generation failed !\n"))
   782             self.logger.write_error(traceback.format_exc())
   803             self.logger.write_error(traceback.format_exc())
   783             self.ResetBuildMD5()
   804             self.ResetBuildMD5()
   784             return False
   805             return False
       
   806 
       
   807         # Generate C code and compilation params from liraries
       
   808         try:
       
   809             LibCFilesAndCFLAGS, LibLDFLAGS, LibExtraFiles = self.GetLibrariesCCode(buildpath)
       
   810         except Exception, exc:
       
   811             self.logger.write_error(_("Runtime extensions C code generation failed !\n"))
       
   812             self.logger.write_error(traceback.format_exc())
       
   813             self.ResetBuildMD5()
       
   814             return False
       
   815 
       
   816         self.LocationCFilesAndCFLAGS =  CTNLocationCFilesAndCFLAGS + LibCFilesAndCFLAGS
       
   817         self.LDFLAGS = CTNLDFLAGS + LibLDFLAGS
       
   818         ExtraFiles = CTNExtraFiles + LibExtraFiles
   785 
   819 
   786         # Get temporary directory path
   820         # Get temporary directory path
   787         extrafilespath = self._getExtraFilesPath()
   821         extrafilespath = self._getExtraFilesPath()
   788         # Remove old directory
   822         # Remove old directory
   789         if os.path.exists(extrafilespath):
   823         if os.path.exists(extrafilespath):