You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
4.7 KiB

Elapsed / Remaining Time on Progress Bar parent 00e0f3e7c6434240ffe02522a0be2daf8c5b456c author Jack McKew <jackmckew2@gmail.com> 1594344614 +1000 committer Jack McKew <jackmckew2@gmail.com> 1594346638 +1000 Elapsed / Remaining Time on Progress Bar parent 00e0f3e7c6434240ffe02522a0be2daf8c5b456c author Fizban <jackmckew2@gmail.com> 1593182730 +1000 committer Fizban <jackmckew2@gmail.com> 1593246420 +1000 parent 00e0f3e7c6434240ffe02522a0be2daf8c5b456c author Fizban <jackmckew2@gmail.com> 1593182730 +1000 committer Fizban <jackmckew2@gmail.com> 1593246359 +1000 Elapsed / Remaining Time on Progress Bar Elapsed / Remaining Time on Progress Bar Time Remaining Text for Progress Time remaining somewhat working Time format working Add tests and pythn 2.7 compat Incase python 2 import perf counter in func Fix flickering text and align Remove transactUI Add new decorators Update time remaining notes in README Amend tests with new arguments Remove unused import Separate into time module Move to dictionary structure options Explicitly show time text Amend tests for separate module Integration test - missing on complete tests Remove old code Elapsed / Remaining Time on Progress Bar parent 00e0f3e7c6434240ffe02522a0be2daf8c5b456c author Fizban <jackmckew2@gmail.com> 1593182730 +1000 committer Fizban <jackmckew2@gmail.com> 1593246420 +1000 parent 00e0f3e7c6434240ffe02522a0be2daf8c5b456c author Fizban <jackmckew2@gmail.com> 1593182730 +1000 committer Fizban <jackmckew2@gmail.com> 1593246359 +1000 Elapsed / Remaining Time on Progress Bar Elapsed / Remaining Time on Progress Bar Time Remaining Text for Progress Time remaining somewhat working Time format working Add tests and pythn 2.7 compat Incase python 2 import perf counter in func Fix flickering text and align Remove transactUI Add new decorators Update time remaining notes in README Amend tests with new arguments Remove unused import Separate into time module Move to dictionary structure options Explicitly show time text Amend tests for separate module Remove old code Pass testdata as dict instead of kwargs Merge dictionaries for defaults Test almost working Delete settings.json Revert "Test almost working" This reverts commit f17d50681cae664719f67a7e8cc0b1feaf1ac4c7. Back to working state Remove unhelpful test Update docs Working without updated test Add tests for time remaining text Elapsed / Remaining Time on Progress Bar parent 00e0f3e7c6434240ffe02522a0be2daf8c5b456c author Jack McKew <jackmckew2@gmail.com> 1594344614 +1000 committer Jack McKew <jackmckew2@gmail.com> 1594346638 +1000 Elapsed / Remaining Time on Progress Bar parent 00e0f3e7c6434240ffe02522a0be2daf8c5b456c author Fizban <jackmckew2@gmail.com> 1593182730 +1000 committer Fizban <jackmckew2@gmail.com> 1593246420 +1000 parent 00e0f3e7c6434240ffe02522a0be2daf8c5b456c author Fizban <jackmckew2@gmail.com> 1593182730 +1000 committer Fizban <jackmckew2@gmail.com> 1593246359 +1000 Elapsed / Remaining Time on Progress Bar Elapsed / Remaining Time on Progress Bar Time Remaining Text for Progress Time remaining somewhat working Time format working Add tests and pythn 2.7 compat Incase python 2 import perf counter in func Fix flickering text and align Remove transactUI Add new decorators Update time remaining notes in README Amend tests with new arguments Remove unused import Separate into time module Move to dictionary structure options Explicitly show time text Amend tests for separate module Integration test - missing on complete tests Remove old code Elapsed / Remaining Time on Progress Bar parent 00e0f3e7c6434240ffe02522a0be2daf8c5b456c author Fizban <jackmckew2@gmail.com> 1593182730 +1000 committer Fizban <jackmckew2@gmail.com> 1593246420 +1000 parent 00e0f3e7c6434240ffe02522a0be2daf8c5b456c author Fizban <jackmckew2@gmail.com> 1593182730 +1000 committer Fizban <jackmckew2@gmail.com> 1593246359 +1000 Elapsed / Remaining Time on Progress Bar Elapsed / Remaining Time on Progress Bar Time Remaining Text for Progress Time remaining somewhat working Time format working Add tests and pythn 2.7 compat Incase python 2 import perf counter in func Fix flickering text and align Remove transactUI Add new decorators Update time remaining notes in README Amend tests with new arguments Remove unused import Separate into time module Move to dictionary structure options Explicitly show time text Amend tests for separate module Remove old code Pass testdata as dict instead of kwargs Merge dictionaries for defaults Test almost working Delete settings.json Revert "Test almost working" This reverts commit f17d50681cae664719f67a7e8cc0b1feaf1ac4c7. Back to working state Remove unhelpful test Update docs Working without updated test Add tests for time remaining text Remove artifact from squashing Amend artifacts Handle no progress and tidy up footer label
4 years ago
  1. import os
  2. import re
  3. import signal
  4. import subprocess
  5. import sys
  6. from functools import partial
  7. from threading import Thread
  8. import psutil
  9. from gooey.gui import events
  10. from gooey.gui.pubsub import pub
  11. from gooey.gui.util.casting import safe_float
  12. from gooey.util.functional import unit, bind
  13. class ProcessController(object):
  14. def __init__(self, progress_regex, progress_expr, hide_progress_msg,
  15. encoding, shell=True, shutdown_signal=signal.SIGTERM):
  16. self._process = None
  17. self.progress_regex = progress_regex
  18. self.progress_expr = progress_expr
  19. self.hide_progress_msg = hide_progress_msg
  20. self.encoding = encoding
  21. self.wasForcefullyStopped = False
  22. self.shell_execution = shell
  23. self.shutdown_signal = shutdown_signal
  24. def was_success(self):
  25. self._process.communicate()
  26. return self._process.returncode == 0
  27. def poll(self):
  28. if not self._process:
  29. raise Exception('Not started!')
  30. return self._process.poll()
  31. def stop(self):
  32. """
  33. Sends a signal of the user's choosing (default SIGTERM) to
  34. the child process.
  35. """
  36. if self.running():
  37. self.wasForcefullyStopped = True
  38. self.send_shutdown_signal()
  39. def send_shutdown_signal(self):
  40. parent = psutil.Process(self._process.pid)
  41. for child in parent.children(recursive=True):
  42. print(child)
  43. child.send_signal(self.shutdown_signal)
  44. parent.send_signal(self.shutdown_signal)
  45. def running(self):
  46. return self._process and self.poll() is None
  47. def run(self, command):
  48. """
  49. Kicks off the user's code in a subprocess.
  50. Implementation Note: CREATE_NEW_SUBPROCESS is required to have signals behave sanely
  51. on windows. See the signal_support module for full background.
  52. """
  53. self.wasForcefullyStopped = False
  54. env = os.environ.copy()
  55. env["GOOEY"] = "1"
  56. env["PYTHONIOENCODING"] = self.encoding
  57. # TODO: why is this try/catch here..?
  58. try:
  59. self._process = subprocess.Popen(
  60. command.encode(sys.getfilesystemencoding()),
  61. stdout=subprocess.PIPE, stdin=subprocess.PIPE,
  62. stderr=subprocess.STDOUT, shell=self.shell_execution, env=env,
  63. creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
  64. except:
  65. self._process = subprocess.Popen(
  66. command,
  67. stdout=subprocess.PIPE, stdin=subprocess.PIPE,
  68. stderr = subprocess.STDOUT, shell = self.shell_execution, env=env,
  69. creationflags=subprocess.CREATE_NEW_PROCESS_GROUP
  70. )
  71. t = Thread(target=self._forward_stdout, args=(self._process,))
  72. t.start()
  73. def _forward_stdout(self, process):
  74. '''
  75. Reads the stdout of `process` and forwards lines and progress
  76. to any interested subscribers
  77. '''
  78. while True:
  79. line = process.stdout.readline()
  80. if not line:
  81. break
  82. _progress = self._extract_progress(line)
  83. pub.send_message(events.PROGRESS_UPDATE, progress=_progress)
  84. if _progress is None or self.hide_progress_msg is False:
  85. pub.send_message(events.CONSOLE_UPDATE,
  86. msg=line.decode(self.encoding))
  87. pub.send_message(events.EXECUTION_COMPLETE)
  88. def _extract_progress(self, text):
  89. '''
  90. Finds progress information in the text using the
  91. user-supplied regex and calculation instructions
  92. '''
  93. # monad-ish dispatch to avoid the if/else soup
  94. find = partial(re.search, string=text.strip().decode(self.encoding))
  95. regex = unit(self.progress_regex)
  96. match = bind(regex, find)
  97. result = bind(match, self._calculate_progress)
  98. return result
  99. def _calculate_progress(self, match):
  100. '''
  101. Calculates the final progress value found by the regex
  102. '''
  103. if not self.progress_expr:
  104. return safe_float(match.group(1))
  105. else:
  106. return self._eval_progress(match)
  107. def _eval_progress(self, match):
  108. '''
  109. Runs the user-supplied progress calculation rule
  110. '''
  111. _locals = {k: safe_float(v) for k, v in match.groupdict().items()}
  112. if "x" not in _locals:
  113. _locals["x"] = [safe_float(x) for x in match.groups()]
  114. try:
  115. return int(eval(self.progress_expr, {}, _locals))
  116. except:
  117. return None