diff options
author | 2020-07-02 12:34:25 +0000 | |
---|---|---|
committer | 2020-07-13 08:57:03 +0000 | |
commit | b437b2c38ec0d5bb234fef0459b0e0533d08b7af (patch) | |
tree | a234cd12effde9854b77ea3ceaa6848a80a241b0 /pylog/onaplogging | |
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')
-rw-r--r-- | pylog/onaplogging/colorFormatter.py | 22 | ||||
-rw-r--r-- | pylog/onaplogging/marker/markerHandler.py | 2 | ||||
-rw-r--r-- | pylog/onaplogging/mdcContext.py | 2 | ||||
-rw-r--r-- | pylog/onaplogging/mdcformatter.py | 16 | ||||
-rw-r--r-- | pylog/onaplogging/utils.py | 31 |
5 files changed, 50 insertions, 23 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): diff --git a/pylog/onaplogging/marker/markerHandler.py b/pylog/onaplogging/marker/markerHandler.py index 12b5488..e9ce810 100644 --- a/pylog/onaplogging/marker/markerHandler.py +++ b/pylog/onaplogging/marker/markerHandler.py @@ -44,7 +44,7 @@ class MarkerNotifyHandler(SMTPHandler): if matchMarkerHelp(record, self.markers): if sys.version_info > (2, 7): - return super(SMTPHandler, self).handle(record) + return super(MarkerNotifyHandler, self).handle(record) else: return SMTPHandler.handle(self, record) diff --git a/pylog/onaplogging/mdcContext.py b/pylog/onaplogging/mdcContext.py index fa94536..ecdc2d9 100644 --- a/pylog/onaplogging/mdcContext.py +++ b/pylog/onaplogging/mdcContext.py @@ -48,7 +48,7 @@ class MDCContext(threading.local): def remove(self, key): - if key in self.localDict: + if key in self._localDict: del self._localDict[key] def clear(self): diff --git a/pylog/onaplogging/mdcformatter.py b/pylog/onaplogging/mdcformatter.py index 545a4c1..4cacbe8 100644 --- a/pylog/onaplogging/mdcformatter.py +++ b/pylog/onaplogging/mdcformatter.py @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -import sys import logging from .markerFormatter import MarkerFormatter +from .utils import is_above_python_2_7, is_above_python_3_2 class MDCFormatter(MarkerFormatter): @@ -33,12 +33,12 @@ class MDCFormatter(MarkerFormatter): :param colorfmt: colored output with ANSI escape code on terminal :param style: style mapping keys in python3 """ - if sys.version_info > (3, 2): + if is_above_python_3_2(): super(MDCFormatter, self).__init__(fmt=fmt, datefmt=datefmt, colorfmt=colorfmt, style=style) - elif sys.version_info > (2, 7): + elif is_above_python_2_7(): super(MDCFormatter, self).__init__(fmt=fmt, datefmt=datefmt, colorfmt=colorfmt) @@ -112,7 +112,7 @@ class MDCFormatter(MarkerFormatter): """ mdcIndex = self._fmt.find(self._mdc_tag) if mdcIndex == -1: - if sys.version_info > (2, 7): + if is_above_python_2_7(): return super(MDCFormatter, self).format(record) else: return MarkerFormatter.format(self, record) @@ -121,10 +121,10 @@ class MDCFormatter(MarkerFormatter): if mdcFmtWords is None: self._fmt = self._fmt.replace(self._mdc_tag, "") - if sys.version_info > (3, 2): + if is_above_python_3_2(): self._style = logging._STYLES[self.style][0](self._fmt) - if sys.version_info > (2, 7): + if is_above_python_2_7(): return super(MDCFormatter, self).format(record) else: return MarkerFormatter.format(self, record) @@ -142,10 +142,10 @@ class MDCFormatter(MarkerFormatter): mdcstr = self._replaceStr(keys=mdcFmtkeys).format(**res) self._fmt = self._fmt.replace(self._mdc_tag, mdcstr) - if sys.version_info > (3, 2): + if is_above_python_3_2(): self._style = logging._STYLES[self.style][0](self._fmt) - if sys.version_info > (2, 7): + if is_above_python_2_7(): return super(MDCFormatter, self).format(record) else: return MarkerFormatter.format(self, record) diff --git a/pylog/onaplogging/utils.py b/pylog/onaplogging/utils.py new file mode 100644 index 0000000..5c96b2d --- /dev/null +++ b/pylog/onaplogging/utils.py @@ -0,0 +1,31 @@ +# Copyright (c) 2020 Deutsche Telekom. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + +import sys + + +def is_above_python_3_2(): # type: () -> bool + """Check if code is running at least on Python 3.2 version. + + Returns: + bool: True if it's at least 3.2 version, False otherwise + + """ + return sys.version_info >= (3, 2, 0, "final", 0) + + +def is_above_python_2_7(): # type: () -> bool + """Check if code is running at least on Python 2.7 version. + + Returns: + bool: True if it's at least 2.7 version, False otherwise + + """ + return sys.version_info >= (2, 7, 0, "final", 0) |