Small refactoring & fixed tests

This commit is contained in:
Gina Häußge committed 2018-05-03 17:42:30 +02:00
1 parent b7adf115ee
commit 8369d311bf
2 files changed
+35 -29

No files matched your search

+13 -10
View File
@@ -47,6 +47,10 @@ current_render_job = None
_capture_format = "{prefix}-%d.jpg"
_output_format = "{prefix}.mpg"
# ffmpeg progress regexes
_ffmpeg_duration_regex = re.compile("Duration: (\d{2}):(\d{2}):(\d{2})\.\d{2}")
_ffmpeg_current_regex = re.compile("time=(\d{2}):(\d{2}):(\d{2})\.\d{2}")
# old capture format, needed to delete old left-overs from
# versions <1.2.9
_old_capture_format_re = re.compile("^tmp_\d{5}.jpg$")
@@ -786,9 +790,8 @@ class TimelapseRenderJob(object):
self._thread = None
self._logger = logging.getLogger(__name__)
self._duration_regex = re.compile("Duration: (\d{2}):(\d{2}):(\d{2})\.\d{2}")
self._current_regex = re.compile("time=(\d{2}):(\d{2}):(\d{2})\.\d{2}")
self._duration_i = 0
self._parsed_duration = 0
def process(self):
"""Processes the job."""
@@ -866,22 +869,22 @@ class TimelapseRenderJob(object):
def _process_ffmpeg_output(self, *lines):
for line in lines:
# We should be getting the time more often, so try it first
time = self._current_regex.search(line)
if time is not None and self._duration_i is not 0:
current_s = self._convert_time(*time.groups())
progress = current_s / float(self._duration_i) * 100
current_time = _ffmpeg_current_regex.search(line)
if current_time is not None and self._parsed_duration is not 0:
current_s = self._convert_time(*current_time.groups())
progress = current_s / float(self._parsed_duration) * 100
# Update progress bar
for callback in _update_callbacks:
try:
callback.sendRenderProgress(progress)
except:
except Exception as ex:
self._logger.exception("Exception while pushing render progress")
else:
duration = self._duration_regex.search(line)
duration = _ffmpeg_duration_regex.search(line)
if duration is not None:
self._duration_i = self._convert_time(*duration.groups())
self._parsed_duration = self._convert_time(*duration.groups())
@staticmethod
def _convert_time(hours, minutes, seconds):
+22 -19
View File
@@ -5,7 +5,11 @@ __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agp
__copyright__ = "Copyright (C) 2016 The OctoPrint Project - Released under terms of the AGPLv3 License"
import unittest
import mock
try:
import unittest.mock as mock
except ImportError:
import mock
import os
import time
@@ -185,41 +189,40 @@ class TimelapseTest(unittest.TestCase):
# Test strings
noChange1Str = " built on Jan 7 2014 22:07:02 with gcc 4.8.2 (GCC)"
durationStr = " Duration: 00:00:18.60, start: 0.000000, bitrate: "
durationStr = " Duration: 00:00:18.60, start: 0.000000, bitrate: " # 18s duration
noChange2Str = "0: Video: mpeg2video, yuv420p, 640x480, q=2-31, 10000 kb/s, 90k tbn, 25 tbc"
progress1Str = "frame= 134 fps=0.0 q=1.6 size= 1528kB time=00:00:05.28 bitrate=2370.7kbits/s dup=80 drop=0 "
progress2Str = "frame= 274 fps=270 q=2.0 size= 2748kB time=00:00:10.88 bitrate=2069.1kbits/s dup=164 drop=0 "
expectedProgress = 0
progress1Str = "frame= 134 fps=0.0 q=1.6 size= 1528kB time=00:00:05.28 bitrate=2370.7kbits/s dup=80 drop=0 " # 5s elapsed
expectedProgress1 = 5.0 / 18.0
progress2Str = "frame= 274 fps=270 q=2.0 size= 2748kB time=00:00:10.88 bitrate=2069.1kbits/s dup=164 drop=0 " # 10s elapsed
expectedProgress2 = 10.0 / 18.0
# Callback mock
class test_callback:
def sendRenderProgress(self, progress):
self.assertAlmostEqual(progress, expectedProgress, 3)
callback = mock.MagicMock()
callback.sendRenderProgress = mock.MagicMock()
# Register mock callback
octoprint.timelapse.register_callback(test_callback)
octoprint.timelapse.register_callback(callback)
r = octoprint.timelapse.TimelapseRenderJob("", "", "", "")
self.assertEqual(r._duration_i, 0)
self.assertEqual(r._parsed_duration, 0)
r._process_ffmpeg_output(noChange1Str)
self.assertEqual(r._duration_i, 0)
self.assertEqual(r._parsed_duration, 0)
r._process_ffmpeg_output(progress1Str)
self.assertEqual(r._duration_i, 0)
self.assertEqual(r._parsed_duration, 0)
r._process_ffmpeg_output(durationStr)
self.assertEqual(r._duration_i, 18)
self.assertEqual(r._parsed_duration, 18)
r._process_ffmpeg_output(noChange2Str)
self.assertEqual(r._duration_i, 18)
self.assertEqual(r._parsed_duration, 18)
expectedProgress = 27.777
r._process_ffmpeg_output(progress1Str)
self.assertEqual(r._duration_i, 18)
self.assertEqual(r._parsed_duration, 18)
callback.sendRenderProgress.assert_called_with(expectedProgress1)
expectedProgress = 55.555
r._process_ffmpeg_output(progress2Str)
self.assertEqual(r._duration_i, 18)
self.assertEqual(r._parsed_duration, 18)
callback.sendRenderProgress.assert_called_with(expectedProgress2)