-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MOLEXTRACT_LOG_LEVEL with debugging capabilities (#12)
- Loading branch information
1 parent
413aa78
commit a303a00
Showing
6 changed files
with
150 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from .rule import Rule | ||
from .parser import Parser | ||
|
||
__version__ = '1.0.0' | ||
__version__ = '1.0.0' |
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,69 @@ | ||
""" | ||
Utility functions to debug / log what is happening within in a Rule | ||
""" | ||
import os | ||
import sys | ||
import logging | ||
|
||
LOG_NAME_TO_LEVEL = { | ||
'CRITICAL': logging.CRITICAL, | ||
'FATAL': logging.FATAL, | ||
'ERROR': logging.ERROR, | ||
'WARN': logging.WARNING, | ||
'WARNING': logging.WARNING, | ||
'INFO': logging.INFO, | ||
'DEBUG': logging.DEBUG, | ||
'NOTSET': logging.NOTSET, | ||
} | ||
|
||
MOLEXTRACT_LOG_LEVEL = os.getenv('MOLEXTRACT_LOG_LEVEL', 'INFO') | ||
ESC_CHAR = '\x1b[' | ||
TAG_LEFT_JUST = 100 | ||
VERB_LEFT_JUST = 12 | ||
|
||
logger = logging.getLogger('molextract') | ||
handler = logging.StreamHandler() | ||
logger.addHandler(handler) | ||
logger.setLevel(MOLEXTRACT_LOG_LEVEL) | ||
|
||
_COLOR_CODES = { | ||
'BLACK': 30, | ||
'RED': 31, | ||
'GREEN': 32, | ||
'YELLOW': 33, | ||
'BLUE': 34, | ||
'MAGENTA': 35, | ||
'CYAN': 36, | ||
'WHITE': 37, | ||
'RESET': 0 | ||
} | ||
|
||
|
||
class ColorType(type): | ||
def __getattr__(self, item): | ||
code = _COLOR_CODES.get(item) | ||
if code is None: | ||
raise ValueError(f'{item} is not a valid color') | ||
|
||
if sys.stderr.isatty(): | ||
return f'{ESC_CHAR}{code}m' | ||
|
||
return '' | ||
|
||
|
||
class Color(metaclass=ColorType): | ||
pass | ||
|
||
|
||
def log_start_tag(start_tag, rule_id): | ||
verb = "executed".ljust(VERB_LEFT_JUST) | ||
start_tag = start_tag.ljust(TAG_LEFT_JUST) | ||
msg = f'{start_tag} {Color.GREEN}{verb}{Color.RESET} {rule_id}' | ||
logger.debug(msg) | ||
|
||
|
||
def log_end_tag(end_tag, rule_id): | ||
verb = "ended".ljust(VERB_LEFT_JUST) | ||
end_tag = end_tag.ljust(TAG_LEFT_JUST) | ||
msg = f'{end_tag} {Color.RED}{verb}{Color.RESET} {rule_id}' | ||
logger.debug(msg) |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from setuptools import setup | ||
|
||
setup() | ||
setup() |
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,24 @@ | ||
from unittest import mock | ||
import pytest | ||
|
||
from molextract import debug | ||
|
||
|
||
@mock.patch('sys.stderr.isatty', return_value=True) | ||
def test_color(mock_isatty): | ||
for color in debug._COLOR_CODES: | ||
code = debug._COLOR_CODES[color] | ||
string = f'{debug.ESC_CHAR}{code}m' | ||
assert debug.Color.__getattr__(color) == string | ||
|
||
with pytest.raises(ValueError, match='not a valid color'): | ||
debug.Color.ORANGE | ||
|
||
|
||
@mock.patch('sys.stderr.isatty') | ||
def test_color_notty(mock_isatty): | ||
mock_isatty.return_value = True | ||
assert len(debug.Color.RED) > 0 | ||
|
||
mock_isatty.return_value = False | ||
assert debug.Color.RED == '' |
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