From 17b6b02986b7cba279db6a75f4a69ca6c94dadc7 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 6 Jun 2024 22:24:36 +0200 Subject: [PATCH] Replaced `warnings.warn` with `logging.warnings`. I couldn't find an easy and reliable way of routing `warnings.warn` to `logging`. Closes: #281 --- CHANGELOG.md | 15 ++++++++++--- platypush/plugins/__init__.py | 7 +----- platypush/plugins/light/hue/__init__.py | 24 ++++++++++----------- platypush/plugins/sensor/hcsr04/__init__.py | 5 +---- platypush/plugins/sound/__init__.py | 17 ++++----------- platypush/schemas/media/jellyfin.py | 14 ++++++------ 6 files changed, 37 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8dd7b95250..8014cf5b69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,13 +1,22 @@ # Changelog +## [Unreleased] + +- [[#281](https://git.platypush.tech/platypush/platypush/issues/281)] + replaced `warnings.warn` with `logging.warning`, as there is no easy and + reliable way of routing `warnings.warn` to `logging`. + ## [1.1.0] - 2024-06-06 -- [#405] Fixed timezone/timestamp rendering issues for `calendar.ical` events. -- [#403] Included inherited actions in plugins docs. +- [[#405](https://git.platypush.tech/platypush/platypush/issues/405)] Fixed + timezone/timestamp rendering issues for `calendar.ical` events. +- [[#403]((https://git.platypush.tech/platypush/platypush/issues/403))] + Included inherited actions in plugins docs. ## [1.0.7] - 2024-06-02 -- [#384] Added `assistant.openai` and `tts.openai` plugins. +- [[#384]((https://git.platypush.tech/platypush/platypush/issues/384))] Added + `assistant.openai` and `tts.openai` plugins. ## [1.0.6] - 2024-06-01 diff --git a/platypush/plugins/__init__.py b/platypush/plugins/__init__.py index a1ed1eb0e0..edb33fc5dd 100644 --- a/platypush/plugins/__init__.py +++ b/platypush/plugins/__init__.py @@ -1,7 +1,6 @@ import asyncio import logging import threading -import warnings from abc import ABC, abstractmethod from functools import wraps @@ -168,11 +167,7 @@ class RunnablePlugin(Plugin): self._thread: Optional[threading.Thread] = None if kwargs.get('poll_seconds') is not None: - warnings.warn( - 'poll_seconds is deprecated, use poll_interval instead', - DeprecationWarning, - stacklevel=2, - ) + self.logger.warning('poll_seconds is deprecated, use poll_interval instead') if self.poll_interval is None: self.poll_interval = kwargs['poll_seconds'] diff --git a/platypush/plugins/light/hue/__init__.py b/platypush/plugins/light/hue/__init__.py index 1bc8769f11..7ee9434564 100644 --- a/platypush/plugins/light/hue/__init__.py +++ b/platypush/plugins/light/hue/__init__.py @@ -17,8 +17,6 @@ from typing import ( Set, Union, ) -import warnings - from platypush.config import Config from platypush.context import get_bus from platypush.entities import Entity, LightEntityManager @@ -86,11 +84,7 @@ class LightHuePlugin(RunnablePlugin, LightEntityManager): poll_seconds = kwargs.pop('poll_seconds', None) if poll_seconds is not None: - warnings.warn( - 'poll_seconds is deprecated, use poll_interval instead', - DeprecationWarning, - stacklevel=2, - ) + self.logger.warning('poll_seconds is deprecated, use poll_interval instead') if poll_interval is None: poll_interval = poll_seconds @@ -1156,12 +1150,16 @@ class LightHuePlugin(RunnablePlugin, LightEntityManager): temperature=entity.get('state', {}).get('ct'), colormode=entity.get('colormode'), reachable=entity.get('state', {}).get('reachable'), - x=entity['state']['xy'][0] - if entity.get('state', {}).get('xy') - else None, - y=entity['state']['xy'][1] - if entity.get('state', {}).get('xy') - else None, + x=( + entity['state']['xy'][0] + if entity.get('state', {}).get('xy') + else None + ), + y=( + entity['state']['xy'][1] + if entity.get('state', {}).get('xy') + else None + ), effect=entity.get('state', {}).get('effect'), **( { diff --git a/platypush/plugins/sensor/hcsr04/__init__.py b/platypush/plugins/sensor/hcsr04/__init__.py index 20cfe8a77f..3343e36948 100644 --- a/platypush/plugins/sensor/hcsr04/__init__.py +++ b/platypush/plugins/sensor/hcsr04/__init__.py @@ -1,7 +1,6 @@ from collections.abc import Collection import time from typing import List, Optional, Union -import warnings from platypush.context import get_bus from platypush.entities.distance import DistanceSensor @@ -45,10 +44,8 @@ class SensorHcsr04Plugin(GpioPlugin, SensorPlugin): measurement_interval = kwargs.pop('measurement_interval', None) if measurement_interval is not None: - warnings.warn( + self.logger.warning( 'measurement_interval is deprecated, use poll_interval instead', - DeprecationWarning, - stacklevel=2, ) poll_interval = measurement_interval diff --git a/platypush/plugins/sound/__init__.py b/platypush/plugins/sound/__init__.py index 5cfe958cab..8ead17a971 100644 --- a/platypush/plugins/sound/__init__.py +++ b/platypush/plugins/sound/__init__.py @@ -1,5 +1,4 @@ from dataclasses import asdict -import warnings from typing import Iterable, List, Optional, Union from platypush.plugins import RunnablePlugin, action @@ -184,10 +183,8 @@ class SoundPlugin(RunnablePlugin): blocksize = blocksize or self.output_blocksize if file: - warnings.warn( + self.logger.warning( 'file is deprecated, use resource instead', - DeprecationWarning, - stacklevel=1, ) if not resource: resource = file @@ -232,10 +229,8 @@ class SoundPlugin(RunnablePlugin): """ Deprecated alias for :meth:`.record`. """ - warnings.warn( + self.logger.warning( 'sound.stream_recording is deprecated, use sound.record instead', - DeprecationWarning, - stacklevel=1, ) return self.record(*args, **kwargs) @@ -319,10 +314,8 @@ class SoundPlugin(RunnablePlugin): """ Deprecated alias for :meth:`.record`. """ - warnings.warn( + self.logger.warning( 'sound.recordplay is deprecated, use sound.record with `play_audio=True` instead', - DeprecationWarning, - stacklevel=1, ) kwargs['play_audio'] = True @@ -398,10 +391,8 @@ class SoundPlugin(RunnablePlugin): Deprecated alias for :meth:`.status`. """ - warnings.warn( + self.logger.warning( 'sound.query_streams is deprecated, use sound.status instead', - DeprecationWarning, - stacklevel=1, ) return self.status() diff --git a/platypush/schemas/media/jellyfin.py b/platypush/schemas/media/jellyfin.py index c4fba21e2b..689c41c0be 100644 --- a/platypush/schemas/media/jellyfin.py +++ b/platypush/schemas/media/jellyfin.py @@ -1,4 +1,4 @@ -import warnings +import logging from marshmallow import Schema, fields, pre_dump, post_dump @@ -6,6 +6,8 @@ from platypush.context import get_plugin from . import MediaArtistSchema, MediaCollectionSchema, MediaVideoSchema +logger = logging.getLogger(__name__) + class JellyfinSchema(Schema): def __init__(self, *args, **kwargs): @@ -20,9 +22,10 @@ class JellyfinSchema(Schema): @post_dump def gen_img_url(self, data: dict, **_) -> dict: if 'image' in self.fields: + plugin = get_plugin('media.jellyfin') + assert plugin, 'The media.jellyfin plugin is not configured' data['image'] = ( - get_plugin('media.jellyfin').server - + f'/Items/{data["id"]}' # type: ignore + plugin.server + f'/Items/{data["id"]}' # type: ignore '/Images/Primary?fillHeight=333&fillWidth=222&quality=96' ) @@ -43,9 +46,8 @@ class JellyfinSchema(Schema): if not video_format: if not available_containers: - warnings.warn( - f'The media ID {data["Id"]} has no available video containers', - stacklevel=2, + logger.warning( + 'The media ID %s has no available video containers', data["Id"] ) return data