Skip to content

Commit

Permalink
Update src/enrich/__init__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
saforem2 committed Feb 6, 2024
1 parent 2391357 commit d265c0f
Showing 1 changed file with 142 additions and 2 deletions.
144 changes: 142 additions & 2 deletions src/enrich/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,82 @@
from __future__ import absolute_import, annotations, division, print_function
import logging
import os
import yaml
from typing import Optional
from enrich.console import get_console
from enrich.handler import RichHandler
from enrich.handler import RichHandler as EnrichHandler
from enrich.config import STYLES
import logging.config

os.environ['PYTHONIOENCODING'] = 'utf-8'


def get_enrich_logging_config_as_yaml(
name: str = 'enrich',
level: str = 'INFO') -> str:
return fr"""
---
# version: 1
handlers:
{name}:
(): enrich.handler.RichHandler
show_time: true
show_level: true
enable_link_path: false
level: {level.upper()}
root:
handlers: [{name}]
disable_existing_loggers: false
...
"""


# def get_logging_config_as_yaml(level: str = 'DEBUG') -> str:
# # >>> import yaml
# # >>>
# # >>> names_yaml = """
# # ... - 'eric'
# # ... - 'justin'
# # ... - 'mary-kate'
# # ... """
# return fr"""
# handlers:
# term:
# class: enrich.handler.RichHandler
# show_time: true
# show_level: true
# enable_link_path: false
# level: {level}
# root:
# handlers: [term]
# disable_existing_loggers: false
# """
#

# def get_logging_config(level: str = 'INFO') -> logging.config.dictConfig:
# config = yaml.safe_load(get_logging_config_as_yaml(level=level))
# # with Path('logconf.yaml').open('r') as stream:
# # config = yaml.load(stream, Loader=yaml.FullLoader)
# return logging.config.dictConfig(config)


def get_logger_new(
name: str,
level: str = 'INFO',
):
config = yaml.safe_load(
get_enrich_logging_config_as_yaml(
name=name,
level=level
),
)
# # config = yaml.load(stream, Loader=yaml.FullLoader)
logging.config.dictConfig(config)
log = logging.getLogger(name=name)
log.setLevel(level)
return log


def get_file_logger(
name: Optional[str] = None,
level: str = 'INFO',
Expand Down Expand Up @@ -45,6 +113,17 @@ def get_file_logger(
return log


def get_active_enrich_handlers(logger: logging.Logger) -> list:
return (
[
(idx, h) for idx, h in enumerate(logger.handlers) if isinstance(
h,
EnrichHandler
)
]
)


def get_logger(
name: Optional[str] = None,
level: str = 'INFO',
Expand All @@ -63,7 +142,7 @@ def get_logger(
if console.is_jupyter:
console.is_jupyter = False
log.addHandler(
RichHandler(
EnrichHandler(
omit_repeated_times=False,
level=level,
console=console,
Expand All @@ -79,6 +158,38 @@ def get_logger(
and all([i == log.handlers[0] for i in log.handlers])
):
log.handlers = [log.handlers[0]]
enrich_handlers = get_active_enrich_handlers(log)
# enrich_handlers = (
# [
# (idx, h) for idx, h in enumerate(log.handlers) if isinstance(
# h,
# EnrichHandler
# )
# ]
# )
found_handlers = 0
if len(enrich_handlers) > 1:
for h in log.handlers:
if isinstance(h, EnrichHandler):
if found_handlers > 1:
log.warning(
'More than one `EnrichHandler` in current logger: '
f'{log.handlers}'
)
log.removeHandler(h)
found_handlers += 1
if len(get_active_enrich_handlers(log)) > 1:
log.warning(
'More than one `EnrichHandler` in current logger: '
f'{log.handlers}'
)
# log.warning(f'Using {enrich_handlers[-1][1]}')
# log.removeHandler(log.handlers[enrich_handlers[-1][0]])
# # log.handlers = enrich_handlers[-1]
# # assert (
# len() == 1
# # )

return log


Expand All @@ -104,5 +215,34 @@ def print_styles():
f.write(console.export_html(inline_styles=True))


def print_styles_alt(
html: bool = False,
txt: bool = False,
):
from pathlib import Path
from rich.text import Text
from enrich.style import DEFAULT_STYLES
from rich.table import Table
console = get_console(record=html, width=150)
table = Table("Name", "Styling")
styles = DEFAULT_STYLES
styles |= STYLES
for style_name, style in styles.items():
table.add_row(Text(style_name, style=style), str(style))
console.print(table)
if html:
outfile = 'enrich_styles.html'
print(f'Saving to `{outfile}`')
with open(outfile, 'w') as f:
f.write(console.export_html(inline_styles=True))
if txt:
file1 = "enrich_styles.txt"
text = console.export_text()
# with open(file1, "w") as file:
with Path(file1).open('w') as file:
file.write(text)



if __name__ == "__main__": # pragma: no cover
print_styles()

0 comments on commit d265c0f

Please sign in to comment.