Move LogPipe directly into process.py, we don't need to split that stuff on different files

This commit is contained in:
Alexandre Aubin 2021-02-28 21:20:42 +01:00
parent e85b9f71d5
commit cafe68f30e
2 changed files with 33 additions and 38 deletions

View file

@ -9,8 +9,6 @@ import queue
# process.quote syntax to access this module !
from shlex import quote
from .stream import LogPipe
quote # This line is here to avoid W0611 PEP8 error (see comments above)
# Prevent to import subprocess only for common classes
@ -94,6 +92,39 @@ def call_async_output(args, callback, **kwargs):
return p.poll()
class LogPipe(threading.Thread):
# Adapted from https://codereview.stackexchange.com/a/17959
def __init__(self, log_callback, queue):
"""Setup the object with a logger and a loglevel
and start the thread
"""
threading.Thread.__init__(self)
self.daemon = False
self.log_callback = log_callback
self.fdRead, self.fdWrite = os.pipe()
self.pipeReader = os.fdopen(self.fdRead)
self.queue = queue
self.start()
def fileno(self):
"""Return the write file descriptor of the pipe"""
return self.fdWrite
def run(self):
"""Run the thread, logging everything."""
for line in iter(self.pipeReader.readline, ""):
self.queue.put((self.log_callback, line.strip("\n")))
self.pipeReader.close()
def close(self):
"""Close the write end of the pipe."""
os.close(self.fdWrite)
# Call multiple commands -----------------------------------------------

View file

@ -1,36 +0,0 @@
import os
import threading
class LogPipe(threading.Thread):
# Adapted from https://codereview.stackexchange.com/a/17959
def __init__(self, log_callback, queue):
"""Setup the object with a logger and a loglevel
and start the thread
"""
threading.Thread.__init__(self)
self.daemon = False
self.log_callback = log_callback
self.fdRead, self.fdWrite = os.pipe()
self.pipeReader = os.fdopen(self.fdRead)
self.queue = queue
self.start()
def fileno(self):
"""Return the write file descriptor of the pipe"""
return self.fdWrite
def run(self):
"""Run the thread, logging everything."""
for line in iter(self.pipeReader.readline, ""):
self.queue.put((self.log_callback, line.strip("\n")))
self.pipeReader.close()
def close(self):
"""Close the write end of the pipe."""
os.close(self.fdWrite)