From ef9c395346977265359144b5e0f2d8d46c5b5e01 Mon Sep 17 00:00:00 2001 From: marcosrmartin Date: Sun, 2 Jun 2024 16:55:19 +0000 Subject: [PATCH] Funcionalidad y configuracion comun #51 #52 --- mass/__init__.py | 0 mass/config.py | 39 +++++++++++++++++++++++++++++++++++++++ mass/decorators.py | 26 ++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 mass/__init__.py create mode 100644 mass/config.py create mode 100644 mass/decorators.py diff --git a/mass/__init__.py b/mass/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mass/config.py b/mass/config.py new file mode 100644 index 0000000..e485c06 --- /dev/null +++ b/mass/config.py @@ -0,0 +1,39 @@ +import logging +import signal + +NGINX = "NGINX" +HTTPD = "HTTPD" +PROXY = "PROXY" + +PORT = { + NGINX: "8009", + HTTPD: "8008", + PROXY: "80" +} + +# Logger configuration +logging.basicConfig( + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' +) + +# Get a specific logger for the package +logger = logging.getLogger('MASS') + +def configure_logging(level=logging.INFO): + logger.setLevel(level) + for handler in logger.handlers: + handler.setLevel(level) + +_shutdown = False + +def get_shutdown(): + return _shutdown + + +def signal_handler(sig, frame): + global _shutdown + if not _shutdown: + logger.info("\nShutting down...") + _shutdown = True + +signal.signal(signal.SIGINT, signal_handler) diff --git a/mass/decorators.py b/mass/decorators.py new file mode 100644 index 0000000..a490a7d --- /dev/null +++ b/mass/decorators.py @@ -0,0 +1,26 @@ +import time +import functools +from .config import logger + +def timing_decorator(func): + @functools.wraps(func) + def wrapper(*args, **kwargs): + start_time = time.time() + result = func(*args, **kwargs) + end_time = time.time() + elapsed_time = end_time - start_time + logger.debug(f"Method {func.__name__} execution took: {elapsed_time:.6f} seconds.") + return result + return wrapper + +def apply_timing_to_methods(decorator): + def decorate(cls): + for attr in dir(cls): + if attr.startswith("__") and attr.endswith("__"): + continue + original_method = getattr(cls, attr) + if callable(original_method): + decorated_method = decorator(original_method) + setattr(cls, attr, decorated_method) + return cls + return decorate