BeremizIDELauncher.py
changeset 1661 78f28f40bc10
child 1662 8816f7316d9c
equal deleted inserted replaced
1659:aec0ed4b6f39 1661:78f28f40bc10
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 # This file is part of Beremiz, a Integrated Development Environment for
       
     5 # programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
       
     6 #
       
     7 # Copyright (C) 2016 - 2017: Andrey Skvortsov <andrej.skvortzov@gmail.com>
       
     8 #
       
     9 # See COPYING file for copyrights details.
       
    10 #
       
    11 # This program is free software; you can redistribute it and/or
       
    12 # modify it under the terms of the GNU General Public License
       
    13 # as published by the Free Software Foundation; either version 2
       
    14 # of the License, or (at your option) any later version.
       
    15 #
       
    16 # This program is distributed in the hope that it will be useful,
       
    17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    19 # GNU General Public License for more details.
       
    20 #
       
    21 # You should have received a copy of the GNU General Public License
       
    22 # along with this program; if not, write to the Free Software
       
    23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
       
    24 
       
    25 
       
    26 
       
    27 import os, sys, getopt
       
    28 import time
       
    29 import __builtin__
       
    30 
       
    31 class BeremizIDELauncher:
       
    32     def __init__(self):
       
    33         self.updateinfo_url = None
       
    34         self.extensions = []
       
    35         self.app_dir = os.path.dirname(os.path.realpath(__file__))
       
    36         self.projectOpen = None
       
    37         self.buildpath = None
       
    38         self.splash = None
       
    39         self.splashPath = self.Bpath("images", "splash.png")
       
    40 
       
    41     def Bpath(self, *args):
       
    42         return os.path.join(self.app_dir,*args)
       
    43 
       
    44     def ShowSplashScreen(self):
       
    45         from wx.lib.agw.advancedsplash import AdvancedSplash
       
    46         bmp = wx.Image(self.splashPath).ConvertToBitmap()
       
    47         self.splash = AdvancedSplash(None, bitmap=bmp)
       
    48 
       
    49         # process all events
       
    50         # even the events generated by splash themself during showing
       
    51         if wx.Platform == '__WXMSW__':
       
    52             self.splash.Show()
       
    53             self.splash.ProcessEvent(wx.PaintEvent())
       
    54         else:
       
    55             for i in range(0,30):
       
    56                 wx.Yield()
       
    57                 time.sleep(0.01);
       
    58 
       
    59 
       
    60     def Usage(self):
       
    61         print "\nUsage:"
       
    62         print "\n   %s [Projectpath] [Buildpath]\n"%sys.argv[0]
       
    63 
       
    64     def SetCmdOptions(self):
       
    65         self.shortCmdOpts = "hu:e:"
       
    66         self.longCmdOpts = ["help", "updatecheck=", "extend="]
       
    67 
       
    68     def ProcessOption(self, o, a):
       
    69         if o in ("-h", "--help"):
       
    70             self.Usage()
       
    71             sys.exit()
       
    72         if o in ("-u", "--updatecheck"):
       
    73             self.updateinfo_url = a
       
    74         if o in ("-e", "--extend"):
       
    75             self.extensions.append(a)
       
    76 
       
    77     def ProcessCommandLineArgs(self):
       
    78         self.SetCmdOptions()
       
    79         try:
       
    80             opts, args = getopt.getopt(sys.argv[1:], self.shortCmdOpts, self.longCmdOpts)
       
    81         except getopt.GetoptError:
       
    82             # print help information and exit:
       
    83             self.Usage()
       
    84             sys.exit(2)
       
    85 
       
    86         for o, a in opts:
       
    87             self.ProcessOption(o, a)
       
    88 
       
    89         if len(args) > 2:
       
    90             self.Usage()
       
    91             sys.exit()
       
    92 
       
    93         elif len(args) == 1:
       
    94             self.projectOpen = args[0]
       
    95             self.buildpath = None
       
    96         elif len(args) == 2:
       
    97             self.projectOpen = args[0]
       
    98             self.buildpath = args[1]
       
    99 
       
   100     def CreateApplication(self):
       
   101         if os.path.exists("BEREMIZ_DEBUG"):
       
   102             __builtin__.__dict__["BMZ_DBG"] = True
       
   103         else :
       
   104             __builtin__.__dict__["BMZ_DBG"] = False
       
   105 
       
   106         global wxversion, wx
       
   107         import wxversion
       
   108         wxversion.select(['2.8', '3.0'])
       
   109         import wx
       
   110 
       
   111         if wx.VERSION >= (3, 0, 0):
       
   112             self.app = wx.App(redirect=BMZ_DBG)
       
   113         else:
       
   114             self.app = wx.PySimpleApp(redirect=BMZ_DBG)
       
   115 
       
   116         self.app.SetAppName('beremiz')
       
   117         if wx.VERSION < (3, 0, 0):
       
   118             wx.InitAllImageHandlers()
       
   119 
       
   120         self.ShowSplashScreen()
       
   121         self.BackgroundInitialization()
       
   122         self.app.MainLoop()
       
   123 
       
   124     def BackgroundInitialization(self):
       
   125         self.InitI18n()
       
   126         self.CheckUpdates()
       
   127         self.LoadExtensions()
       
   128         self.ImportModules()
       
   129         self.InstallExceptionHandler()
       
   130         self.ShowUI()
       
   131 
       
   132     def InitI18n(self):
       
   133         from util.misc import InstallLocalRessources
       
   134         InstallLocalRessources(self.app_dir)
       
   135 
       
   136     def LoadExtensions(self):
       
   137         for extfilename in self.extensions:
       
   138             from util.TranslationCatalogs import AddCatalog
       
   139             from util.BitmapLibrary import AddBitmapFolder
       
   140             extension_folder = os.path.split(os.path.realpath(extfilename))[0]
       
   141             sys.path.append(extension_folder)
       
   142             AddCatalog(os.path.join(extension_folder, "locale"))
       
   143             AddBitmapFolder(os.path.join(extension_folder, "images"))
       
   144             execfile(extfilename, locals())
       
   145 
       
   146     def CheckUpdates(self):
       
   147         if self.updateinfo_url is not None:
       
   148             updateinfo = _("Fetching %s") % self.updateinfo_url
       
   149 
       
   150             def updateinfoproc():
       
   151                 global updateinfo
       
   152                 try :
       
   153                     import urllib2
       
   154                     updateinfo = urllib2.urlopen(self.updateinfo_url,None).read()
       
   155                 except :
       
   156                     updateinfo = _("update info unavailable.")
       
   157 
       
   158             from threading import Thread
       
   159             self.splash.SetText(text=updateinfo)
       
   160             updateinfoThread = Thread(target=updateinfoproc)
       
   161             updateinfoThread.start()
       
   162             updateinfoThread.join(2)
       
   163             self.splash.SetText(text=updateinfo)
       
   164 
       
   165     def ImportModules(self):
       
   166         global Beremiz
       
   167         import Beremiz
       
   168 
       
   169     def InstallExceptionHandler(self):
       
   170         import version
       
   171         import tempfile
       
   172         logpath = tempfile.gettempdir()+os.sep+'Beremiz'
       
   173         Beremiz.AddExceptHook(logpath,version.app_version)
       
   174 
       
   175     def ShowUI(self):
       
   176         self.frame = Beremiz.Beremiz(None, self.projectOpen, self.buildpath)
       
   177         if self.splash:
       
   178             self.splash.Close()
       
   179         self.frame.Show()
       
   180 
       
   181     def Start(self):
       
   182         self.ProcessCommandLineArgs()
       
   183         self.CreateApplication()
       
   184 
       
   185 if __name__ == '__main__':
       
   186     beremiz = BeremizIDELauncher()
       
   187     beremiz.Start()