Use bytes in output queue

This commit is contained in:
Mathias Koehler 2014-09-04 18:37:48 +02:00
parent 5ad21350cc
commit 1311944669
2 changed files with 29 additions and 14 deletions

View file

@ -5,7 +5,7 @@
Your entrypoint for every Task you want to do with FFmpeg Your entrypoint for every Task you want to do with FFmpeg
:copyright: (c) 2013 by Mathias Koehler. :copyright: (c) 2014 by Mathias Koehler.
:license: BSD, see LICENSE for more details. :license: BSD, see LICENSE for more details.
""" """
@ -72,18 +72,18 @@ class FFmpegProcess(object):
def _queue_output(self, out, queue): def _queue_output(self, out, queue):
"""Read the output from the command bytewise. On every newline """Read the output from the command bytewise. On every newline
the line is put to the queue.""" the line is put to the queue."""
line = '' line = bytearray()
running = self.process.poll() is None running = self.running
while running: while running:
chunk = out.read(1).decode('utf-8') byte = out.read(1)
if chunk == '': if byte == b'':
running = self.process.poll() is None running = self.running
continue continue
line += chunk line += byte
if chunk in ('\n', '\r'): if byte in (b'\n', b'\r'):
queue.put(line, timeout=0.4) queue.put(''.join(line.decode('utf8')), timeout=0.4)
line = '' line = bytearray()
out.close() out.close()
def run(self, daemon=True): def run(self, daemon=True):
@ -101,6 +101,18 @@ class FFmpegProcess(object):
thread.start() thread.start()
return self return self
@property
def running(self):
return self.process.poll() is None
@property
def successful(self):
return self.process.returncode == 0
@property
def failed(self):
return not (self.successful or self.running)
def readlines(self, keepends=False): def readlines(self, keepends=False):
"""Yield lines from the queue that were collected from the """Yield lines from the queue that were collected from the
command. You can specify if you want to keep newlines at the ends. command. You can specify if you want to keep newlines at the ends.
@ -140,7 +152,7 @@ class FFmpeg(ParameterContainer):
:param args: A list of Containers that should be appended :param args: A list of Containers that should be appended
""" """
def __init__(self, binary='ffmaeg', *args): def __init__(self, binary='ffmpeg', *args):
self.binary = binary self.binary = binary
self.process = None self.process = None
ParameterContainer.__init__(self, *args) ParameterContainer.__init__(self, *args)
@ -168,3 +180,6 @@ class FFmpeg(ParameterContainer):
def __iter__(self): def __iter__(self):
return chain([self.binary], ParameterContainer.__iter__(self)) return chain([self.binary], ParameterContainer.__iter__(self))
def __str__(self):
return" ".join(self)

View file

@ -16,14 +16,14 @@ class FFmpegTestCase(unittest.TestCase):
popen = self.patcher.start() popen = self.patcher.start()
self.instance = popen.return_value self.instance = popen.return_value
read_value = list('this is a line\nthis too\n') read_value = bytearray(b'this is a line\nthis too\n')
poll = lambda: None if read_value else 0 poll = lambda: None if read_value else 0
def read(*args): def read(*args):
try: try:
return read_value.pop(0).encode('utf-8') return bytearray([read_value.pop(0)])
except IndexError: except IndexError:
return ''.encode('utf-8') return b''
self.instance.poll.side_effect = poll self.instance.poll.side_effect = poll
self.instance.stdout.read.side_effect = read self.instance.stdout.read.side_effect = read