Skip to content

Commit

Permalink
refactor logging, misc refactors (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
jziolkowski committed Sep 15, 2024
1 parent 40a142e commit f945d5e
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 92 deletions.
32 changes: 11 additions & 21 deletions tdmgr/GUI/dialogs/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import csv
import logging
import re
from logging.handlers import TimedRotatingFileHandler

from PyQt5.QtCore import QDir, QFileInfo, QSettings, QSize, Qt, QTimer, QUrl, pyqtSlot
from PyQt5.QtGui import QDesktopServices, QFont, QIcon
Expand Down Expand Up @@ -34,14 +33,15 @@
from tdmgr.util.discovery import lwt_discovery_stage2
from tdmgr.util.mqtt import DEFAULT_PATTERNS, Message, MqttClient, expand_fulltopic

log = logging.getLogger(__name__)


class MainWindow(QMainWindow):
def __init__(
self,
version: str,
settings: QSettings,
devices: QSettings,
log_path: str,
debug: bool,
*args,
**kwargs,
Expand Down Expand Up @@ -69,17 +69,6 @@ def __init__(
self.devices = devices
self.setMinimumSize(QSize(1000, 600))

# configure logging
logging.basicConfig(
level="DEBUG" if debug else "INFO",
datefmt="%Y-%m-%d %H:%M:%S",
format="%(asctime)s [%(levelname)s] %(message)s",
)
logging.getLogger().addHandler(
TimedRotatingFileHandler(filename=log_path, when="d", interval=1)
)
logging.info("### TDM START ###")

# load devices from the devices file, create TasmotaDevices and add the to the environment
for mac in self.devices.childGroups():
self.devices.beginGroup(mac)
Expand Down Expand Up @@ -129,6 +118,7 @@ def __init__(

self.tele_docks = []
self.consoles = []
log.info(f"### TDM {self._version} START ###")

def setup_main_layout(self):
self.mdi = QMdiArea()
Expand Down Expand Up @@ -352,7 +342,7 @@ def mqtt_message(self, msg: Message):
# try:
# obj = DiscoverySchema.model_validate_json(msg.payload)
# except ValueError:
# logging.error("Unable to parse Tasmota discovery message: %s", msg.payload)
# log.error("Unable to parse Tasmota discovery message: %s", msg.payload)
#
# if obj and not self.env.find_device(obj.t):
# device = TasmotaDevice.from_discovery(obj)
Expand All @@ -363,7 +353,7 @@ def mqtt_message(self, msg: Message):
#
# self.env.devices.append(device)
# self.device_model.addDevice(device)
# logging.info(
# log.info(
# "DISCOVERY(NATIVE): Discovered topic=%s with fulltopic=%s",
# obj.t,
# device.p["FullTopic"],
Expand All @@ -375,7 +365,7 @@ def mqtt_message(self, msg: Message):

if device := self.env.find_device(msg.topic):
if msg.is_lwt:
logging.debug("MQTT: LWT message for %s: %s", device.p["Topic"], msg.payload)
log.debug("MQTT: LWT message for %s: %s", device.p["Topic"], msg.payload)
device.update_property("LWT", msg.payload)

if msg.payload == device.p["Online"]:
Expand All @@ -396,7 +386,7 @@ def mqtt_message(self, msg: Message):
# unknown device, start autodiscovery process
if msg.is_lwt:
self.env.lwts[msg.topic] = msg.payload
logging.info("DISCOVERY(LEGACY): LWT from an unknown device %s", msg.topic)
log.info("DISCOVERY(LEGACY): LWT from an unknown device %s", msg.topic)

# STAGE 1
# load default and user-provided FullTopic patterns and for all the patterns,
Expand All @@ -418,7 +408,7 @@ def mqtt_message(self, msg: Message):
"/LWT", "/FullTopic"
)
)
logging.debug(
log.debug(
"DISCOVERY(LEGACY): Asking an unknown device for FullTopic at %s",
possible_topic_cmnd,
)
Expand All @@ -429,7 +419,7 @@ def mqtt_message(self, msg: Message):
if d := lwt_discovery_stage2(self.env, msg):
self.env.devices.append(d)
self.device_model.addDevice(d)
logging.debug("DISCOVERY: Sending initial query to topic %s", d.p["Topic"])
log.debug("DISCOVERY: Sending initial query to topic %s", d.p["Topic"])
self.initial_query(d, True)
tele_topic = d.tele_topic("LWT")
self.env.lwts.pop(tele_topic, None)
Expand Down Expand Up @@ -496,7 +486,7 @@ def clear_retained_topics(self):
topic = itm.text()
self.mqtt.publish(topic, retain=True)
self.env.retained.remove(topic)
logging.info("MQTT: Cleared %s", topic)
log.info("MQTT: Cleared %s", topic)

def prefs(self):
dlg = PrefsDialog(self.settings)
Expand Down Expand Up @@ -535,7 +525,7 @@ def open_config_file(self):

@staticmethod
def open_log_location():
fi = QFileInfo(logging.getLogger().handlers[1].baseFilename)
fi = QFileInfo(log.getLogger().handlers[1].baseFilename)
QDesktopServices.openUrl(QUrl.fromLocalFile(fi.absolutePath()))

def auto_telemetry_period(self):
Expand Down
46 changes: 25 additions & 21 deletions tdmgr/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import pathlib
import sys
from logging.handlers import TimedRotatingFileHandler

from PyQt5.QtCore import QDir, QSettings
from PyQt5.QtWidgets import QApplication
Expand All @@ -17,31 +18,33 @@
version = ""


def get_settings(args):
if args.local:
return QSettings("tdm.cfg", QSettings.IniFormat)
if args.config_location:
return QSettings(os.path.join(args.config_location, "tdm.cfg"), QSettings.IniFormat)
return QSettings(QSettings.IniFormat, QSettings.UserScope, "tdm", "tdm")
def configure_logging(args) -> None:
log_path = os.path.join(QDir.tempPath(), "tdm.log")


def get_devices(args):
if args.local:
return QSettings("devices.cfg", QSettings.IniFormat)
if args.config_location:
return QSettings(os.path.join(args.config_location, "devices.cfg"), QSettings.IniFormat)
return QSettings(QSettings.IniFormat, QSettings.UserScope, "tdm", "devices")
log_path = "tdm.log"
elif args.log_location:
log_path = os.path.join(args.log_location, "tdm.log")

logging.basicConfig(
level="DEBUG" if args.debug else "INFO",
datefmt="%Y-%m-%d %H:%M:%S",
format="%(asctime)s [%(levelname)s] [%(filename)s] %(message)s",
)
logging.getLogger(__name__).addHandler(
TimedRotatingFileHandler(filename=log_path, when="d", interval=1)
)


def get_log_path(args):
def get_settings(args: argparse.Namespace, filename: str) -> QSettings:
if args.local:
return "tdm.log"
if args.log_location:
return os.path.join(args.log_location, "tdm.log")
return os.path.join(QDir.tempPath(), "tdm.log")
return QSettings(f"{filename}.ini", QSettings.IniFormat)
if args.config_location:
return QSettings(os.path.join(args.config_location, f"{filename}.ini"), QSettings.IniFormat)
return QSettings(QSettings.IniFormat, QSettings.UserScope, "tdm", f"{filename}.ini")


def setup_parser():
def setup_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(prog='Tasmota Device Manager')
parser.add_argument('--debug', action='store_true', help='Enable debugging')
parser.add_argument(
Expand All @@ -54,17 +57,18 @@ def setup_parser():
return parser


def start():
def start() -> None:
parser = setup_parser()
args = parser.parse_args()
configure_logging(args)

try:
app = QApplication(sys.argv)
app.lastWindowClosed.connect(app.quit)
app.setStyle("Fusion")

settings, devices, log_path = get_settings(args), get_devices(args), get_log_path(args)
MW = MainWindow(version, settings, devices, log_path, args.debug)
settings, devices = get_settings(args, "tdm"), get_settings(args, "devices")
MW = MainWindow(version, settings, devices, args.debug)
MW.show()
sys.exit(app.exec_())

Expand Down
13 changes: 0 additions & 13 deletions tdmgr/schemas/common.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
import logging
from enum import Enum

from pydantic import BaseModel, ConfigDict, model_validator


class OnOffEnum(str, Enum):
ON = "ON"
OFF = "OFF"


class TDMBaseModel(BaseModel):
model_config = ConfigDict(extra="allow")

@model_validator(mode="after")
def log_extra_fields(cls, values):
if cls.__name__ != "StatusSNSSchema" and values.model_extra:
logging.warning("%s has extra fields: %s", cls.__name__, values.model_extra)
return values
Loading

0 comments on commit f945d5e

Please sign in to comment.