""" This file is part of RuneOptimizer. RuneOptimizer is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. RuneOptimizer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with RuneOptimizer. If not, see . """ class DialogUpdateJson(wx.Dialog): """ The JSON data updater dialog. Parameters ---------- formSizer : wx.BoxSizer Sizer with the file selector and options. progressSizer : wx.BoxSizer Sizer with the progress image. Hidden on class creation. messageSizer : wx.BoxSizer Sizer with the progress message box. Hidden on class creation. buttonSizer : wx.BoxSizer Contains the accept button. fileSelector : wxFilePickerCtrl. File selector starsCheck : wx.Checkbox. Checkbox to indicate to save only units with 6 stars. runesCheck : wx.Checkbox. Checkbox to indicate to save only units with runes. teamsCheck : wx.Checkbox. Checkbox to indicate team deletion. updateDone : boolean Indicates if the update process has been run (default False) updateError : boolean Indicates if the update process has been run with errors (default False) messageLabel : wx.StaticText Label wich shows the output of the update process. timer : wx.Timer Timer to check the update process for new output to display. process : wx.Process The update process. Methods ------- checkProcess(event) Checks the update process for new output. updateComplete(event) Called at process end, cleans the dialog and set status variables. accept(event) Checks from accept button. Starts the update process. """ formSizer = None progressSizer = None messageSizer = None buttonSizer = None fileSelector = None starsCheck = None runesCheck = None teamsCheck = None updateDone = False updateError = False messageLabel = None timer = None process = None process = None def __init__(self, parent, id=wx.ID_ANY,): """Initializes the panel. Sets upt all the widgets. Parameters ---------- parent : wx.* Dialog parent. id : int, optional ID for the dialig (default wx.ID_ANY). """ # Parent constructor wx.Dialog.__init__( self, parent=parent, id=id, pos=(50, 50), size=(400, 290), title="Update from JSON file", style=wx.DEFAULT_DIALOG_STYLE ) self.formSizer = wx.BoxSizer(wx.VERTICAL) self.progressSizer = wx.BoxSizer(wx.VERTICAL) self.messageSizer = wx.BoxSizer(wx.VERTICAL) self.buttonSizer = wx.BoxSizer(wx.VERTICAL) fileSelectorLabel = wx.StaticText( parent=self, id=wx.ID_ANY, label="Select a JSON file", pos=(30, 30), size=(130, 30) ) self.formSizer.Add(fileSelectorLabel) self.fileSelector = wx.FilePickerCtrl(parent=self, id=wx.ID_ANY, path="", message="Select JSON file", wildcard="JSON files (*.json)|*.json", style=wx.FC_DEFAULT_STYLE, pos=(140, 20), size=(250, 40) ) self.formSizer.Add(self.fileSelector) self.starsCheck = wx.CheckBox( parent=self, id=wx.ID_ANY, label="Only import units at with 6 stars.", pos=(30, 80), size=(230, 20) ) self.formSizer.Add(self.starsCheck) self.runesCheck = wx.CheckBox( parent=self, id=wx.ID_ANY, label="Only import units with runes.", pos=(30, 110), size=(230, 20) ) self.formSizer.Add(self.runesCheck) self.clearTeams = wx.CheckBox( parent=self, id=wx.ID_ANY, label="Clear team data", pos=(30, 140), size=(230, 20) ) self.formSizer.Add(self.clearTeams) self.btAccept = wx.Button( parent=self, id=wx.ID_ANY, pos=(100, 180), size=(100, 40), style=wx.LC_REPORT, label="Accept" ) btClose = wx.Button( parent=self, id=wx.ID_OK, pos=(210, 180), size=(100, 40), style=wx.LC_REPORT, label="Close" ) self.buttonSizer.Add(self.btAccept) self.Bind(wx.EVT_BUTTON, self.accept, self.btAccept) anim = wx.adv.Animation( os.path.dirname(os.path.realpath(__file__)) + '/res/icon/progress.gif' ) progressCtrl = wx.adv.AnimationCtrl( parent=self, id=wx.ID_ANY, anim=anim, pos=(126, 176), size=(48, 48) ) progressCtrl.Play() self.progressSizer.Add(progressCtrl) # Progress message self.messageLabel = wx.StaticText( parent=self, id=wx.ID_ANY, label="", pos=(20, 20), size=(340, 460) ) self.messageSizer.Add(self.messageLabel) # Hide progress bar and messages self.progressSizer.ShowItems(False) self.messageSizer.ShowItems(False) def checkProcess(self, event): """Checks the process output and updates the progress message. Parameters ---------- event : wx.Event, optional The event that triggered the call. """ if self.process is not None: stream = self.process.GetInputStream() if stream.CanRead(): text = bytes.decode(stream.read()) #text = text[:-1] # Remove the last newline self.messageLabel.SetLabel(text) else: self.timer.Stop() def updateComplete(self, event): """Called when the update process is complete. Hiddes the progress image, checks for errors in the output, prints a message annd sets self.updateDone and self.updateError. Parameters ---------- event : wx.Event, optional The event that triggered the call. """ self.timer.Stop() self.progressSizer.ShowItems(False) errStream = bytes.decode(self.process.GetErrorStream().read()) if errStream == "": self.messageLabel.SetLabel( self.messageLabel.GetLabel() + "\nAll done!" ) self.updateDone = True self.updateError = False else: self.messageLabel.SetLabel( self.messageLabel.GetLabel() + "\nUpdate didnt' complete succesfully" ) self.updateDone = True self.updateError = True def accept(self, event): """Called when the accept button is clicked. Hiddes the form and shows the progress image and message box, composes the command and launchs it Parameters ---------- event : wx.Event, optional The event that triggered the call. """ self.formSizer.ShowItems(False) self.progressSizer.ShowItems(True) self.messageSizer.ShowItems(True) self.buttonSizer.ShowItems(False) # TODO: Executable name for windows command = "RuneOptimizer update " command += pipes.quote(self.fileSelector.GetPath()) if self.starsCheck.GetValue(): command += " --six-stars" if self.runesCheck.GetValue(): command += " --with-runes" if self.clearTeams.GetValue(): command += " --clear-teams" command += " --gui" print("Command: " + command) # Timer ot periodically check on the process self.timer = wx.Timer(self) self.timer.Start(1000) self.Bind(wx.EVT_TIMER, self.checkProcess) # Create the process self.Bind(wx.EVT_END_PROCESS, self.updateComplete) self.process = wx.Process(self) self.process.Redirect() wx.Execute(command, wx.EXEC_ASYNC, self.process)