-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9daa336
commit 5ed6825
Showing
9 changed files
with
237 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import logging | ||
from colorama import Fore, Style, init | ||
|
||
initialized = False | ||
|
||
def setup_logging(level: int = logging.INFO): | ||
global initialized | ||
if initialized: | ||
return | ||
# Initialize colorama for cross-platform colored output | ||
init(autoreset=True) | ||
|
||
# Create a custom formatter | ||
class ColoredFormatter(logging.Formatter): | ||
FORMATS = { | ||
logging.DEBUG: Fore.CYAN + "[%(asctime)s] %(levelname)-8s %(name)s: %(message)s" + Style.RESET_ALL, | ||
logging.INFO: Fore.GREEN + "[%(asctime)s] %(levelname)-8s %(name)s: %(message)s" + Style.RESET_ALL, | ||
logging.WARNING: Fore.YELLOW + "[%(asctime)s] %(levelname)-8s %(name)s: %(message)s" + Style.RESET_ALL, | ||
logging.ERROR: Fore.RED + "[%(asctime)s] %(levelname)-8s %(name)s: %(message)s" + Style.RESET_ALL, | ||
logging.CRITICAL: Fore.RED + Style.BRIGHT + "[%(asctime)s] %(levelname)-8s %(name)s: %(message)s" + Style.RESET_ALL | ||
} | ||
|
||
def format(self, record: logging.LogRecord) -> str: | ||
log_fmt = self.FORMATS.get(record.levelno) | ||
formatter = logging.Formatter(log_fmt, datefmt="%Y-%m-%d %H:%M:%S") | ||
return formatter.format(record) | ||
|
||
# Create and configure the logger | ||
logger = logging.getLogger("ell") | ||
logger.setLevel(level) | ||
|
||
# Create console handler and set formatter | ||
console_handler = logging.StreamHandler() | ||
console_handler.setFormatter(ColoredFormatter()) | ||
|
||
# Add the handler to the logger | ||
logger.addHandler(console_handler) | ||
initialized = True | ||
|
||
return logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
import aiomqtt | ||
|
||
|
||
class Publisher(ABC): | ||
@abstractmethod | ||
async def publish(self, topic: str, message: str) -> None: | ||
pass | ||
|
||
|
||
class MqttPub(Publisher): | ||
def __init__(self, conn: aiomqtt.Client): | ||
self.mqtt_client = conn | ||
|
||
async def publish(self, topic: str, message: str) -> None: | ||
await self.mqtt_client.publish(topic, message) | ||
|
||
|
||
class NoopPublisher(Publisher): | ||
async def publish(self, topic: str, message: str) -> None: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.