From 5bb4c45f94bd421a4d81fef3d2e6f19ef72d2140 Mon Sep 17 00:00:00 2001 From: CodeWriter21 Date: Thu, 18 Aug 2022 07:56:39 +0430 Subject: [PATCH] Minor fixes and improvements. --- CHANGES-LOG.md | 4 ++ README.md | 4 +- log21/CrashReporter/Reporters.py | 70 +++++++++++--------------------- log21/StreamHandler.py | 15 ++++--- log21/__init__.py | 12 +++--- setup.py | 2 +- 6 files changed, 43 insertions(+), 64 deletions(-) diff --git a/CHANGES-LOG.md b/CHANGES-LOG.md index 83b0a8a..408cc1c 100644 --- a/CHANGES-LOG.md +++ b/CHANGES-LOG.md @@ -6,6 +6,10 @@ Help this project by [Donation](DONATE.md) Changes log ----------- +### 2.3.9 + +Minor fixes and improvements. + ### 2.3.8 Added `catch` and `ignore` methods to `log21.CrashReporter.Reporter`. diff --git a/README.md b/README.md index 9f221eb..081e496 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,9 @@ python setup.py install Changes ------- -### 2.3.8 +### 2.3.9 -Added `catch` and `ignore` methods to `log21.CrashReporter.Reporter`. +Minor fixes and improvements. [Full Changes Log](https://github.com/MPCodeWriter21/log21/blob/master/CHANGES-LOG.md) diff --git a/log21/CrashReporter/Reporters.py b/log21/CrashReporter/Reporters.py index 771d933..28c8576 100644 --- a/log21/CrashReporter/Reporters.py +++ b/log21/CrashReporter/Reporters.py @@ -87,54 +87,20 @@ def reporter(self, func): :return: Wrapped function. """ - exceptions_to_catch = tuple(self._exceptions_to_catch) if self._exceptions_to_catch else None - exceptions_to_ignore = tuple(self._exceptions_to_ignore) if self._exceptions_to_ignore else None - - if exceptions_to_catch and exceptions_to_ignore: - @_wraps(func) - def wrap(*args, **kwargs): - try: - return func(*args, **kwargs) - except BaseException as e: - if isinstance(e, exceptions_to_catch) and not isinstance(e, exceptions_to_ignore): - self._reporter_function(e) - if self.raise_after_report: - raise e - else: - raise e - elif self._exceptions_to_catch: - @_wraps(func) - def wrap(*args, **kwargs): - try: - return func(*args, **kwargs) - except BaseException as e: - if isinstance(e, exceptions_to_catch): - self._reporter_function(e) - if self.raise_after_report: - raise e - else: - raise e - elif self._exceptions_to_ignore: - @_wraps(func) - def wrap(*args, **kwargs): - try: - return func(*args, **kwargs) - except BaseException as e: - if not isinstance(e, exceptions_to_ignore): - self._reporter_function(e) - if self.raise_after_report: - raise e - else: - raise e - else: - @_wraps(func) - def wrap(*args, **kwargs): - try: - return func(*args, **kwargs) - except BaseException as e: + exceptions_to_catch = tuple(self._exceptions_to_catch) if self._exceptions_to_catch else BaseException + exceptions_to_ignore = tuple(self._exceptions_to_ignore) if self._exceptions_to_ignore else tuple() + + @_wraps(func) + def wrap(*args, **kwargs): + try: + return func(*args, **kwargs) + except BaseException as e: + if isinstance(e, exceptions_to_catch) and not isinstance(e, exceptions_to_ignore): self._reporter_function(e) if self.raise_after_report: raise e + else: + raise e return wrap @@ -144,9 +110,14 @@ def catch(self, exception: BaseException): :param exception: Exception to catch. """ + if not isinstance(exception, BaseException): + raise TypeError('`exception` must be an instance of BaseException') if self._exceptions_to_catch is None: self._exceptions_to_catch = set() - self._exceptions_to_catch.add(exception) + if exception not in self._exceptions_to_catch: + self._exceptions_to_catch.add(exception) + else: + raise ValueError('exception is already in the list of exceptions to catch') def ignore(self, exception: BaseException): """ @@ -154,9 +125,14 @@ def ignore(self, exception: BaseException): :param exception: Exception to ignore. """ + if not isinstance(exception, BaseException): + raise TypeError('`exception` must be an instance of BaseException') if self._exceptions_to_ignore is None: self._exceptions_to_ignore = set() - self._exceptions_to_ignore.add(exception) + if exception not in self._exceptions_to_ignore: + self._exceptions_to_ignore.add(exception) + else: + raise ValueError('exception is already in the list of exceptions to ignore') class ConsoleReporter(Reporter): diff --git a/log21/StreamHandler.py b/log21/StreamHandler.py index 5c1cdb6..641dbc5 100644 --- a/log21/StreamHandler.py +++ b/log21/StreamHandler.py @@ -44,14 +44,13 @@ def check_cr(self, record): record.msg = _gc(*find.split(record.msg[:index])) + record.msg[index + 1:] def check_nl(self, record): - if record.msg: - while record.msg[0] == '\n': - file_descriptor = getattr(self.stream, 'fileno', None) - if file_descriptor: - file_descriptor = file_descriptor() - if file_descriptor in (1, 2): # stdout or stderr - self.stream.write('\n') - record.msg = record.msg[1:] + while record.msg and record.msg[0] == '\n': + file_descriptor = getattr(self.stream, 'fileno', None) + if file_descriptor: + file_descriptor = file_descriptor() + if file_descriptor in (1, 2): # stdout or stderr + self.stream.write('\n') + record.msg = record.msg[1:] def emit(self, record): if self.HandleCR: diff --git a/log21/__init__.py b/log21/__init__.py index 86f2790..eaf175a 100644 --- a/log21/__init__.py +++ b/log21/__init__.py @@ -21,7 +21,7 @@ from log21.Formatters import ColorizingFormatter, DecolorizingFormatter from log21.Colors import Colors, get_color, get_colors, ansi_escape, get_color_name, closest_color -__version__ = "2.3.8" +__version__ = "2.3.9" __author__ = "CodeWriter21 (Mehrad Pooryoussof)" __github__ = "Https://GitHub.com/MPCodeWriter21/log21" __all__ = ['ColorizingStreamHandler', 'DecolorizingFileHandler', 'ColorizingFormatter', 'DecolorizingFormatter', @@ -31,7 +31,7 @@ 'ansi_escape', '__version__', '__author__', '__github__', 'debug', 'info', 'warning', 'warn', 'error', 'critical', 'fatal', 'exception', 'log', 'basic_config', 'basicConfig', 'ProgressBar', 'progress_bar', 'LoggingWindow', 'LoggingWindowHandler', 'get_logging_window', 'CrashReporter', 'console_crash_reporter', - 'file_crash_reporter'] + 'file_crash_reporter', 'console_reporter', 'file_reporter'] _manager = Manager() _logging.setLoggerClass(Logger) @@ -440,8 +440,8 @@ def progress_bar(progress: float, total: float, width: int = None, prefix: str = print(bar.get_bar(progress, total)) -__console_reporter = CrashReporter.ConsoleReporter() -console_crash_reporter = __console_reporter.reporter +console_reporter = CrashReporter.ConsoleReporter() +console_crash_reporter = console_reporter.reporter -__file_reporter = CrashReporter.FileReporter(file='crash_report.log') -file_crash_reporter = __file_reporter.reporter +file_reporter = CrashReporter.FileReporter(file='crash_report.log') +file_crash_reporter = file_reporter.reporter diff --git a/setup.py b/setup.py index ee539eb..b991361 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ long_description = file.read() DESCRIPTION = 'A simple logging package that helps you log colorized messages in Windows console.' -VERSION = '2.3.8' +VERSION = '2.3.9' setup( name='log21',