From bcc83ff4bd6a58519790d8b7a1c01458f7934926 Mon Sep 17 00:00:00 2001 From: Vignesh Rao Date: Tue, 24 Dec 2024 22:28:23 -0600 Subject: [PATCH] Remove explicit boolean check for smart devices --- docs/index.html | 2 +- jarvis/executors/files.py | 17 +------ jarvis/executors/lights.py | 15 +++---- jarvis/executors/tv.py | 14 +++--- jarvis/modules/logger.py | 92 +++++++++++++++++++------------------- 5 files changed, 63 insertions(+), 77 deletions(-) diff --git a/docs/index.html b/docs/index.html index 5b10f3ff..a3a0c543 100644 --- a/docs/index.html +++ b/docs/index.html @@ -3696,7 +3696,7 @@

———-Executors———-
-jarvis.executors.files.get_smart_devices() dict | bool | None
+jarvis.executors.files.get_smart_devices() dict | None

Load smart devices’ data from feed file.

diff --git a/jarvis/executors/files.py b/jarvis/executors/files.py index 584ad8b0..5609601a 100644 --- a/jarvis/executors/files.py +++ b/jarvis/executors/files.py @@ -190,22 +190,9 @@ def put_automation( _dumper(models.fileio.automation, sorted_data) -def get_smart_devices() -> dict | bool | None: +def get_smart_devices() -> dict | None: """Load smart devices' data from feed file.""" - # fixme: Change the logic to NOT look for False specifically - try: - with open(models.fileio.smart_devices) as file: - if smart_devices := yaml.load(stream=file, Loader=yaml.FullLoader): - return smart_devices - else: - logger.warning("'%s' is empty.", models.fileio.smart_devices) - except (yaml.YAMLError, FileNotFoundError) as error: - if isinstance(error, FileNotFoundError): - logger.warning("%s not found.", models.fileio.smart_devices) - return - else: - logger.debug(error) - return False + return _loader(models.fileio.smart_devices) def put_smart_devices(data: dict) -> None: diff --git a/jarvis/executors/lights.py b/jarvis/executors/lights.py index 86dd8f5b..60b7c98f 100644 --- a/jarvis/executors/lights.py +++ b/jarvis/executors/lights.py @@ -123,16 +123,15 @@ def lights(phrase: str) -> None: if not internet.vpn_checker(): return - smart_devices = files.get_smart_devices() - if smart_devices is False: - speaker.speak( - text=f"I'm sorry {models.env.title}! I wasn't able to read the source information." - ) - return - if smart_devices and (lights_map := get_lights(data=smart_devices)): + if (smart_devices := files.get_smart_devices()) and ( + lights_map := get_lights(data=smart_devices) + ): logger.debug(lights_map) else: - logger.warning("%s is empty for lights.", models.fileio.smart_devices) + logger.warning( + "Smart devices are not configured for lights in %s", + models.fileio.smart_devices, + ) support.no_env_vars() return diff --git a/jarvis/executors/tv.py b/jarvis/executors/tv.py index 06531862..4701a824 100644 --- a/jarvis/executors/tv.py +++ b/jarvis/executors/tv.py @@ -101,17 +101,15 @@ def television(phrase: str) -> None: if not internet.vpn_checker(): return - smart_devices = files.get_smart_devices() - if smart_devices is False: - speaker.speak( - text=f"I'm sorry {models.env.title}! I wasn't able to read the source information." - ) - return - if smart_devices and (tv_mapping := get_tv(data=smart_devices)): + if (smart_devices := files.get_smart_devices()) and ( + tv_mapping := get_tv(data=smart_devices) + ): tv_map, key = tv_mapping logger.debug("%s stored with key: '%s'", tv_map, key) else: - logger.warning("%s is empty for tv.", models.fileio.smart_devices) + logger.warning( + "Smart devices are not configured for tv in %s", models.fileio.smart_devices + ) support.no_env_vars() return diff --git a/jarvis/modules/logger.py b/jarvis/modules/logger.py index 62d77204..61c9a15f 100644 --- a/jarvis/modules/logger.py +++ b/jarvis/modules/logger.py @@ -5,7 +5,7 @@ from logging import Formatter from logging.config import dictConfig -from pydantic import BaseModel +from pydantic import BaseModel, Field from jarvis.modules.builtin_overrides import AddProcessName from jarvis.modules.models import models @@ -84,53 +84,55 @@ class APIConfig(BaseModel): ) ERROR_LOG_FORMAT: str = "%(levelname)s\t %(message)s" - LOG_CONFIG: dict = { - "version": 1, - "disable_existing_loggers": True, - "formatters": { - "default": { - "()": "uvicorn.logging.DefaultFormatter", - "fmt": DEFAULT_LOG_FORM, - "use_colors": False, - }, - "access": { - "()": "uvicorn.logging.AccessFormatter", - "fmt": ACCESS_LOG_FORMAT, - "use_colors": False, - }, - "error": { - "()": "uvicorn.logging.DefaultFormatter", - "fmt": ERROR_LOG_FORMAT, - "use_colors": False, + LOG_CONFIG: dict = Field( + { + "version": 1, + "disable_existing_loggers": True, + "formatters": { + "default": { + "()": "uvicorn.logging.DefaultFormatter", + "fmt": DEFAULT_LOG_FORM, + "use_colors": False, + }, + "access": { + "()": "uvicorn.logging.AccessFormatter", + "fmt": ACCESS_LOG_FORMAT, + "use_colors": False, + }, + "error": { + "()": "uvicorn.logging.DefaultFormatter", + "fmt": ERROR_LOG_FORMAT, + "use_colors": False, + }, }, - }, - "handlers": { - "default": { - "formatter": "default", - "class": "logging.FileHandler", - "filename": DEFAULT_LOG_FILENAME, + "handlers": { + "default": { + "formatter": "default", + "class": "logging.FileHandler", + "filename": DEFAULT_LOG_FILENAME, + }, + "access": { + "formatter": "access", + "class": "logging.FileHandler", + "filename": ACCESS_LOG_FILENAME, + }, + "error": { + "formatter": "error", + "class": "logging.FileHandler", + "filename": DEFAULT_LOG_FILENAME, + }, }, - "access": { - "formatter": "access", - "class": "logging.FileHandler", - "filename": ACCESS_LOG_FILENAME, + "loggers": { + "uvicorn": {"handlers": ["default"], "level": DEFAULT_LOG_LEVEL}, + "uvicorn.access": {"handlers": ["access"], "level": DEFAULT_LOG_LEVEL}, + "uvicorn.error": { + "handlers": ["error"], + "level": DEFAULT_LOG_LEVEL, + "propagate": True, # Since FastAPI is threaded + }, }, - "error": { - "formatter": "error", - "class": "logging.FileHandler", - "filename": DEFAULT_LOG_FILENAME, - }, - }, - "loggers": { - "uvicorn": {"handlers": ["default"], "level": DEFAULT_LOG_LEVEL}, - "uvicorn.access": {"handlers": ["access"], "level": DEFAULT_LOG_LEVEL}, - "uvicorn.error": { - "handlers": ["error"], - "level": DEFAULT_LOG_LEVEL, - "propagate": True, # Since FastAPI is threaded - }, - }, - } + } + ) def log_file(filename: str) -> str: