Beremiz.py
author etisserant
Tue, 28 Aug 2007 15:00:52 +0200
changeset 15 7a473efc4530
parent 13 f1f0edbeb313
child 17 ee8cb104dbe0
permissions -rw-r--r--
More precise design for plugins.... to be continued...
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
     1
#!/usr/bin/env python
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
     3
3
6d8728efcdec Adding validity verifications on project folders opened
lbessard
parents: 2
diff changeset
     4
#This file is part of Beremiz, a Integrated Development Environment for
6d8728efcdec Adding validity verifications on project folders opened
lbessard
parents: 2
diff changeset
     5
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. 
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
     6
#
3
6d8728efcdec Adding validity verifications on project folders opened
lbessard
parents: 2
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
     8
#
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
     9
#See COPYING file for copyrights details.
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    10
#
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    15
#
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3
6d8728efcdec Adding validity verifications on project folders opened
lbessard
parents: 2
diff changeset
    19
#General Public License for more details.
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    20
#
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    24
13
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
    25
__version__ = "$Revision$"
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
    26
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    27
import wx
5
9565bb5facf7 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 4
diff changeset
    28
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    29
from time import localtime
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    30
from datetime import datetime
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    31
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
    32
import os, re, platform, sys, time, traceback, getopt, commands
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    33
base_folder = os.path.split(sys.path[0])[0]
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    34
sys.path.append(os.path.join(base_folder, "plcopeneditor"))
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    35
sys.path.append(os.path.join(base_folder, "CanFestival-3", "objdictgen"))
12
a1f9e514f708 plugin framework organization being defined
etisserant
parents: 11
diff changeset
    36
sys.path.append(os.path.join(base_folder, "wxsvg", "svgui", "defeditor"))
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    37
7
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    38
iec2cc_path = os.path.join(base_folder, "matiec", "iec2cc")
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    39
ieclib_path = os.path.join(base_folder, "matiec", "lib")
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    40
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    41
from PLCOpenEditor import PLCOpenEditor, ProjectDialog
11
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
    42
from TextViewer import TextViewer
12
a1f9e514f708 plugin framework organization being defined
etisserant
parents: 11
diff changeset
    43
from plcopen.structures import IEC_KEYWORDS#, AddPlugin
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    44
from PLCControler import PLCControler
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    45
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
    46
class LogPseudoFile:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
    47
    """ Base class for file like objects to facilitate StdOut for the Shell."""
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
    48
    def __init__(self, output = None):
7
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    49
        self.red_white = wx.TextAttr("RED", "WHITE")
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    50
        self.red_yellow = wx.TextAttr("RED", "YELLOW")
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    51
        self.black_white = wx.TextAttr("BLACK", "WHITE")
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    52
        self.default_style = None
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
    53
        self.output = output
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
    54
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
    55
    def writelines(self, l):
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
    56
        map(self.write, l)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
    57
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
    58
    def write(self, s):
7
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    59
        if self.default_style != self.black_white: 
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    60
            self.output.SetDefaultStyle(self.black_white)
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    61
            self.default_style = self.black_white
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    62
        self.output.AppendText(s) 
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    63
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    64
    def write_warning(self, s):
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    65
        if self.default_style != self.red_white: 
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    66
            self.output.SetDefaultStyle(self.red_white)
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    67
            self.default_style = self.red_white
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    68
        self.output.AppendText(s) 
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    69
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    70
    def write_error(self, s):
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    71
        if self.default_style != self.red_yellow: 
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    72
            self.output.SetDefaultStyle(self.red_yellow)
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    73
            self.default_style = self.red_yellow
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
    74
        self.output.AppendText(s) 
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
    75
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
    76
    def flush(self):
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
    77
        self.output.SetValue("")
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
    78
    
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
    79
    def isatty(self):
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
    80
        return false
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
    81
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
    82
[ID_BEREMIZ, ID_BEREMIZLOGCONSOLE, ID_BEREMIZEDITPLCBUTTON,
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
    83
 ID_BEREMIZBUILDBUTTON, ID_BEREMIZSIMULATEBUTTON,
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
    84
 ID_BEREMIZRUNBUTTON, ID_BEREMIZBUSLIST,
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
    85
 ID_BEREMIZADDBUSBUTTON, ID_BEREMIZDELETEBUSBUTTON,
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    86
] = [wx.NewId() for _init_ctrls in range(9)]
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    87
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
    88
[ID_BEREMIZFILEMENUITEMS0, ID_BEREMIZFILEMENUITEMS1, 
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
    89
 ID_BEREMIZFILEMENUITEMS2, ID_BEREMIZFILEMENUITEMS3, 
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
    90
 ID_BEREMIZFILEMENUITEMS5, ID_BEREMIZFILEMENUITEMS7, 
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    91
] = [wx.NewId() for _init_coll_FileMenu_Items in range(6)]
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    92
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
    93
[ID_BEREMIZEDITMENUITEMS0, ID_BEREMIZEDITMENUITEMS2, 
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
    94
 ID_BEREMIZEDITMENUITEMS3, 
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    95
] = [wx.NewId() for _init_coll_EditMenu_Items in range(3)]
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    96
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
    97
[ID_BEREMIZRUNMENUITEMS0, ID_BEREMIZRUNMENUITEMS2, 
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
    98
 ID_BEREMIZRUNMENUITEMS3, ID_BEREMIZRUNMENUITEMS5, 
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
    99
] = [wx.NewId() for _init_coll_EditMenu_Items in range(4)]
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   100
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   101
[ID_BEREMIZHELPMENUITEMS0, ID_BEREMIZHELPMENUITEMS1, 
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   102
] = [wx.NewId() for _init_coll_HelpMenu_Items in range(2)]
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   103
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   104
class Beremiz(wx.Frame):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   105
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   106
    def _init_coll_FileMenu_Items(self, parent):
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   107
        parent.Append(help='', id=ID_BEREMIZFILEMENUITEMS0,
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   108
              kind=wx.ITEM_NORMAL, text=u'New\tCTRL+N')
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   109
        parent.Append(help='', id=ID_BEREMIZFILEMENUITEMS1,
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   110
              kind=wx.ITEM_NORMAL, text=u'Open\tCTRL+O')
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   111
        parent.Append(help='', id=ID_BEREMIZFILEMENUITEMS2,
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   112
              kind=wx.ITEM_NORMAL, text=u'Save\tCTRL+S')
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   113
        parent.Append(help='', id=ID_BEREMIZFILEMENUITEMS3,
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   114
              kind=wx.ITEM_NORMAL, text=u'Close Project')
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   115
        parent.AppendSeparator()
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   116
        parent.Append(help='', id=ID_BEREMIZFILEMENUITEMS5,
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   117
              kind=wx.ITEM_NORMAL, text=u'Properties')
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   118
        parent.AppendSeparator()
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   119
        parent.Append(help='', id=ID_BEREMIZFILEMENUITEMS7,
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   120
              kind=wx.ITEM_NORMAL, text=u'Quit\tCTRL+Q')
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   121
        self.Bind(wx.EVT_MENU, self.OnNewProjectMenu,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   122
              id=ID_BEREMIZFILEMENUITEMS0)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   123
        self.Bind(wx.EVT_MENU, self.OnOpenProjectMenu,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   124
              id=ID_BEREMIZFILEMENUITEMS1)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   125
        self.Bind(wx.EVT_MENU, self.OnSaveProjectMenu,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   126
              id=ID_BEREMIZFILEMENUITEMS2)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   127
        self.Bind(wx.EVT_MENU, self.OnCloseProjectMenu,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   128
              id=ID_BEREMIZFILEMENUITEMS3)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   129
        self.Bind(wx.EVT_MENU, self.OnPropertiesMenu,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   130
              id=ID_BEREMIZFILEMENUITEMS5)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   131
        self.Bind(wx.EVT_MENU, self.OnQuitMenu,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   132
              id=ID_BEREMIZFILEMENUITEMS7)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   133
        
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   134
    def _init_coll_EditMenu_Items(self, parent):
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   135
        parent.Append(help='', id=ID_BEREMIZEDITMENUITEMS0,
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   136
              kind=wx.ITEM_NORMAL, text=u'Edit PLC\tCTRL+R')
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   137
        parent.AppendSeparator()
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   138
        parent.Append(help='', id=ID_BEREMIZEDITMENUITEMS2,
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   139
              kind=wx.ITEM_NORMAL, text=u'Add Bus')
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   140
        parent.Append(help='', id=ID_BEREMIZEDITMENUITEMS3,
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   141
              kind=wx.ITEM_NORMAL, text=u'Delete Bus')
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   142
        self.Bind(wx.EVT_MENU, self.OnEditPLCMenu,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   143
              id=ID_BEREMIZEDITMENUITEMS0)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   144
        self.Bind(wx.EVT_MENU, self.OnAddBusMenu,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   145
              id=ID_BEREMIZEDITMENUITEMS2)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   146
        self.Bind(wx.EVT_MENU, self.OnDeleteBusMenu,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   147
              id=ID_BEREMIZEDITMENUITEMS3)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   148
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   149
    def _init_coll_RunMenu_Items(self, parent):
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   150
        parent.Append(help='', id=ID_BEREMIZRUNMENUITEMS0,
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   151
              kind=wx.ITEM_NORMAL, text=u'Build\tCTRL+R')
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   152
        parent.AppendSeparator()
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   153
        parent.Append(help='', id=ID_BEREMIZRUNMENUITEMS2,
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   154
              kind=wx.ITEM_NORMAL, text=u'Simulate')
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   155
        parent.Append(help='', id=ID_BEREMIZRUNMENUITEMS3,
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   156
              kind=wx.ITEM_NORMAL, text=u'Run')
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   157
        parent.AppendSeparator()
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   158
        parent.Append(help='', id=ID_BEREMIZRUNMENUITEMS5,
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   159
              kind=wx.ITEM_NORMAL, text=u'Save Log')
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   160
        self.Bind(wx.EVT_MENU, self.OnBuildMenu,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   161
              id=ID_BEREMIZRUNMENUITEMS0)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   162
        self.Bind(wx.EVT_MENU, self.OnSimulateMenu,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   163
              id=ID_BEREMIZRUNMENUITEMS2)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   164
        self.Bind(wx.EVT_MENU, self.OnRunMenu,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   165
              id=ID_BEREMIZRUNMENUITEMS3)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   166
        self.Bind(wx.EVT_MENU, self.OnSaveLogMenu,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   167
              id=ID_BEREMIZRUNMENUITEMS5)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   168
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   169
    def _init_coll_HelpMenu_Items(self, parent):
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   170
        parent.Append(help='', id=ID_BEREMIZHELPMENUITEMS0,
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   171
              kind=wx.ITEM_NORMAL, text=u'Beremiz\tF1')
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   172
        parent.Append(help='', id=ID_BEREMIZHELPMENUITEMS1,
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   173
              kind=wx.ITEM_NORMAL, text=u'About')
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   174
        self.Bind(wx.EVT_MENU, self.OnBeremizMenu,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   175
              id=ID_BEREMIZHELPMENUITEMS0)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   176
        self.Bind(wx.EVT_MENU, self.OnAboutMenu,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   177
              id=ID_BEREMIZHELPMENUITEMS1)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   178
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   179
    def _init_coll_menuBar1_Menus(self, parent):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   180
        parent.Append(menu=self.FileMenu, title=u'File')
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   181
        parent.Append(menu=self.EditMenu, title=u'Edit')
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   182
        parent.Append(menu=self.RunMenu, title=u'Run')
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   183
        parent.Append(menu=self.HelpMenu, title=u'Help')
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   184
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   185
    def _init_utils(self):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   186
        self.menuBar1 = wx.MenuBar()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   187
        self.FileMenu = wx.Menu(title=u'')
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   188
        self.EditMenu = wx.Menu(title=u'')
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   189
        self.RunMenu = wx.Menu(title=u'')
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   190
        self.HelpMenu = wx.Menu(title=u'')
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   191
        
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   192
        self._init_coll_menuBar1_Menus(self.menuBar1)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   193
        self._init_coll_FileMenu_Items(self.FileMenu)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   194
        self._init_coll_EditMenu_Items(self.EditMenu)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   195
        self._init_coll_RunMenu_Items(self.RunMenu)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   196
        self._init_coll_HelpMenu_Items(self.HelpMenu)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   197
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   198
    def _init_coll_MainGridSizer_Items(self, parent):
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   199
        parent.AddSizer(self.ControlPanelSizer, 0, border=0, flag=wx.GROW)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   200
        parent.AddWindow(self.LogConsole, 0, border=0, flag=wx.GROW)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   201
        
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   202
    def _init_coll_MainGridSizer_Growables(self, parent):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   203
        parent.AddGrowableCol(0)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   204
        parent.AddGrowableRow(1)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   205
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   206
    def _init_coll_ControlPanelSizer_Items(self, parent):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   207
        parent.AddSizer(self.ControlButtonSizer, 0, border=0, flag=0)
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   208
        parent.AddWindow(self.BusList, 0, border=0, flag=wx.GROW)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   209
        parent.AddSizer(self.BusButtonSizer, 0, border=0, flag=0)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   210
        
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   211
        
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   212
    def _init_coll_ControlPanelSizer_Growables(self, parent):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   213
        parent.AddGrowableCol(1)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   214
        parent.AddGrowableRow(0)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   215
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   216
    def _init_coll_ControlButtonSizer_Items(self, parent):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   217
        parent.AddWindow(self.EditPLCButton, 0, border=0, flag=0)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   218
        parent.AddWindow(self.BuildButton, 0, border=0, flag=0)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   219
        parent.AddWindow(self.SimulateButton, 0, border=0, flag=0)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   220
        parent.AddWindow(self.RunButton, 0, border=0, flag=0)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   221
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   222
    def _init_coll_BusButtonSizer_Items(self, parent):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   223
        parent.AddWindow(self.AddBusButton, 0, border=0, flag=0)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   224
        parent.AddWindow(self.DeleteBusButton, 0, border=0, flag=0)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   225
        
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   226
    def _init_sizers(self):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   227
        self.MainGridSizer = wx.FlexGridSizer(cols=1, hgap=2, rows=2, vgap=2)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   228
        self.ControlPanelSizer = wx.FlexGridSizer(cols=3, hgap=2, rows=1, vgap=2)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   229
        self.ControlButtonSizer = wx.GridSizer(cols=2, hgap=2, rows=2, vgap=2)
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   230
        self.BusButtonSizer = wx.BoxSizer(wx.VERTICAL)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   231
        
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   232
        self._init_coll_MainGridSizer_Growables(self.MainGridSizer)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   233
        self._init_coll_MainGridSizer_Items(self.MainGridSizer)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   234
        self._init_coll_ControlPanelSizer_Growables(self.ControlPanelSizer)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   235
        self._init_coll_ControlPanelSizer_Items(self.ControlPanelSizer)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   236
        self._init_coll_ControlButtonSizer_Items(self.ControlButtonSizer)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   237
        self._init_coll_BusButtonSizer_Items(self.BusButtonSizer)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   238
        
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   239
        self.SetSizer(self.MainGridSizer)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   240
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   241
    def _init_ctrls(self, prnt):
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   242
        wx.Frame.__init__(self, id=ID_BEREMIZ, name=u'Beremiz',
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   243
              parent=prnt, pos=wx.Point(0, 0), size=wx.Size(600, 300),
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   244
              style=wx.DEFAULT_FRAME_STYLE, title=u'Beremiz')
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   245
        self._init_utils()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   246
        self.SetClientSize(wx.Size(600, 300))
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   247
        self.SetMenuBar(self.menuBar1)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   248
        
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   249
        self.LogConsole = wx.TextCtrl(id=ID_BEREMIZLOGCONSOLE, value='',
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   250
              name='LogConsole', parent=self, pos=wx.Point(0, 0),
7
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   251
              size=wx.Size(0, 0), style=wx.TE_MULTILINE|wx.TE_RICH2)
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   252
        
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   253
        self.EditPLCButton = wx.Button(id=ID_BEREMIZEDITPLCBUTTON, label='Edit\nPLC',
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   254
              name='EditPLCButton', parent=self, pos=wx.Point(0, 0),
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   255
              size=wx.Size(48, 48), style=0)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   256
        self.EditPLCButton.Bind(wx.EVT_BUTTON, self.OnEditPLCButton,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   257
              id=ID_BEREMIZEDITPLCBUTTON)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   258
        
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   259
        self.BuildButton = wx.Button(id=ID_BEREMIZBUILDBUTTON, label='Build',
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   260
              name='BuildButton', parent=self, pos=wx.Point(0, 0),
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   261
              size=wx.Size(48, 48), style=0)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   262
        self.BuildButton.Bind(wx.EVT_BUTTON, self.OnBuildButton,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   263
              id=ID_BEREMIZBUILDBUTTON)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   264
        
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   265
        self.SimulateButton = wx.Button(id=ID_BEREMIZSIMULATEBUTTON, label='Simul',
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   266
              name='SimulateButton', parent=self, pos=wx.Point(0, 0),
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   267
              size=wx.Size(48, 48), style=0)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   268
        self.EditPLCButton.Bind(wx.EVT_BUTTON, self.OnSimulateButton,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   269
              id=ID_BEREMIZSIMULATEBUTTON)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   270
        
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   271
        self.RunButton = wx.Button(id=ID_BEREMIZRUNBUTTON, label='Run',
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   272
              name='RunButton', parent=self, pos=wx.Point(0, 0),
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   273
              size=wx.Size(48, 48), style=0)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   274
        self.RunButton.Bind(wx.EVT_BUTTON, self.OnRunButton,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   275
              id=ID_BEREMIZRUNBUTTON)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   276
        
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   277
        self.BusList = wx.ListBox(choices=[], id=ID_BEREMIZBUSLIST,
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   278
              name='BusList', parent=self, pos=wx.Point(0, 0),
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   279
              size=wx.Size(-1, -1), style=wx.LB_SINGLE|wx.LB_NEEDED_SB)
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   280
        self.BusList.Bind(wx.EVT_LISTBOX_DCLICK, self.OnBusListDClick,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   281
              id=ID_BEREMIZBUSLIST)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   282
        
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   283
        self.AddBusButton = wx.Button(id=ID_BEREMIZADDBUSBUTTON, label='Add',
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   284
              name='AddBusButton', parent=self, pos=wx.Point(0, 0),
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   285
              size=wx.Size(48, 48), style=0)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   286
        self.AddBusButton.Bind(wx.EVT_BUTTON, self.OnAddBusButton,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   287
              id=ID_BEREMIZADDBUSBUTTON)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   288
        
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   289
        self.DeleteBusButton = wx.Button(id=ID_BEREMIZDELETEBUSBUTTON, label='Delete',
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   290
              name='DeleteBusButton', parent=self, pos=wx.Point(0, 0),
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   291
              size=wx.Size(48, 48), style=0)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   292
        self.DeleteBusButton.Bind(wx.EVT_BUTTON, self.OnDeleteBusButton,
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   293
              id=ID_BEREMIZDELETEBUSBUTTON)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   294
        
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   295
        self._init_sizers()
13
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   296
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   297
    def __init__(self, parent, projectOpen):
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   298
        self._init_ctrls(parent)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   299
        
11
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   300
        for name in plugins.__all__:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   301
            module = getattr(plugins, name)
12
a1f9e514f708 plugin framework organization being defined
etisserant
parents: 11
diff changeset
   302
            
a1f9e514f708 plugin framework organization being defined
etisserant
parents: 11
diff changeset
   303
			#AddPlugin(module.GetBlockGenerationFunction(self))
11
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   304
        
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   305
        self.CurrentProjectPath = ""
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   306
        
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   307
        self.PLCManager = None
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   308
        self.PLCEditor = None
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   309
        self.BusManagers = {}
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   310
        
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   311
        self.Log = LogPseudoFile(self.LogConsole)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   312
        
6
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   313
        if projectOpen:
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   314
            self.OpenProject(projectOpen)
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   315
            
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   316
        self.RefreshButtons()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   317
        self.RefreshMainMenu()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   318
        
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   319
    def RefreshButtons(self):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   320
        if self.CurrentProjectPath == "":
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   321
            self.LogConsole.Enable(False)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   322
            self.EditPLCButton.Enable(False)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   323
            self.BuildButton.Enable(False)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   324
            self.SimulateButton.Enable(False)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   325
            self.RunButton.Enable(False)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   326
            self.BusList.Enable(False)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   327
            self.AddBusButton.Enable(False)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   328
            self.DeleteBusButton.Enable(False)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   329
        else:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   330
            self.LogConsole.Enable(True)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   331
            self.EditPLCButton.Enable(True)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   332
            self.BuildButton.Enable(True)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   333
            self.SimulateButton.Enable(True)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   334
            self.RunButton.Enable(True)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   335
            self.BusList.Enable(True)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   336
            self.AddBusButton.Enable(True)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   337
            self.DeleteBusButton.Enable(True)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   338
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   339
    def RefreshBusList(self):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   340
        selected = self.BusList.GetStringSelection()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   341
        self.BusList.Clear()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   342
        busidlist = self.BusManagers.keys()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   343
        busidlist.sort()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   344
        for id in busidlist:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   345
            bus_infos = self.BusManagers[id]
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   346
            self.BusList.Append("0x%2.2X\t%s\t%s"%(id, bus_infos["Type"], bus_infos["Name"]))
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   347
        if selected != "":
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   348
            self.BusList.SetStringSelection(selected)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   349
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   350
    def RefreshMainMenu(self):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   351
        if self.menuBar1:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   352
            if self.CurrentProjectPath == "":
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   353
                self.menuBar1.EnableTop(1, False)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   354
                self.menuBar1.EnableTop(2, False)
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   355
                self.FileMenu.Enable(ID_BEREMIZFILEMENUITEMS2, False)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   356
                self.FileMenu.Enable(ID_BEREMIZFILEMENUITEMS3, False)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   357
                self.FileMenu.Enable(ID_BEREMIZFILEMENUITEMS5, False)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   358
            else:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   359
                self.menuBar1.EnableTop(1, True)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   360
                self.menuBar1.EnableTop(2, True)
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   361
                self.FileMenu.Enable(ID_BEREMIZFILEMENUITEMS2, True)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   362
                self.FileMenu.Enable(ID_BEREMIZFILEMENUITEMS3, True)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   363
                self.FileMenu.Enable(ID_BEREMIZFILEMENUITEMS5, True)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   364
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   365
    def OnNewProjectMenu(self, event):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   366
        if self.CurrentProjectPath != "":
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   367
            defaultpath = self.CurrentProjectPath
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   368
        else:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   369
            defaultpath = os.getcwd()
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   370
        dialog = wx.DirDialog(self , "Choose a project", defaultpath, wx.DD_NEW_DIR_BUTTON)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   371
        if dialog.ShowModal() == wx.ID_OK:
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   372
            projectpath = dialog.GetPath()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   373
            dialog.Destroy()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   374
            if os.path.isdir(projectpath) and len(os.listdir(projectpath)) == 0:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   375
                os.mkdir(os.path.join(projectpath, "eds"))
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   376
                self.PLCManager = PLCControler()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   377
                plc_file = os.path.join(projectpath, "plc.xml")
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   378
                dialog = ProjectDialog(self)
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   379
                if dialog.ShowModal() == wx.ID_OK:
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   380
                    values = dialog.GetValues()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   381
                    values["creationDateTime"] = datetime(*localtime()[:6])
8
56bae4ff53c4 Bug on new project fixed
lbessard
parents: 7
diff changeset
   382
                    self.PLCManager.CreateNewProject(values.pop("projectName"))
56bae4ff53c4 Bug on new project fixed
lbessard
parents: 7
diff changeset
   383
                    self.PLCManager.SetProjectProperties(properties=values)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   384
                    self.PLCManager.SaveXMLFile(plc_file)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   385
                    self.CurrentProjectPath = projectpath
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   386
                dialog.Destroy()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   387
                self.RefreshButtons()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   388
                self.RefreshMainMenu()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   389
            else:
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   390
                message = wx.MessageDialog(self, "Folder choosen isn't empty. You can't use it for a new project!", "ERROR", wx.OK|wx.ICON_ERROR)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   391
                message.ShowModal()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   392
                message.Destroy()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   393
        event.Skip()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   394
    
6
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   395
    def OpenProject(self, projectpath):
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   396
        try:
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   397
            if not os.path.isdir(projectpath):
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   398
                raise Exception
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   399
            self.BusManagers = {}
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   400
            configpath = os.path.join(projectpath, ".project")
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   401
            if not os.path.isfile(configpath):
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   402
                raise Exception
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   403
            file = open(configpath, "r")
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   404
            lines = [line.strip() for line in file.readlines() if line.strip() != ""]
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   405
            if lines[0] != "Beremiz":
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   406
                file.close()
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   407
                raise Exception
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   408
            for bus_id, bus_type, bus_name in [line.split(" ") for line in lines[1:]]:
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   409
                id = int(bus_id, 16)
11
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   410
                controller = getattr(plugins, bus_type).controller
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   411
                if controller != None:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   412
                    manager = controller()
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   413
                    result = manager.LoadProject(projectpath, bus_name)
6
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   414
                    if not result:
11
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   415
                        self.BusManagers[id] = {"Name" : bus_name, "Type" : bus_type, "Manager" : manager, "Editor" : None}
6
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   416
                    else:
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   417
                        message = wx.MessageDialog(self, result, "Error", wx.OK|wx.ICON_ERROR)
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   418
                        message.ShowModal()
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   419
                        message.Destroy()
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   420
                else:
11
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   421
                    self.BusManagers[id] = {"Name" : bus_name, "Type" : bus_type, "Manager" : None, "Editor" : None}
6
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   422
            file.close()
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   423
            self.PLCManager = PLCControler()
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   424
            plc_file = os.path.join(projectpath, "plc.xml")
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   425
            if os.path.isfile(plc_file):
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   426
                self.PLCManager.OpenXMLFile(plc_file)
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   427
                self.CurrentProjectPath = projectpath
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   428
            else:
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   429
                dialog = ProjectDialog(self)
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   430
                if dialog.ShowModal() == wx.ID_OK:
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   431
                    values = dialog.GetValues()
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   432
                    projectname = values.pop("projectName")
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   433
                    values["creationDateTime"] = datetime(*localtime()[:6])
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   434
                    self.PLCManager.CreateNewProject(projectname)
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   435
                    self.PLCManager.SetProjectProperties(values)
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   436
                    self.PLCManager.SaveXMLFile(plc_file)
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   437
                    self.CurrentProjectPath = projectpath
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   438
                dialog.Destroy()
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   439
            self.RefreshBusList()
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   440
            self.RefreshButtons()
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   441
            self.RefreshMainMenu()
11
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   442
        except Exception, message:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   443
            message = wx.MessageDialog(self, "\"%s\" folder is not a valid Beremiz project\n%s"%(projectpath,message), "Error", wx.OK|wx.ICON_ERROR)
6
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   444
            message.ShowModal()
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   445
            message.Destroy()
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   446
    
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   447
    def OnOpenProjectMenu(self, event):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   448
        if self.CurrentProjectPath != "":
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   449
            defaultpath = self.CurrentProjectPath
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   450
        else:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   451
            defaultpath = os.getcwd()
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   452
        dialog = wx.DirDialog(self , "Choose a project", defaultpath, wx.DD_NEW_DIR_BUTTON)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   453
        if dialog.ShowModal() == wx.ID_OK:
6
97d73990053d Bug on command line fixed
lbessard
parents: 5
diff changeset
   454
            self.OpenProject(dialog.GetPath())
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   455
            dialog.Destroy()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   456
        event.Skip()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   457
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   458
    def OnCloseProjectMenu(self, event):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   459
        self.PLCManager = None
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   460
        self.CurrentProjectPath = projectpath
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   461
        self.RefreshButtons()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   462
        self.RefreshMainMenu()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   463
        event.Skip()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   464
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   465
    def OnSaveProjectMenu(self, event):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   466
        self.PLCManager.SaveXMLFile()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   467
        cpjfilepath = os.path.join(self.CurrentProjectPath, "nodelist.cpj")
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   468
        file = open(cpjfilepath, "w")
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   469
        file.write("")
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   470
        file.close()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   471
        configpath = os.path.join(self.CurrentProjectPath, ".project")
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   472
        file = open(configpath, "w")
3
6d8728efcdec Adding validity verifications on project folders opened
lbessard
parents: 2
diff changeset
   473
        file.write("Beremiz\n")
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   474
        busidlist = self.BusManagers.keys()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   475
        busidlist.sort()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   476
        for id in busidlist:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   477
            bus_infos = self.BusManagers[id]
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   478
            file.write("0x%2.2X %s %s\n"%(id, bus_infos["Type"], bus_infos["Name"]))
11
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   479
            bus_infos["Manager"].SaveProject(bus_infos["Name"])
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   480
        file.close()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   481
        event.Skip()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   482
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   483
    def OnPropertiesMenu(self, event):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   484
        event.Skip()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   485
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   486
    def OnQuitMenu(self, event):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   487
        self.Close()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   488
        event.Skip()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   489
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   490
    def OnEditPLCMenu(self, event):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   491
        self.EditPLC()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   492
        event.Skip()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   493
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   494
    def OnAddBusMenu(self, event):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   495
        self.AddBus()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   496
        event.Skip()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   497
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   498
    def OnDeleteBusMenu(self, event):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   499
        self.DeleteBus()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   500
        event.Skip()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   501
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   502
    def OnBuildMenu(self, event):
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   503
        self.BuildAutom()
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   504
        event.Skip()
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   505
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   506
    def OnSimulateMenu(self, event):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   507
        event.Skip()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   508
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   509
    def OnRunMenu(self, event):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   510
        event.Skip()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   511
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   512
    def OnSaveLogMenu(self, event):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   513
        event.Skip()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   514
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   515
    def OnBeremizMenu(self, event):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   516
        event.Skip()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   517
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   518
    def OnAboutMenu(self, event):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   519
        event.Skip()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   520
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   521
    def OnEditPLCButton(self, event):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   522
        self.EditPLC()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   523
        event.Skip()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   524
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   525
    def OnBuildButton(self, event):
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   526
        self.BuildAutom()
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   527
        event.Skip()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   528
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   529
    def OnSimulateButton(self, event):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   530
        event.Skip()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   531
        
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   532
    def OnRunButton(self, event):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   533
        event.Skip()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   534
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   535
    def OnAddBusButton(self, event):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   536
        self.AddBus()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   537
        event.Skip()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   538
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   539
    def OnDeleteBusButton(self, event):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   540
        self.DeleteBus()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   541
        event.Skip()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   542
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   543
    def OnBusListDClick(self, event):
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   544
        selected = event.GetSelection()
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   545
        busidlist = self.BusManagers.keys()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   546
        busidlist.sort()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   547
        bus_infos = self.BusManagers[busidlist[selected]]
11
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   548
        view = getattr(plugins, bus_infos["Type"]).view
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   549
        if view != None:
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   550
            if bus_infos["Editor"] == None:
11
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   551
                editor = view(self, bus_infos["Manager"])
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   552
                editor.SetBusId(busidlist[selected])
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   553
                editor.Show()
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   554
                bus_infos["Editor"] = editor
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   555
        event.Skip()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   556
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   557
    def CloseEditor(self, bus_id):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   558
        if self.BusManagers.get(bus_id, None) != None:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   559
            self.BusManagers[bus_id]["Editor"] = None
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   560
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   561
    def AddBus(self):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   562
        dialog = AddBusDialog(self)
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   563
        if dialog.ShowModal() == wx.ID_OK:
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   564
            values = dialog.GetValues()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   565
            if values["busID"].startswith("0x"):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   566
                bus_id = int(values["busID"], 16)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   567
            else:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   568
                bus_id = int(values["busID"])
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   569
            if self.BusManagers.get(bus_id, None) == None:
11
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   570
                controller = getattr(plugins, values["busType"]).controller
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   571
                if controller != None:
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   572
                    manager = controller()
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   573
                    result = manager.LoadProject(self.CurrentProjectPath, values["busName"])
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   574
                    if not result:
11
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   575
                        self.BusManagers[bus_id] = {"Name" : values["busName"], "Type" : values["busType"], "Manager" : manager, "Editor" : None}
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   576
                    else:
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   577
                        message = wx.MessageDialog(self, result, "Error", wx.OK|wx.ICON_ERROR)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   578
                        message.ShowModal()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   579
                        message.Destroy()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   580
                else:
11
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   581
                    self.BusManagers[bus_id] = {"Name" : values["busName"], "Type" : values["busType"], "Manager" : None, "Editor" : None}
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   582
            else:
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   583
                message = wx.MessageDialog(self, "The bus ID \"0x%2.2X\" is already used!"%bus_id, "Error", wx.OK|wx.ICON_ERROR)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   584
                message.ShowModal()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   585
                message.Destroy()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   586
            self.RefreshBusList()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   587
        dialog.Destroy()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   588
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   589
    def DeleteBus(self):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   590
        busidlist = self.BusManagers.keys()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   591
        busidlist.sort()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   592
        list = ["0x%2.2X\t%s\t%s"%(id, self.BusManagers[id]["Type"], self.BusManagers[id]["Name"]) for id in busidlist]
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   593
        dialog = wx.SingleChoiceDialog(self, "Select Bus to delete:", "Bus Delete", list, wx.OK|wx.CANCEL)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   594
        if dialog.ShowModal() == wx.ID_OK:
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   595
            selected = dialog.GetSelection()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   596
            editor = self.BusManagers[busidlist[selected]]["Editor"]
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   597
            if editor:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   598
                editor.Close()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   599
            self.BusManagers.pop(busidlist[selected])
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   600
            self.RefreshBusList()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   601
        dialog.Destroy()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   602
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   603
    def EditPLC(self):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   604
        if not self.PLCEditor:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   605
            self.PLCEditor = PLCOpenEditor(self, self.PLCManager)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   606
            self.PLCEditor.RefreshProjectTree()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   607
            self.PLCEditor.RefreshFileMenu()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   608
            self.PLCEditor.RefreshEditMenu()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   609
            self.PLCEditor.RefreshToolBar()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   610
            self.PLCEditor.Show()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   611
7
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   612
    def LogCommand(self, Command, sz_limit = 100):
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   613
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   614
        import os, popen2, fcntl, select, signal
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   615
        
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   616
        child = popen2.Popen3(Command, 1) # capture stdout and stderr from command
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   617
        child.tochild.close()             # don't need to talk to child
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   618
        outfile = child.fromchild 
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   619
        outfd = outfile.fileno()
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   620
        errfile = child.childerr
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   621
        errfd = errfile.fileno()
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   622
        outdata = errdata = ''
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   623
        outeof = erreof = 0
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   624
        outlen = errlen = 0
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   625
        while 1:
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   626
            ready = select.select([outfd,errfd],[],[]) # wait for input
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   627
            if outfd in ready[0]:
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   628
                outchunk = outfile.readline()
12
a1f9e514f708 plugin framework organization being defined
etisserant
parents: 11
diff changeset
   629
                if outchunk == '': outeof = 1 
a1f9e514f708 plugin framework organization being defined
etisserant
parents: 11
diff changeset
   630
                else : outlen += 1
7
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   631
                outdata += outchunk
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   632
                self.Log.write(outchunk)
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   633
            if errfd in ready[0]:
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   634
                errchunk = errfile.readline()
12
a1f9e514f708 plugin framework organization being defined
etisserant
parents: 11
diff changeset
   635
                if errchunk == '': erreof = 1 
a1f9e514f708 plugin framework organization being defined
etisserant
parents: 11
diff changeset
   636
                else : errlen += 1
7
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   637
                errdata += errchunk
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   638
                self.Log.write_warning(errchunk)
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   639
            if outeof and erreof : break
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   640
            if errlen > sz_limit or outlen > sz_limit : 
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   641
                os.kill(child.pid, signal.SIGTERM)
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   642
                self.Log.write_error("Output size reached limit -- killed\n")
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   643
                break
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   644
        err = child.wait()
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   645
        return (err, outdata, errdata)
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   646
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   647
    def BuildAutom(self):
13
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   648
        LOCATED_MODEL = re.compile("__LOCATED_VAR\(([A-Z]*),([_A-Za-z0-9]*)\)")
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   649
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   650
        if self.PLCManager:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   651
            self.TargetDir = os.path.join(self.CurrentProjectPath, "build")
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   652
            if not os.path.exists(self.TargetDir):
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   653
                os.mkdir(self.TargetDir)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   654
            self.Log.flush()
7
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   655
            #sys.stdout = self.Log
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   656
            try:
9
bc29b3bf2a5a Improved error report
etisserant
parents: 8
diff changeset
   657
                self.Log.write("Generating IEC-61131 code...\n")
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   658
                plc_file = os.path.join(self.TargetDir, "plc.st")
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   659
                result = self.PLCManager.GenerateProgram(plc_file)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   660
                if not result:
9
bc29b3bf2a5a Improved error report
etisserant
parents: 8
diff changeset
   661
                    raise Exception, "Error : ST/IL/SFC code generator returned %d"%result
7
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   662
                self.Log.write("Compiling ST Program in to C Program...\n")
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   663
                status, result, err_result = self.LogCommand("%s %s -I %s %s"%(iec2cc_path, plc_file, ieclib_path, self.TargetDir))
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   664
                if status:
9
bc29b3bf2a5a Improved error report
etisserant
parents: 8
diff changeset
   665
                    new_dialog = wx.Frame(None)
bc29b3bf2a5a Improved error report
etisserant
parents: 8
diff changeset
   666
                    ST_viewer = TextViewer(new_dialog, None, None)
bc29b3bf2a5a Improved error report
etisserant
parents: 8
diff changeset
   667
                    #ST_viewer.Enable(False)
bc29b3bf2a5a Improved error report
etisserant
parents: 8
diff changeset
   668
                    ST_viewer.SetKeywords(IEC_KEYWORDS)
bc29b3bf2a5a Improved error report
etisserant
parents: 8
diff changeset
   669
                    ST_viewer.SetText(file(plc_file).read())
bc29b3bf2a5a Improved error report
etisserant
parents: 8
diff changeset
   670
                    new_dialog.Show()
bc29b3bf2a5a Improved error report
etisserant
parents: 8
diff changeset
   671
                    raise Exception, "Error : IEC to C compiler returned %d"%status
12
a1f9e514f708 plugin framework organization being defined
etisserant
parents: 11
diff changeset
   672
                C_files = result.splitlines()
a1f9e514f708 plugin framework organization being defined
etisserant
parents: 11
diff changeset
   673
                C_files.remove("POUS.c")
a1f9e514f708 plugin framework organization being defined
etisserant
parents: 11
diff changeset
   674
                C_files = map(lambda filename:os.path.join(self.TargetDir, filename), C_files)
7
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   675
                self.Log.write("Extracting Located Variables...\n")
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   676
                location_file = open(os.path.join(self.TargetDir,"LOCATED_VARIABLES.h"))
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   677
                locations = []
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   678
                lines = [line.strip() for line in location_file.readlines()]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   679
                for line in lines:
13
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   680
                    result = self.LOCATED_MODEL.match(line)
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   681
                    if result:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   682
                        locations.append(result.groups())
7
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   683
                self.Log.write("Generating Network Configurations...\n")
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   684
                for bus_id, bus_infos in self.BusManagers.items():
12
a1f9e514f708 plugin framework organization being defined
etisserant
parents: 11
diff changeset
   685
                   if bus_infos["Manager"]:
a1f9e514f708 plugin framework organization being defined
etisserant
parents: 11
diff changeset
   686
                        c_filename = "%s.c"%os.path.join(self.TargetDir, gen_cfile.FormatName(bus_infos["Name"]))
a1f9e514f708 plugin framework organization being defined
etisserant
parents: 11
diff changeset
   687
                        result = bus_infos["Manager"].GenerateBus(c_filename, locations)
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   688
                        if result:
12
a1f9e514f708 plugin framework organization being defined
etisserant
parents: 11
diff changeset
   689
                            raise Exception
a1f9e514f708 plugin framework organization being defined
etisserant
parents: 11
diff changeset
   690
                        else:
a1f9e514f708 plugin framework organization being defined
etisserant
parents: 11
diff changeset
   691
                            C_files.append(c_filename)
7
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   692
                self.Log.write("Generating Makefiles...\n")
12
a1f9e514f708 plugin framework organization being defined
etisserant
parents: 11
diff changeset
   693
                self.Log.write(str(C_files))
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   694
                
7
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   695
                self.Log.write("Compiling Project...\n")
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   696
                
7
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   697
                self.Log.write("\nBuild Project completed\n")
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   698
            except Exception, message:
7
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   699
                self.Log.write_error("\nBuild Failed\n")
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   700
                self.Log.write(str(message))
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   701
                pass
7
e20fa7257d41 Added stdout/stderr separation limitation and coloration
etisserant
parents: 6
diff changeset
   702
            #sys.stdout = sys.__stdout__
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents: 0
diff changeset
   703
                
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   704
#-------------------------------------------------------------------------------
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   705
#                             Add Bus Dialog
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   706
#-------------------------------------------------------------------------------
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   707
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   708
[ID_ADDBUSDIALOG, ID_ADDBUSDIALOGBUSID, 
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   709
 ID_ADDBUSDIALOGBUSNAME, ID_ADDBUSDIALOGBUSTYPE, 
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   710
 ID_ADDBUSDIALOGSTATICTEXT1, ID_ADDBUSDIALOGSTATICTEXT2, 
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   711
 ID_ADDBUSDIALOGSTATICTEXT3,
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   712
] = [wx.NewId() for _init_ctrls in range(7)]
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   713
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   714
class AddBusDialog(wx.Dialog):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   715
    def _init_coll_flexGridSizer1_Items(self, parent):
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   716
        parent.AddSizer(self.MainSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   717
        parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   718
        
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   719
    def _init_coll_flexGridSizer1_Growables(self, parent):
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   720
        parent.AddGrowableCol(0)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   721
        parent.AddGrowableRow(0)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   722
    
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   723
    def _init_coll_MainSizer_Items(self, parent):
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   724
        parent.AddWindow(self.staticText1, 0, border=0, flag=wx.GROW)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   725
        parent.AddWindow(self.BusId, 0, border=0, flag=wx.GROW)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   726
        parent.AddWindow(self.staticText2, 0, border=0, flag=wx.GROW)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   727
        parent.AddWindow(self.BusType, 0, border=0, flag=wx.GROW)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   728
        parent.AddWindow(self.staticText3, 0, border=0, flag=wx.GROW)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   729
        parent.AddWindow(self.BusName, 0, border=0, flag=wx.GROW)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   730
        
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   731
    def _init_coll_MainSizer_Growables(self, parent):
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   732
        parent.AddGrowableCol(1)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   733
        
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   734
    def _init_sizers(self):
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   735
        self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   736
        self.MainSizer = wx.FlexGridSizer(cols=2, hgap=0, rows=3, vgap=15)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   737
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   738
        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   739
        self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   740
        self._init_coll_MainSizer_Items(self.MainSizer)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   741
        self._init_coll_MainSizer_Growables(self.MainSizer)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   742
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   743
        self.SetSizer(self.flexGridSizer1)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   744
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   745
    def _init_ctrls(self, prnt):
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   746
        wx.Dialog.__init__(self, id=ID_ADDBUSDIALOG,
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   747
              name='PouDialog', parent=prnt, pos=wx.Point(376, 183),
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   748
              size=wx.Size(300, 200), style=wx.DEFAULT_DIALOG_STYLE,
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   749
              title='Create a new POU')
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   750
        self.SetClientSize(wx.Size(300, 200))
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   751
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   752
        self.staticText1 = wx.StaticText(id=ID_ADDBUSDIALOGSTATICTEXT1,
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   753
              label='Bus ID:', name='staticText1', parent=self,
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   754
              pos=wx.Point(0, 0), size=wx.Size(100, 17), style=0)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   755
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   756
        self.BusId = wx.TextCtrl(id=ID_ADDBUSDIALOGBUSID,
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   757
              name='BusId', parent=self, pos=wx.Point(0, 0), 
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   758
              size=wx.Size(0, 24), style=0)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   759
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   760
        self.staticText2 = wx.StaticText(id=ID_ADDBUSDIALOGSTATICTEXT2,
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   761
              label='Bus Type:', name='staticText2', parent=self,
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   762
              pos=wx.Point(0, 0), size=wx.Size(100, 17), style=0)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   763
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   764
        self.BusType = wx.Choice(id=ID_ADDBUSDIALOGBUSTYPE,
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   765
              name='BusType', parent=self, pos=wx.Point(0, 0),
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   766
              size=wx.Size(0, 24), style=0)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   767
        
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   768
        self.staticText3 = wx.StaticText(id=ID_ADDBUSDIALOGSTATICTEXT3,
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   769
              label='Bus Name:', name='staticText3', parent=self,
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   770
              pos=wx.Point(0, 0), size=wx.Size(100, 17), style=0)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   771
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   772
        self.BusName = wx.TextCtrl(id=ID_ADDBUSDIALOGBUSNAME,
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   773
              name='BusName', parent=self, pos=wx.Point(0, 0),
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   774
              size=wx.Size(0, 24), style=0)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   775
        
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   776
        self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   777
        self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId())
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   778
        
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   779
        self._init_sizers()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   780
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   781
    def __init__(self, parent):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   782
        self._init_ctrls(parent)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   783
        
11
75ae893d5eed Adding plugin support in Beremiz
lbessard
parents: 9
diff changeset
   784
        for option in [""] + plugins.__all__:
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   785
            self.BusType.Append(option)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   786
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   787
    def OnOK(self, event):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   788
        error = []
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   789
        bus_id = self.BusId.GetValue()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   790
        if bus_id == "":
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   791
            error.append("Bus ID")
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   792
        if self.BusType.GetStringSelection() == "":
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   793
            error.append("Bus Type")
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   794
        if self.BusName.GetValue() == "":
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   795
            error.append("Bus Name")
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   796
        if len(error) > 0:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   797
            text = ""
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   798
            for i, item in enumerate(error):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   799
                if i == 0:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   800
                    text += item
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   801
                elif i == len(error) - 1:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   802
                    text += " and %s"%item
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   803
                else:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   804
                    text += ", %s"%item 
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   805
            message = wxMessageDialog(self, "Form isn't complete. %s must be filled!"%text, "Error", wxOK|wxICON_ERROR)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   806
            message.ShowModal()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   807
            message.Destroy()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   808
        elif bus_id.startswith("0x"):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   809
            try:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   810
                bus_id = int(bus_id, 16)
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   811
                self.EndModal(wx.ID_OK)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   812
            except:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   813
                message = wxMessageDialog(self, "Bus ID must be a decimal or hexadecimal number!", "Error", wxOK|wxICON_ERROR)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   814
                message.ShowModal()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   815
                message.Destroy()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   816
        elif not bus_id.startswith("-"):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   817
            try:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   818
                bus_id = int(bus_id)
4
e9d061c23e83 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 3
diff changeset
   819
                self.EndModal(wx.ID_OK)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   820
            except:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   821
                message = wxMessageDialog(self, "Bus ID must be a decimal or hexadecimal number!", "Error", wxOK|wxICON_ERROR)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   822
                message.ShowModal()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   823
                message.Destroy()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   824
        else:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   825
            message = wxMessageDialog(self, "Bus Id must be greater than 0!", "Error", wxOK|wxICON_ERROR)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   826
            message.ShowModal()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   827
            message.Destroy()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   828
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   829
    def SetValues(self, values):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   830
        for item, value in values.items():
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   831
            if item == "busID":
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   832
                self.BusID.SetValue(value)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   833
            elif item == "busType":
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   834
                self.BusType.SetStringSelection(value)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   835
            elif item == "busName":
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   836
                self.BusName.SetValue(value)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   837
                
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   838
    def GetValues(self):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   839
        values = {}
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   840
        values["busID"] = self.BusId.GetValue()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   841
        values["busType"] = self.BusType.GetStringSelection()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   842
        values["busName"] = self.BusName.GetValue()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   843
        return values
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   844
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   845
#-------------------------------------------------------------------------------
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   846
#                               Exception Handler
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   847
#-------------------------------------------------------------------------------
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   848
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   849
Max_Traceback_List_Size = 20
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   850
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   851
def Display_Exception_Dialog(e_type,e_value,e_tb):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   852
    trcbck_lst = []
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   853
    for i,line in enumerate(traceback.extract_tb(e_tb)):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   854
        trcbck = " " + str(i+1) + ". "
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   855
        if line[0].find(os.getcwd()) == -1:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   856
            trcbck += "file : " + str(line[0]) + ",   "
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   857
        else:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   858
            trcbck += "file : " + str(line[0][len(os.getcwd()):]) + ",   "
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   859
        trcbck += "line : " + str(line[1]) + ",   " + "function : " + str(line[2])
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   860
        trcbck_lst.append(trcbck)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   861
        
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   862
    # Allow clicking....
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   863
    cap = wx.Window_GetCapture()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   864
    if cap:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   865
        cap.ReleaseMouse()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   866
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   867
    dlg = wx.SingleChoiceDialog(None, 
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   868
        """
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   869
An error happens.
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   870
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   871
Click on OK for saving an error report.
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   872
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   873
Please contact LOLITech at:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   874
+33 (0)3 29 52 95 67
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   875
bugs_Beremiz@lolitech.fr
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   876
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   877
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   878
Error:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   879
""" +
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   880
        str(e_type) + " : " + str(e_value), 
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   881
        "Error",
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   882
        trcbck_lst)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   883
    try:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   884
        res = (dlg.ShowModal() == wx.ID_OK)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   885
    finally:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   886
        dlg.Destroy()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   887
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   888
    return res
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   889
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   890
def Display_Error_Dialog(e_value):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   891
    message = wxMessageDialog(None, str(e_value), "Error", wxOK|wxICON_ERROR)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   892
    message.ShowModal()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   893
    message.Destroy()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   894
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   895
def get_last_traceback(tb):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   896
    while tb.tb_next:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   897
        tb = tb.tb_next
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   898
    return tb
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   899
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   900
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   901
def format_namespace(d, indent='    '):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   902
    return '\n'.join(['%s%s: %s' % (indent, k, repr(v)[:10000]) for k, v in d.iteritems()])
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   903
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   904
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   905
ignored_exceptions = [] # a problem with a line in a module is only reported once per session
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   906
5
9565bb5facf7 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 4
diff changeset
   907
def AddExceptHook(path, app_version='[No version]'):#, ignored_exceptions=[]):
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   908
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   909
    def handle_exception(e_type, e_value, e_traceback):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   910
        traceback.print_exception(e_type, e_value, e_traceback) # this is very helpful when there's an exception in the rest of this func
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   911
        last_tb = get_last_traceback(e_traceback)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   912
        ex = (last_tb.tb_frame.f_code.co_filename, last_tb.tb_frame.f_lineno)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   913
        if str(e_value).startswith("!!!"):
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   914
            Display_Error_Dialog(e_value)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   915
        elif ex not in ignored_exceptions:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   916
            result = Display_Exception_Dialog(e_type,e_value,e_traceback)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   917
            if result:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   918
                ignored_exceptions.append(ex)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   919
                info = {
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   920
                    'app-title' : wx.GetApp().GetAppName(), # app_title
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   921
                    'app-version' : app_version,
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   922
                    'wx-version' : wx.VERSION_STRING,
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   923
                    'wx-platform' : wx.Platform,
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   924
                    'python-version' : platform.python_version(), #sys.version.split()[0],
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   925
                    'platform' : platform.platform(),
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   926
                    'e-type' : e_type,
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   927
                    'e-value' : e_value,
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   928
                    'date' : time.ctime(),
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   929
                    'cwd' : os.getcwd(),
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   930
                    }
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   931
                if e_traceback:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   932
                    info['traceback'] = ''.join(traceback.format_tb(e_traceback)) + '%s: %s' % (e_type, e_value)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   933
                    last_tb = get_last_traceback(e_traceback)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   934
                    exception_locals = last_tb.tb_frame.f_locals # the locals at the level of the stack trace where the exception actually occurred
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   935
                    info['locals'] = format_namespace(exception_locals)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   936
                    if 'self' in exception_locals:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   937
                        info['self'] = format_namespace(exception_locals['self'].__dict__)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   938
                
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   939
                output = open(path+os.sep+"bug_report_"+info['date'].replace(':','-').replace(' ','_')+".txt",'w')
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   940
                lst = info.keys()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   941
                lst.sort()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   942
                for a in lst:
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   943
                    output.write(a+":\n"+str(info[a])+"\n\n")
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   944
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   945
    #sys.excepthook = lambda *args: wx.CallAfter(handle_exception, *args)
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   946
    sys.excepthook = handle_exception
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   947
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   948
if __name__ == '__main__':
13
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   949
    def usage():
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   950
        print "\nUsage of Beremiz.py :"
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   951
        print "\n   %s [Projectpath]\n"%sys.argv[0]
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   952
    
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   953
    try:
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   954
        opts, args = getopt.getopt(sys.argv[1:], "h", ["help"])
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   955
    except getopt.GetoptError:
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   956
        # print help information and exit:
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   957
        usage()
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   958
        sys.exit(2)
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   959
    
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   960
    for o, a in opts:
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   961
        if o in ("-h", "--help"):
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   962
            usage()
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   963
            sys.exit()
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   964
    
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   965
    if len(args) > 1:
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   966
        usage()
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   967
        sys.exit()
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   968
    elif len(args) == 1:
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   969
        projectOpen = args[0]
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   970
    else:
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   971
        projectOpen = None
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   972
    
5
9565bb5facf7 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 4
diff changeset
   973
    app = wx.PySimpleApp()
9565bb5facf7 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 4
diff changeset
   974
    wx.InitAllImageHandlers()
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   975
    
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   976
    # Install a exception handle for bug reports
5
9565bb5facf7 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 4
diff changeset
   977
    AddExceptHook(os.getcwd(),__version__)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   978
    
13
f1f0edbeb313 More precise design for plugins.... to be continued...
etisserant
parents: 12
diff changeset
   979
    frame = Beremiz(None, projectOpen)
0
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   980
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   981
    frame.Show()
215982c73cd6 First Beremiz commit
lbessard
parents:
diff changeset
   982
    app.MainLoop()