From eae6fbbc6067d9777854bc4d7589e601e21af532 Mon Sep 17 00:00:00 2001 From: Todd Ransom Date: Thu, 5 Sep 2024 13:08:25 -0400 Subject: [PATCH] implement pylint suggestions --- stream.py | 18 ++++++++++++------ streaming.py | 23 +++++++++++++++-------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/stream.py b/stream.py index 1c3357f..f082f80 100644 --- a/stream.py +++ b/stream.py @@ -1,4 +1,5 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python3 +"""A simple streaming mjpg server""" import io import logging @@ -24,7 +25,9 @@ class StreamingOutput(io.BufferedIOBase): - def __init__(self): + """Simple class to create streaming output""" + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.frame = None self.condition = Condition() @@ -35,7 +38,9 @@ def write(self, buf): class StreamingHandler(server.BaseHTTPRequestHandler): - def do_GET(self): + """Simple class to handle http requests""" + def do_GET(self): # pylint: disable=invalid-name + """Handle GET requests""" if self.path == '/': self.send_response(301) self.send_header('Location', '/index.html') @@ -65,21 +70,22 @@ def do_GET(self): self.end_headers() self.wfile.write(frame) self.wfile.write(b'\r\n') - except Exception as e: + except BrokenPipeError as exception: logging.warning( 'Removed streaming client %s: %s', - self.client_address, str(e)) + self.client_address, str(exception)) else: self.send_error(404) self.end_headers() class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer): + """Configuration for the http server""" allow_reuse_address = True daemon_threads = True -picam2 = Picamera2() +picam2 = Picamera2() # pylint: disable=not-callable picam2.configure(picam2.create_video_configuration(main={"size": (640, 480)})) output = StreamingOutput() picam2.start_recording(MJPEGEncoder(), FileOutput(output)) diff --git a/streaming.py b/streaming.py index 60adb14..2f20340 100644 --- a/streaming.py +++ b/streaming.py @@ -1,4 +1,5 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python3 +"""Interface to easily enable the creation of a streaming mjpg server""" import io import logging @@ -28,7 +29,9 @@ class StreamingOutput(io.BufferedIOBase): - def __init__(self): + """Simple class to create streaming output""" + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.frame = None self.condition = Condition() @@ -38,7 +41,9 @@ def write(self, buf): self.condition.notify_all() class StreamingHandler(server.BaseHTTPRequestHandler): - def do_GET(self): + """Simple class to handle http requests""" + def do_GET(self): # pylint: disable=invalid-name + """Handle GET requests""" if self.path == '/': self.send_response(301) self.send_header('Location', '/index.html') @@ -68,29 +73,31 @@ def do_GET(self): self.end_headers() self.wfile.write(frame) self.wfile.write(b'\r\n') - except Exception as e: + except BrokenPipeError as exception: logging.warning( 'Removed streaming client %s: %s', - self.client_address, str(e)) + self.client_address, str(exception)) else: self.send_error(404) self.end_headers() class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer): + """Configuration for the http server""" allow_reuse_address = True daemon_threads = True def serve(port: int = 3001): - picam2 = Picamera2() + """Start streaming""" + picam2 = Picamera2() # pylint: disable=not-callable picam2.configure(picam2.create_video_configuration(main={"size": (640, 480)})) picam2.start_recording(MJPEGEncoder(), FileOutput(output)) try: address = ('', port) - server = StreamingServer(address, StreamingHandler) - server.serve_forever() + streaming_server = StreamingServer(address, StreamingHandler) + streaming_server.serve_forever() finally: picam2.stop_recording()