DialogUpdateJson.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. """
  2. This file is part of RuneOptimizer.
  3. RuneOptimizer is free software: you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the Free
  5. Software Foundation, either version 3 of the License, or (at your option)
  6. any later version.
  7. RuneOptimizer is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. RuneOptimizer. If not, see <https://www.gnu.org/licenses/>.
  13. """
  14. class DialogUpdateJson(wx.Dialog):
  15. """
  16. The JSON data updater dialog.
  17. Parameters
  18. ----------
  19. formSizer : wx.BoxSizer
  20. Sizer with the file selector and options.
  21. progressSizer : wx.BoxSizer
  22. Sizer with the progress image. Hidden on class creation.
  23. messageSizer : wx.BoxSizer
  24. Sizer with the progress message box. Hidden on class creation.
  25. buttonSizer : wx.BoxSizer
  26. Contains the accept button.
  27. fileSelector : wxFilePickerCtrl.
  28. File selector
  29. starsCheck : wx.Checkbox.
  30. Checkbox to indicate to save only units with 6 stars.
  31. runesCheck : wx.Checkbox.
  32. Checkbox to indicate to save only units with runes.
  33. teamsCheck : wx.Checkbox.
  34. Checkbox to indicate team deletion.
  35. updateDone : boolean
  36. Indicates if the update process has been run (default False)
  37. updateError : boolean
  38. Indicates if the update process has been run with errors (default False)
  39. messageLabel : wx.StaticText
  40. Label wich shows the output of the update process.
  41. timer : wx.Timer
  42. Timer to check the update process for new output to display.
  43. process : wx.Process
  44. The update process.
  45. Methods
  46. -------
  47. checkProcess(event)
  48. Checks the update process for new output.
  49. updateComplete(event)
  50. Called at process end, cleans the dialog and set status variables.
  51. accept(event)
  52. Checks from accept button. Starts the update process.
  53. """
  54. formSizer = None
  55. progressSizer = None
  56. messageSizer = None
  57. buttonSizer = None
  58. fileSelector = None
  59. starsCheck = None
  60. runesCheck = None
  61. teamsCheck = None
  62. updateDone = False
  63. updateError = False
  64. messageLabel = None
  65. timer = None
  66. process = None
  67. process = None
  68. def __init__(self, parent, id=wx.ID_ANY,):
  69. """Initializes the panel.
  70. Sets upt all the widgets.
  71. Parameters
  72. ----------
  73. parent : wx.*
  74. Dialog parent.
  75. id : int, optional
  76. ID for the dialig (default wx.ID_ANY).
  77. """
  78. # Parent constructor
  79. wx.Dialog.__init__(
  80. self, parent=parent, id=id, pos=(50, 50), size=(400, 290),
  81. title="Update from JSON file", style=wx.DEFAULT_DIALOG_STYLE
  82. )
  83. self.formSizer = wx.BoxSizer(wx.VERTICAL)
  84. self.progressSizer = wx.BoxSizer(wx.VERTICAL)
  85. self.messageSizer = wx.BoxSizer(wx.VERTICAL)
  86. self.buttonSizer = wx.BoxSizer(wx.VERTICAL)
  87. fileSelectorLabel = wx.StaticText(
  88. parent=self, id=wx.ID_ANY, label="Select a JSON file",
  89. pos=(30, 30), size=(130, 30)
  90. )
  91. self.formSizer.Add(fileSelectorLabel)
  92. self.fileSelector = wx.FilePickerCtrl(parent=self,
  93. id=wx.ID_ANY, path="",
  94. message="Select JSON file", wildcard="JSON files (*.json)|*.json",
  95. style=wx.FC_DEFAULT_STYLE, pos=(140, 20), size=(250, 40)
  96. )
  97. self.formSizer.Add(self.fileSelector)
  98. self.starsCheck = wx.CheckBox(
  99. parent=self, id=wx.ID_ANY, label="Only import units at with 6 stars.",
  100. pos=(30, 80), size=(230, 20)
  101. )
  102. self.formSizer.Add(self.starsCheck)
  103. self.runesCheck = wx.CheckBox(
  104. parent=self, id=wx.ID_ANY, label="Only import units with runes.",
  105. pos=(30, 110), size=(230, 20)
  106. )
  107. self.formSizer.Add(self.runesCheck)
  108. self.clearTeams = wx.CheckBox(
  109. parent=self, id=wx.ID_ANY, label="Clear team data",
  110. pos=(30, 140), size=(230, 20)
  111. )
  112. self.formSizer.Add(self.clearTeams)
  113. self.btAccept = wx.Button(
  114. parent=self, id=wx.ID_ANY, pos=(100, 180),
  115. size=(100, 40), style=wx.LC_REPORT, label="Accept"
  116. )
  117. btClose = wx.Button(
  118. parent=self, id=wx.ID_OK, pos=(210, 180),
  119. size=(100, 40), style=wx.LC_REPORT, label="Close"
  120. )
  121. self.buttonSizer.Add(self.btAccept)
  122. self.Bind(wx.EVT_BUTTON, self.accept, self.btAccept)
  123. anim = wx.adv.Animation(
  124. os.path.dirname(os.path.realpath(__file__)) + '/res/icon/progress.gif'
  125. )
  126. progressCtrl = wx.adv.AnimationCtrl(
  127. parent=self, id=wx.ID_ANY, anim=anim, pos=(126, 176), size=(48, 48)
  128. )
  129. progressCtrl.Play()
  130. self.progressSizer.Add(progressCtrl)
  131. # Progress message
  132. self.messageLabel = wx.StaticText(
  133. parent=self, id=wx.ID_ANY, label="",
  134. pos=(20, 20), size=(340, 460)
  135. )
  136. self.messageSizer.Add(self.messageLabel)
  137. # Hide progress bar and messages
  138. self.progressSizer.ShowItems(False)
  139. self.messageSizer.ShowItems(False)
  140. def checkProcess(self, event):
  141. """Checks the process output and updates the progress message.
  142. Parameters
  143. ----------
  144. event : wx.Event, optional
  145. The event that triggered the call.
  146. """
  147. if self.process is not None:
  148. stream = self.process.GetInputStream()
  149. if stream.CanRead():
  150. text = bytes.decode(stream.read())
  151. #text = text[:-1] # Remove the last newline
  152. self.messageLabel.SetLabel(text)
  153. else:
  154. self.timer.Stop()
  155. def updateComplete(self, event):
  156. """Called when the update process is complete.
  157. Hiddes the progress image, checks for errors in the output, prints a
  158. message annd sets self.updateDone and self.updateError.
  159. Parameters
  160. ----------
  161. event : wx.Event, optional
  162. The event that triggered the call.
  163. """
  164. self.timer.Stop()
  165. self.progressSizer.ShowItems(False)
  166. errStream = bytes.decode(self.process.GetErrorStream().read())
  167. if errStream == "":
  168. self.messageLabel.SetLabel(
  169. self.messageLabel.GetLabel() + "\nAll done!"
  170. )
  171. self.updateDone = True
  172. self.updateError = False
  173. else:
  174. self.messageLabel.SetLabel(
  175. self.messageLabel.GetLabel() +
  176. "\nUpdate didnt' complete succesfully"
  177. )
  178. self.updateDone = True
  179. self.updateError = True
  180. def accept(self, event):
  181. """Called when the accept button is clicked.
  182. Hiddes the form and shows the progress image and message box, composes
  183. the command and launchs it
  184. Parameters
  185. ----------
  186. event : wx.Event, optional
  187. The event that triggered the call.
  188. """
  189. self.formSizer.ShowItems(False)
  190. self.progressSizer.ShowItems(True)
  191. self.messageSizer.ShowItems(True)
  192. self.buttonSizer.ShowItems(False)
  193. # TODO: Executable name for windows
  194. command = "RuneOptimizer update "
  195. command += pipes.quote(self.fileSelector.GetPath())
  196. if self.starsCheck.GetValue():
  197. command += " --six-stars"
  198. if self.runesCheck.GetValue():
  199. command += " --with-runes"
  200. if self.clearTeams.GetValue():
  201. command += " --clear-teams"
  202. command += " --gui"
  203. print("Command: " + command)
  204. # Timer ot periodically check on the process
  205. self.timer = wx.Timer(self)
  206. self.timer.Start(1000)
  207. self.Bind(wx.EVT_TIMER, self.checkProcess)
  208. # Create the process
  209. self.Bind(wx.EVT_END_PROCESS, self.updateComplete)
  210. self.process = wx.Process(self)
  211. self.process.Redirect()
  212. wx.Execute(command, wx.EXEC_ASYNC, self.process)