apb_extra_utils

Package apb_extra_utils

Modules used on different commonly cases founded in typical projects dealing with strings, mails, logging, sql, xml, excel, postgres, Github api and others.

To install:

pip install apb_extra_utils

Documentation here apb_extra_utils

 1#   coding=utf-8
 2#  #
 3#   Author: ernst
 4#   File: __init__.py
 5#   Created: 29/01/2020, 19:11
 6#   Last modified: 29/01/2020, 19:10
 7#   Copyright (c) 2020
 8"""
 9.. include:: ../README.md
10"""
11
12
13__path__ = __import__('pkgutil').extend_path(__path__, __name__)
14
15# EAM - Para evitar error ddl al usar OSGEO.GDAL se asegura import antes de hacer uso del modulo logging
16try:
17    __import__("osgeo")
18except:
19    pass
20
21import logging
22import logging.config
23import os
24import yaml
25from .misc import get_environ
26
27
28__conf_logging__ = os.path.join(os.path.dirname(__file__), "logging.conf.yml")
29__nom_env_config_logging__ = "LOGGING_CONF_PATH"
30
31if __nom_env_config_logging__ in os.environ:
32    if os.getenv(__nom_env_config_logging__).strip():
33        __conf_logging__ = os.getenv(__nom_env_config_logging__).strip()
34
35
36def set_logging_config(log_config_path):
37    """
38    Configura el logging de sesión
39
40    Args:
41        log_name_file {str, optional}: Si asignado entonces carga la configuracion de logging de ahí.
42                Defecto "logging.conf.yml"
43        log_level {obj, optional}: Nivel de logging (logging.DEBUG, logging.INFO, logging.WARNING,...). Por defecto INFO
44
45    Returns:
46        str: Retorna el path del fichero cargado
47
48    """
49    info_msg = None
50    if os.path.exists(log_config_path):
51        with open(log_config_path, 'rt') as f:
52            config = yaml.safe_load(f.read())
53        logging.config.dictConfig(config)
54        info_msg = "Cargado fichero de configuracion de LOGGING '{}'".format(log_config_path)
55    else:
56        logging.basicConfig()
57
58    if get_environ() == "prod":
59        set_level_logging(logging.INFO)
60
61    root_logger = get_root_logger()
62    if info_msg:
63        root_logger.info(info_msg)
64
65
66def get_root_logger():
67    """
68    Devuelve el logger base del modulo logging
69
70    Returns:
71        logging.logger
72    """
73    return logging.getLogger()
74
75
76def set_level_logging(a_logging_lvl=logging.INFO):
77    """
78    Asigna nivel de logging por el que se mostrarán mensajes
79
80    Args:
81        a_logging_lvl: (logging.DEBUG, logging.INFO, logging.WARNING, ...)
82
83    Returns:
84    """
85    root_logger = get_root_logger()
86    root_logger.setLevel(a_logging_lvl)
87
88
89set_logging_config(__conf_logging__)
def set_logging_config(log_config_path):
37def set_logging_config(log_config_path):
38    """
39    Configura el logging de sesión
40
41    Args:
42        log_name_file {str, optional}: Si asignado entonces carga la configuracion de logging de ahí.
43                Defecto "logging.conf.yml"
44        log_level {obj, optional}: Nivel de logging (logging.DEBUG, logging.INFO, logging.WARNING,...). Por defecto INFO
45
46    Returns:
47        str: Retorna el path del fichero cargado
48
49    """
50    info_msg = None
51    if os.path.exists(log_config_path):
52        with open(log_config_path, 'rt') as f:
53            config = yaml.safe_load(f.read())
54        logging.config.dictConfig(config)
55        info_msg = "Cargado fichero de configuracion de LOGGING '{}'".format(log_config_path)
56    else:
57        logging.basicConfig()
58
59    if get_environ() == "prod":
60        set_level_logging(logging.INFO)
61
62    root_logger = get_root_logger()
63    if info_msg:
64        root_logger.info(info_msg)

Configura el logging de sesión

Arguments:
  • log_name_file {str, optional}: Si asignado entonces carga la configuracion de logging de ahí. Defecto "logging.conf.yml"
  • log_level {obj, optional}: Nivel de logging (logging.DEBUG, logging.INFO, logging.WARNING,...). Por defecto INFO
Returns:

str: Retorna el path del fichero cargado

def get_root_logger():
67def get_root_logger():
68    """
69    Devuelve el logger base del modulo logging
70
71    Returns:
72        logging.logger
73    """
74    return logging.getLogger()

Devuelve el logger base del modulo logging

Returns:

logging.logger

def set_level_logging(a_logging_lvl=20):
77def set_level_logging(a_logging_lvl=logging.INFO):
78    """
79    Asigna nivel de logging por el que se mostrarán mensajes
80
81    Args:
82        a_logging_lvl: (logging.DEBUG, logging.INFO, logging.WARNING, ...)
83
84    Returns:
85    """
86    root_logger = get_root_logger()
87    root_logger.setLevel(a_logging_lvl)

Asigna nivel de logging por el que se mostrarán mensajes

Arguments:
  • a_logging_lvl: (logging.DEBUG, logging.INFO, logging.WARNING, ...)

Returns: