From 27cf1bec52f8f8149e7fb492889d38130e39c9a5 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 1 Jul 2023 03:13:38 +0200 Subject: [PATCH] Removed some optional top-level imports. Optional top-level imports in Tornado route declarations will trigger `ImportError`. While this will just mean that those routes will be skipped, it will also generate a lot of noise on the logs. --- platypush/backend/http/app/utils/routes.py | 3 +-- platypush/plugins/camera/model/camera.py | 4 ++-- platypush/plugins/camera/model/writer/__init__.py | 9 ++++----- platypush/plugins/camera/model/writer/ffmpeg.py | 8 +++----- 4 files changed, 10 insertions(+), 14 deletions(-) diff --git a/platypush/backend/http/app/utils/routes.py b/platypush/backend/http/app/utils/routes.py index b27fa4ca1..f7de249da 100644 --- a/platypush/backend/http/app/utils/routes.py +++ b/platypush/backend/http/app/utils/routes.py @@ -30,8 +30,7 @@ def get_routes(): if hasattr(module, '__routes__'): routes.extend(module.__routes__) except Exception as e: - logger.warning('Could not import module %s', mod_name) - logger.exception(e) + logger().warning('Could not import module %s: %s', mod_name, str(e)) continue return routes diff --git a/platypush/plugins/camera/model/camera.py b/platypush/plugins/camera/model/camera.py index 3791f201b..851000213 100644 --- a/platypush/plugins/camera/model/camera.py +++ b/platypush/plugins/camera/model/camera.py @@ -3,8 +3,6 @@ import threading from dataclasses import asdict, dataclass from typing import Optional, Union, Tuple, Set -import numpy as np - from platypush.plugins.camera.model.writer import ( StreamWriter, VideoWriter, @@ -78,6 +76,8 @@ class Camera: Calculates the effective resolution of the camera in pixels, taking into account the base resolution, the scale and the rotation. """ + import numpy as np + assert self.info.resolution, 'No base resolution specified' rot = (self.info.rotate or 0) * math.pi / 180 sin = math.sin(rot) diff --git a/platypush/plugins/camera/model/writer/__init__.py b/platypush/plugins/camera/model/writer/__init__.py index a24d780f7..89f4e311d 100644 --- a/platypush/plugins/camera/model/writer/__init__.py +++ b/platypush/plugins/camera/model/writer/__init__.py @@ -7,8 +7,6 @@ import time from abc import ABC, abstractmethod from typing import Optional, IO -from PIL.Image import Image - from platypush.utils import get_redis @@ -28,11 +26,12 @@ class VideoWriter(ABC): self.closed = False @abstractmethod - def write(self, image: Image): + def write(self, image): """ Write an image to the channel. :param image: PIL Image instance. + :type image: PIL.Image.Image """ raise NotImplementedError() @@ -88,7 +87,7 @@ class StreamWriter(VideoWriter, ABC): self.redis_queue = redis_queue self.sock = sock - def write(self, image: Image): + def write(self, image): data = self.encode(image) with self.ready: if self.buffer.closed: @@ -116,7 +115,7 @@ class StreamWriter(VideoWriter, ABC): get_redis().publish(self.redis_queue, data) @abstractmethod - def encode(self, image: Image) -> bytes: + def encode(self, image) -> bytes: """ Encode an image before sending it to the channel. diff --git a/platypush/plugins/camera/model/writer/ffmpeg.py b/platypush/plugins/camera/model/writer/ffmpeg.py index 41011e393..d31f9efce 100644 --- a/platypush/plugins/camera/model/writer/ffmpeg.py +++ b/platypush/plugins/camera/model/writer/ffmpeg.py @@ -5,8 +5,6 @@ import time from abc import ABC from typing import Optional, Tuple -from PIL.Image import Image - from platypush.plugins.camera.model.camera import Camera from platypush.plugins.camera.model.writer import ( VideoWriter, @@ -73,7 +71,7 @@ class FFmpegWriter(VideoWriter, ABC): def is_closed(self): return self.closed or not self.ffmpeg or self.ffmpeg.poll() is not None - def write(self, image: Image): + def write(self, image): if self.is_closed(): return @@ -146,7 +144,7 @@ class FFmpegStreamWriter(StreamWriter, FFmpegWriter, ABC): self._reader = threading.Thread(target=self._reader_thread) self._reader.start() - def encode(self, image: Image) -> bytes: + def encode(self, image) -> bytes: return image.convert('RGB').tobytes() def _reader_thread(self): @@ -173,7 +171,7 @@ class FFmpegStreamWriter(StreamWriter, FFmpegWriter, ABC): self._sock_send(self.frame) - def write(self, image: Image): + def write(self, image): if self.is_closed(): return