diff options
author | Michal Jagiello <michal.jagiello@t-mobile.pl> | 2020-07-02 12:34:25 +0000 |
---|---|---|
committer | Michal Jagiello <michal.jagiello@t-mobile.pl> | 2020-07-13 08:57:03 +0000 |
commit | b437b2c38ec0d5bb234fef0459b0e0533d08b7af (patch) | |
tree | a234cd12effde9854b77ea3ceaa6848a80a241b0 /pylog/onaplogging/colorFormatter.py | |
parent | d80e67cc8994fb0b92c8c1b5e3f51bde21b4a791 (diff) |
Pylog test suite
Prepare test suite for onaplogging library.
Add utils to check Python version.
Issue-ID: LOG-1229
Signed-off-by: Michal Jagiello <michal.jagiello@t-mobile.pl>
Change-Id: I12c431c10f61fceaf70d5ae36581dfd5a876ef72
Diffstat (limited to 'pylog/onaplogging/colorFormatter.py')
-rw-r--r-- | pylog/onaplogging/colorFormatter.py | 22 |
1 files changed, 9 insertions, 13 deletions
diff --git a/pylog/onaplogging/colorFormatter.py b/pylog/onaplogging/colorFormatter.py index 64e220a..ef713ea 100644 --- a/pylog/onaplogging/colorFormatter.py +++ b/pylog/onaplogging/colorFormatter.py @@ -14,9 +14,10 @@ import os import sys -import logging from logging import Formatter +from .utils import is_above_python_2_7, is_above_python_3_2 + ATTRIBUTES = { 'normal': 0, @@ -57,7 +58,8 @@ COLOR_TAG = "color" HIGHLIGHT_TAG = "highlight" ATTRIBUTE_TAG = "attribute" -RESET = '\033[0m' +RESET = "\033[0m" +FMT_STR = "\033[%dm%s" def colored(text, color=None, on_color=None, attrs=None): @@ -70,16 +72,15 @@ def colored(text, color=None, on_color=None, attrs=None): attrs = [attrs] if os.getenv('ANSI_COLORS_DISABLED', None) is None: - fmt_str = '\033[%dm%s' if color is not None and isinstance(color, str): - text = fmt_str % (COLORS.get(color, 0), text) + text = FMT_STR % (COLORS.get(color, 0), text) if on_color is not None and isinstance(on_color, str): - text = fmt_str % (HIGHLIGHTS.get(on_color, 0), text) + text = FMT_STR % (HIGHLIGHTS.get(on_color, 0), text) if attrs is not None: for attr in attrs: - text = fmt_str % (ATTRIBUTES.get(attr, 0), text) + text = FMT_STR % (ATTRIBUTES.get(attr, 0), text) # keep origin color for tail spaces text += RESET @@ -89,20 +90,15 @@ def colored(text, color=None, on_color=None, attrs=None): class BaseColorFormatter(Formatter): def __init__(self, fmt=None, datefmt=None, colorfmt=None, style="%"): - if sys.version_info > (3, 2): + if is_above_python_3_2(): super(BaseColorFormatter, self).__init__( fmt=fmt, datefmt=datefmt, style=style) - elif sys.version_info > (2, 7): + elif is_above_python_2_7(): super(BaseColorFormatter, self).__init__(fmt, datefmt) else: Formatter.__init__(self, fmt, datefmt) self.style = style - if sys.version_info > (3, 2): - if self.style not in logging._STYLES: - raise ValueError('Style must be one of: %s' % ','.join( - logging._STYLES.keys())) - self.colorfmt = colorfmt def _parseColor(self, record): |