DialogUpdateJson.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. Allows for JSON file selection and control options. Runs a update command.
  18. Parameters
  19. ----------
  20. update_done : boolean
  21. Indicates if the update process has been run (default False)
  22. update_error : boolean
  23. Indicates if the update process has been run with errors (default False)
  24. """
  25. _form_sizer = None
  26. _progress_sizer = None
  27. _message_sizer = None
  28. _button_sizer = None
  29. _file_selector = None
  30. _stars_check = None
  31. _runes_check = None
  32. _clear_teams_check = None
  33. update_done = False
  34. update_error = False
  35. _message_label = None
  36. _timer = None
  37. _process = None
  38. def __init__(self, parent, id=wx.ID_ANY,):
  39. """Initializes the panel.
  40. Sets upt all the widgets.
  41. Parameters
  42. ----------
  43. parent : wx.*
  44. Dialog parent.
  45. id : int, optional
  46. ID for the dialig (default wx.ID_ANY).
  47. """
  48. # Parent constructor
  49. wx.Dialog.__init__(
  50. self, parent=parent, id=id, pos=(50, 50), size=(400, 290),
  51. title="Update from JSON file", style=wx.DEFAULT_DIALOG_STYLE
  52. )
  53. self._form_sizer = wx.BoxSizer(wx.VERTICAL)
  54. self._progress_sizer = wx.BoxSizer(wx.VERTICAL)
  55. self._message_sizer = wx.BoxSizer(wx.VERTICAL)
  56. self._button_sizer = wx.BoxSizer(wx.VERTICAL)
  57. file_selector_label = wx.StaticText(
  58. parent=self, id=wx.ID_ANY, label="Select a JSON file",
  59. pos=(30, 30), size=(130, 30)
  60. )
  61. self._form_sizer.Add(file_selector_label)
  62. self._file_selector = wx.FilePickerCtrl(parent=self,
  63. id=wx.ID_ANY, path="",
  64. message="Select JSON file", wildcard="JSON files (*.json)|*.json",
  65. style=wx.FC_DEFAULT_STYLE, pos=(140, 20), size=(250, 40)
  66. )
  67. self._form_sizer.Add(self._file_selector)
  68. self._stars_check = wx.CheckBox(
  69. parent=self, id=wx.ID_ANY, label="Only import units at with 6 stars.",
  70. pos=(30, 80), size=(230, 20)
  71. )
  72. self._form_sizer.Add(self._stars_check)
  73. self._runes_check = wx.CheckBox(
  74. parent=self, id=wx.ID_ANY, label="Only import units with runes.",
  75. pos=(30, 110), size=(230, 20)
  76. )
  77. self._form_sizer.Add(self._runes_check)
  78. self._clear_teams_check = wx.CheckBox(
  79. parent=self, id=wx.ID_ANY, label="Clear team data",
  80. pos=(30, 140), size=(230, 20)
  81. )
  82. self._form_sizer.Add(self._clear_teams_check)
  83. self._accept_button = wx.Button(
  84. parent=self, id=wx.ID_ANY, pos=(100, 180),
  85. size=(100, 40), style=wx.LC_REPORT, label="Accept"
  86. )
  87. wx.Button(
  88. parent=self, id=wx.ID_OK, pos=(210, 180),
  89. size=(100, 40), style=wx.LC_REPORT, label="Close"
  90. )
  91. self._button_sizer.Add(self._accept_button)
  92. self._accept_button.Bind(wx.EVT_BUTTON, self._accept)
  93. anim = wx.adv.Animation(
  94. os.path.dirname(os.path.realpath(__file__)) + '/res/icon/progress.gif'
  95. )
  96. progress_ctrl = wx.adv.AnimationCtrl(
  97. parent=self, id=wx.ID_ANY, anim=anim, pos=(126, 176), size=(48, 48)
  98. )
  99. progress_ctrl.Play()
  100. self._progress_sizer.Add(progress_ctrl)
  101. # Progress message
  102. self._message_label = wx.StaticText(
  103. parent=self, id=wx.ID_ANY, label="",
  104. pos=(20, 20), size=(340, 460)
  105. )
  106. self._message_sizer.Add(self._message_label)
  107. # Hide progress bar and messages
  108. self._progress_sizer.ShowItems(False)
  109. self._message_sizer.ShowItems(False)
  110. def _check_process(self, event):
  111. """Checks the process output and updates the progress message.
  112. Parameters
  113. ----------
  114. event : wx.Event, optional
  115. The event that triggered the call.
  116. """
  117. if self._process is not None:
  118. stream = self._process.GetInputStream()
  119. if stream.CanRead():
  120. text = bytes.decode(stream.read())
  121. #text = text[:-1] # Remove the last newline
  122. self._message_label.SetLabel(text)
  123. else:
  124. self._timer.Stop()
  125. def _update_complete(self, event):
  126. """Called when the update process is complete.
  127. Hiddes the progress image, checks for errors in the output, prints a
  128. message annd sets self.update_done and self.update_error.
  129. Parameters
  130. ----------
  131. event : wx.Event, optional
  132. The event that triggered the call.
  133. """
  134. self._timer.Stop()
  135. self._progress_sizer.ShowItems(False)
  136. errStream = bytes.decode(self._process.GetErrorStream().read())
  137. if errStream == "":
  138. self._message_label.SetLabel(
  139. self._message_label.GetLabel() + "\nAll done!"
  140. )
  141. self.update_done = True
  142. self.update_error = False
  143. else:
  144. self._message_label.SetLabel(
  145. self._message_label.GetLabel() +
  146. "\nUpdate didnt' complete succesfully"
  147. )
  148. self.update_done = True
  149. self.update_error = True
  150. def _accept(self, event):
  151. """Called when the accept button is clicked.
  152. Hiddes the form and shows the progress image and message box, composes
  153. the command and launchs it
  154. Parameters
  155. ----------
  156. event : wx.Event, optional
  157. The event that triggered the call.
  158. """
  159. self._form_sizer.ShowItems(False)
  160. self._progress_sizer.ShowItems(True)
  161. self._message_sizer.ShowItems(True)
  162. self._button_sizer.ShowItems(False)
  163. # TODO: Executable name for windows
  164. command = "runeoptimizer update "
  165. command += pipes.quote(self._file_selector.GetPath())
  166. if self._stars_check.GetValue():
  167. command += " --six-stars"
  168. if self._runes_check.GetValue():
  169. command += " --with-runes"
  170. if self._clear_teams_check.GetValue():
  171. command += " --clear-teams"
  172. command += " --gui"
  173. print("Command: " + command)
  174. # Timer ot periodically check on the process
  175. self._timer = wx.Timer(self)
  176. self._timer.Start(1000)
  177. self._timer.Bind(wx.EVT_TIMER, self._check_process)
  178. # Create the process
  179. self.Bind(wx.EVT_END_PROCESS, self._update_complete)
  180. self._process = wx.Process(self)
  181. self._process.Redirect()
  182. wx.Execute(command, wx.EXEC_ASYNC, self._process)