ProjectController.py
changeset 2459 21164625b393
parent 2429 15f18dc8b56a
parent 2456 7373e3048167
child 2462 ed6b0e905fcb
equal deleted inserted replaced
2458:2a70d5240300 2459:21164625b393
    34 import time
    34 import time
    35 from time import localtime
    35 from time import localtime
    36 import shutil
    36 import shutil
    37 import re
    37 import re
    38 import tempfile
    38 import tempfile
    39 from types import ListType
       
    40 from threading import Timer
    39 from threading import Timer
    41 from datetime import datetime
    40 from datetime import datetime
    42 from weakref import WeakKeyDictionary
    41 from weakref import WeakKeyDictionary
    43 from itertools import izip
    42 from functools import reduce
       
    43 from six.moves import xrange
    44 
    44 
    45 import wx
    45 import wx
    46 
    46 
    47 import features
    47 import features
    48 import connectors
    48 import connectors
    65 from ConfigTreeNode import ConfigTreeNode, XSDSchemaErrorMessage
    65 from ConfigTreeNode import ConfigTreeNode, XSDSchemaErrorMessage
    66 
    66 
    67 base_folder = paths.AbsParentDir(__file__)
    67 base_folder = paths.AbsParentDir(__file__)
    68 
    68 
    69 MATIEC_ERROR_MODEL = re.compile(
    69 MATIEC_ERROR_MODEL = re.compile(
    70     ".*\.st:(\d+)-(\d+)\.\.(\d+)-(\d+): (?:error)|(?:warning) : (.*)$")
    70     r".*\.st:(\d+)-(\d+)\.\.(\d+)-(\d+): (?:error)|(?:warning) : (.*)$")
    71 
    71 
    72 
    72 
    73 def ExtractChildrenTypesFromCatalog(catalog):
    73 def ExtractChildrenTypesFromCatalog(catalog):
    74     children_types = []
    74     children_types = []
    75     for n, d, _h, c in catalog:
    75     for n, d, _h, c in catalog:
    76         if isinstance(c, ListType):
    76         if isinstance(c, list):
    77             children_types.extend(ExtractChildrenTypesFromCatalog(c))
    77             children_types.extend(ExtractChildrenTypesFromCatalog(c))
    78         else:
    78         else:
    79             children_types.append((n, GetClassImporter(c), d))
    79             children_types.append((n, GetClassImporter(c), d))
    80     return children_types
    80     return children_types
    81 
    81 
    82 
    82 
    83 def ExtractMenuItemsFromCatalog(catalog):
    83 def ExtractMenuItemsFromCatalog(catalog):
    84     menu_items = []
    84     menu_items = []
    85     for n, d, h, c in catalog:
    85     for n, d, h, c in catalog:
    86         if isinstance(c, ListType):
    86         if isinstance(c, list):
    87             children = ExtractMenuItemsFromCatalog(c)
    87             children = ExtractMenuItemsFromCatalog(c)
    88         else:
    88         else:
    89             children = []
    89             children = []
    90         menu_items.append((n, d, h, children))
    90         menu_items.append((n, d, h, children))
    91     return menu_items
    91     return menu_items
   710                 os.path.join(self._getBuildPath(), "LOCATED_VARIABLES.h"))
   710                 os.path.join(self._getBuildPath(), "LOCATED_VARIABLES.h"))
   711             # each line of LOCATED_VARIABLES.h declares a located variable
   711             # each line of LOCATED_VARIABLES.h declares a located variable
   712             lines = [line.strip() for line in location_file.readlines()]
   712             lines = [line.strip() for line in location_file.readlines()]
   713             # This regular expression parses the lines genereated by IEC2C
   713             # This regular expression parses the lines genereated by IEC2C
   714             LOCATED_MODEL = re.compile(
   714             LOCATED_MODEL = re.compile(
   715                 "__LOCATED_VAR\((?P<IEC_TYPE>[A-Z]*),(?P<NAME>[_A-Za-z0-9]*),(?P<DIR>[QMI])(?:,(?P<SIZE>[XBWDL]))?,(?P<LOC>[,0-9]*)\)")
   715                 r"__LOCATED_VAR\((?P<IEC_TYPE>[A-Z]*),(?P<NAME>[_A-Za-z0-9]*),(?P<DIR>[QMI])(?:,(?P<SIZE>[XBWDL]))?,(?P<LOC>[,0-9]*)\)")
   716             for line in lines:
   716             for line in lines:
   717                 # If line match RE,
   717                 # If line match RE,
   718                 result = LOCATED_MODEL.match(line)
   718                 result = LOCATED_MODEL.match(line)
   719                 if result:
   719                 if result:
   720                     # Get the resulting dict
   720                     # Get the resulting dict
   768             plc_file.write(open(self._getIECrawcodepath(), "r").read())
   768             plc_file.write(open(self._getIECrawcodepath(), "r").read())
   769             plc_file.write("\n")
   769             plc_file.write("\n")
   770         plc_file.close()
   770         plc_file.close()
   771         plc_file = open(self._getIECcodepath(), "r")
   771         plc_file = open(self._getIECcodepath(), "r")
   772         self.ProgramOffset = 0
   772         self.ProgramOffset = 0
   773         for dummy in plc_file.xreadlines():
   773         for dummy in plc_file.readlines():
   774             self.ProgramOffset += 1
   774             self.ProgramOffset += 1
   775         plc_file.close()
   775         plc_file.close()
   776         plc_file = open(self._getIECcodepath(), "a")
   776         plc_file = open(self._getIECcodepath(), "a")
   777         plc_file.write(open(self._getIECgeneratedcodepath(), "r").read())
   777         plc_file.write(open(self._getIECgeneratedcodepath(), "r").read())
   778         plc_file.close()
   778         plc_file.close()
   856             -2:] == ".h" or fname[-2:] == ".H"]
   856             -2:] == ".h" or fname[-2:] == ".H"]
   857         H_files.remove("LOCATED_VARIABLES.h")
   857         H_files.remove("LOCATED_VARIABLES.h")
   858         H_files = map(
   858         H_files = map(
   859             lambda filename: os.path.join(buildpath, filename), H_files)
   859             lambda filename: os.path.join(buildpath, filename), H_files)
   860         for H_file in H_files:
   860         for H_file in H_files:
   861             with file(H_file, 'r') as original:
   861             with open(H_file, 'r') as original:
   862                 data = original.read()
   862                 data = original.read()
   863             with file(H_file, 'w') as modified:
   863             with open(H_file, 'w') as modified:
   864                 modified.write('#include "beremiz.h"\n' + data)
   864                 modified.write('#include "beremiz.h"\n' + data)
   865 
   865 
   866         self.logger.write(_("Extracting Located Variables...\n"))
   866         self.logger.write(_("Extracting Located Variables...\n"))
   867         # Keep track of generated located variables for later use by
   867         # Keep track of generated located variables for later use by
   868         # self._Generate_C
   868         # self._Generate_C
   950                 self._DbgVariablesList = []
   950                 self._DbgVariablesList = []
   951                 self._IECPathToIdx = {}
   951                 self._IECPathToIdx = {}
   952 
   952 
   953                 # Separate sections
   953                 # Separate sections
   954                 ListGroup = []
   954                 ListGroup = []
   955                 for line in open(csvfile, 'r').xreadlines():
   955                 for line in open(csvfile, 'r').readlines():
   956                     strippedline = line.strip()
   956                     strippedline = line.strip()
   957                     if strippedline.startswith("//"):
   957                     if strippedline.startswith("//"):
   958                         # Start new section
   958                         # Start new section
   959                         ListGroup.append([])
   959                         ListGroup.append([])
   960                     elif len(strippedline) > 0 and len(ListGroup) > 0:
   960                     elif len(strippedline) > 0 and len(ListGroup) > 0:
  1291                 self._IECCodeView = IECCodeViewer(
  1291                 self._IECCodeView = IECCodeViewer(
  1292                     self.AppFrame.TabsOpened, "", self.AppFrame, None, instancepath=name)
  1292                     self.AppFrame.TabsOpened, "", self.AppFrame, None, instancepath=name)
  1293                 self._IECCodeView.SetTextSyntax("ALL")
  1293                 self._IECCodeView.SetTextSyntax("ALL")
  1294                 self._IECCodeView.SetKeywords(IEC_KEYWORDS)
  1294                 self._IECCodeView.SetKeywords(IEC_KEYWORDS)
  1295                 try:
  1295                 try:
  1296                     text = file(plc_file).read()
  1296                     text = open(plc_file).read()
  1297                 except Exception:
  1297                 except Exception:
  1298                     text = '(* No IEC code have been generated at that time ! *)'
  1298                     text = '(* No IEC code have been generated at that time ! *)'
  1299                 self._IECCodeView.SetText(text=text)
  1299                 self._IECCodeView.SetText(text=text)
  1300                 self._IECCodeView.Editor.SetReadOnly(True)
  1300                 self._IECCodeView.Editor.SetReadOnly(True)
  1301                 self._IECCodeView.SetIcon(GetBitmap("ST"))
  1301                 self._IECCodeView.SetIcon(GetBitmap("ST"))
  1501                 if len(Traces) > 0:
  1501                 if len(Traces) > 0:
  1502                     for debug_tick, debug_buff in Traces:
  1502                     for debug_tick, debug_buff in Traces:
  1503                         debug_vars = UnpackDebugBuffer(
  1503                         debug_vars = UnpackDebugBuffer(
  1504                             debug_buff, self.TracedIECTypes)
  1504                             debug_buff, self.TracedIECTypes)
  1505                         if debug_vars is not None and len(debug_vars) == len(self.TracedIECPath):
  1505                         if debug_vars is not None and len(debug_vars) == len(self.TracedIECPath):
  1506                             for IECPath, values_buffer, value in izip(
  1506                             for IECPath, values_buffer, value in zip(
  1507                                     self.TracedIECPath,
  1507                                     self.TracedIECPath,
  1508                                     self.DebugValuesBuffers,
  1508                                     self.DebugValuesBuffers,
  1509                                     debug_vars):
  1509                                     debug_vars):
  1510                                 IECdebug_data = self.IECdebug_datas.get(
  1510                                 IECdebug_data = self.IECdebug_datas.get(
  1511                                     IECPath, None)
  1511                                     IECPath, None)
  1680 
  1680 
  1681     def DispatchDebugValuesProc(self, event):
  1681     def DispatchDebugValuesProc(self, event):
  1682         debug_ticks, buffers = self.SnapshotAndResetDebugValuesBuffers()
  1682         debug_ticks, buffers = self.SnapshotAndResetDebugValuesBuffers()
  1683         start_time = time.time()
  1683         start_time = time.time()
  1684         if len(self.TracedIECPath) == len(buffers):
  1684         if len(self.TracedIECPath) == len(buffers):
  1685             for IECPath, values in izip(self.TracedIECPath, buffers):
  1685             for IECPath, values in zip(self.TracedIECPath, buffers):
  1686                 if len(values) > 0:
  1686                 if len(values) > 0:
  1687                     self.CallWeakcallables(
  1687                     self.CallWeakcallables(
  1688                         IECPath, "NewValues", debug_ticks, values)
  1688                         IECPath, "NewValues", debug_ticks, values)
  1689             if len(debug_ticks) > 0:
  1689             if len(debug_ticks) > 0:
  1690                 self.CallWeakcallables(
  1690                 self.CallWeakcallables(