andrej@1661: #!/usr/bin/env python andrej@1661: # -*- coding: utf-8 -*- andrej@1661: andrej@1661: # This file is part of Beremiz, a Integrated Development Environment for andrej@1661: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival. andrej@1661: # andrej@1661: # Copyright (C) 2016 - 2017: Andrey Skvortsov andrej@1661: # andrej@1661: # See COPYING file for copyrights details. andrej@1661: # andrej@1661: # This program is free software; you can redistribute it and/or andrej@1661: # modify it under the terms of the GNU General Public License andrej@1661: # as published by the Free Software Foundation; either version 2 andrej@1661: # of the License, or (at your option) any later version. andrej@1661: # andrej@1661: # This program is distributed in the hope that it will be useful, andrej@1661: # but WITHOUT ANY WARRANTY; without even the implied warranty of andrej@1661: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the andrej@1661: # GNU General Public License for more details. andrej@1661: # andrej@1661: # You should have received a copy of the GNU General Public License andrej@1661: # along with this program; if not, write to the Free Software andrej@1661: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. andrej@1661: andrej@1661: andrej@1661: andrej@1661: import os, sys, getopt andrej@1661: import time andrej@1661: import __builtin__ andrej@1661: andrej@1661: class BeremizIDELauncher: andrej@1661: def __init__(self): andrej@1661: self.updateinfo_url = None andrej@1661: self.extensions = [] andrej@1661: self.app_dir = os.path.dirname(os.path.realpath(__file__)) andrej@1661: self.projectOpen = None andrej@1661: self.buildpath = None andrej@1661: self.splash = None andrej@1661: self.splashPath = self.Bpath("images", "splash.png") andrej@1661: andrej@1661: def Bpath(self, *args): andrej@1661: return os.path.join(self.app_dir,*args) andrej@1661: andrej@1661: def ShowSplashScreen(self): andrej@1661: from wx.lib.agw.advancedsplash import AdvancedSplash andrej@1661: bmp = wx.Image(self.splashPath).ConvertToBitmap() andrej@1661: self.splash = AdvancedSplash(None, bitmap=bmp) andrej@1661: andrej@1661: # process all events andrej@1661: # even the events generated by splash themself during showing andrej@1661: if wx.Platform == '__WXMSW__': andrej@1661: self.splash.Show() andrej@1661: self.splash.ProcessEvent(wx.PaintEvent()) andrej@1661: else: andrej@1661: for i in range(0,30): andrej@1661: wx.Yield() andrej@1661: time.sleep(0.01); andrej@1661: andrej@1661: andrej@1661: def Usage(self): andrej@1661: print "\nUsage:" andrej@1661: print "\n %s [Projectpath] [Buildpath]\n"%sys.argv[0] andrej@1661: andrej@1661: def SetCmdOptions(self): andrej@1661: self.shortCmdOpts = "hu:e:" andrej@1661: self.longCmdOpts = ["help", "updatecheck=", "extend="] andrej@1661: andrej@1661: def ProcessOption(self, o, a): andrej@1661: if o in ("-h", "--help"): andrej@1661: self.Usage() andrej@1661: sys.exit() andrej@1661: if o in ("-u", "--updatecheck"): andrej@1661: self.updateinfo_url = a andrej@1661: if o in ("-e", "--extend"): andrej@1661: self.extensions.append(a) andrej@1661: andrej@1661: def ProcessCommandLineArgs(self): andrej@1661: self.SetCmdOptions() andrej@1661: try: andrej@1661: opts, args = getopt.getopt(sys.argv[1:], self.shortCmdOpts, self.longCmdOpts) andrej@1661: except getopt.GetoptError: andrej@1661: # print help information and exit: andrej@1661: self.Usage() andrej@1661: sys.exit(2) andrej@1661: andrej@1661: for o, a in opts: andrej@1661: self.ProcessOption(o, a) andrej@1661: andrej@1661: if len(args) > 2: andrej@1661: self.Usage() andrej@1661: sys.exit() andrej@1661: andrej@1661: elif len(args) == 1: andrej@1661: self.projectOpen = args[0] andrej@1661: self.buildpath = None andrej@1661: elif len(args) == 2: andrej@1661: self.projectOpen = args[0] andrej@1661: self.buildpath = args[1] andrej@1661: andrej@1661: def CreateApplication(self): andrej@1661: if os.path.exists("BEREMIZ_DEBUG"): andrej@1661: __builtin__.__dict__["BMZ_DBG"] = True andrej@1661: else : andrej@1661: __builtin__.__dict__["BMZ_DBG"] = False andrej@1661: andrej@1661: global wxversion, wx andrej@1661: import wxversion andrej@1661: wxversion.select(['2.8', '3.0']) andrej@1661: import wx andrej@1661: andrej@1661: if wx.VERSION >= (3, 0, 0): andrej@1661: self.app = wx.App(redirect=BMZ_DBG) andrej@1661: else: andrej@1661: self.app = wx.PySimpleApp(redirect=BMZ_DBG) andrej@1661: andrej@1661: self.app.SetAppName('beremiz') andrej@1661: if wx.VERSION < (3, 0, 0): andrej@1661: wx.InitAllImageHandlers() andrej@1661: andrej@1661: self.ShowSplashScreen() andrej@1661: self.BackgroundInitialization() andrej@1661: self.app.MainLoop() andrej@1661: andrej@1661: def BackgroundInitialization(self): andrej@1661: self.InitI18n() andrej@1661: self.CheckUpdates() andrej@1661: self.LoadExtensions() andrej@1661: self.ImportModules() andrej@1661: self.InstallExceptionHandler() andrej@1661: self.ShowUI() andrej@1661: andrej@1661: def InitI18n(self): andrej@1661: from util.misc import InstallLocalRessources andrej@1661: InstallLocalRessources(self.app_dir) andrej@1661: andrej@1661: def LoadExtensions(self): andrej@1661: for extfilename in self.extensions: andrej@1661: from util.TranslationCatalogs import AddCatalog andrej@1661: from util.BitmapLibrary import AddBitmapFolder andrej@1661: extension_folder = os.path.split(os.path.realpath(extfilename))[0] andrej@1661: sys.path.append(extension_folder) andrej@1661: AddCatalog(os.path.join(extension_folder, "locale")) andrej@1661: AddBitmapFolder(os.path.join(extension_folder, "images")) andrej@1661: execfile(extfilename, locals()) andrej@1661: andrej@1661: def CheckUpdates(self): andrej@1661: if self.updateinfo_url is not None: andrej@1661: updateinfo = _("Fetching %s") % self.updateinfo_url andrej@1661: andrej@1661: def updateinfoproc(): andrej@1661: global updateinfo andrej@1661: try : andrej@1661: import urllib2 andrej@1661: updateinfo = urllib2.urlopen(self.updateinfo_url,None).read() andrej@1661: except : andrej@1661: updateinfo = _("update info unavailable.") andrej@1661: andrej@1661: from threading import Thread andrej@1661: self.splash.SetText(text=updateinfo) andrej@1661: updateinfoThread = Thread(target=updateinfoproc) andrej@1661: updateinfoThread.start() andrej@1661: updateinfoThread.join(2) andrej@1661: self.splash.SetText(text=updateinfo) andrej@1661: andrej@1661: def ImportModules(self): andrej@1661: global Beremiz andrej@1661: import Beremiz andrej@1661: andrej@1661: def InstallExceptionHandler(self): andrej@1661: import version andrej@1661: import tempfile andrej@1661: logpath = tempfile.gettempdir()+os.sep+'Beremiz' andrej@1661: Beremiz.AddExceptHook(logpath,version.app_version) andrej@1661: andrej@1661: def ShowUI(self): andrej@1661: self.frame = Beremiz.Beremiz(None, self.projectOpen, self.buildpath) andrej@1661: if self.splash: andrej@1661: self.splash.Close() andrej@1661: self.frame.Show() andrej@1661: andrej@1661: def Start(self): andrej@1661: self.ProcessCommandLineArgs() andrej@1661: self.CreateApplication() andrej@1661: andrej@1661: if __name__ == '__main__': andrej@1661: beremiz = BeremizIDELauncher() andrej@1661: beremiz.Start()