From dd548acdfbd07913805caafa7a9522351d9daace Mon Sep 17 00:00:00 2001 From: skelly37 Date: Wed, 8 Jun 2022 17:40:18 +0200 Subject: [PATCH] errors refactored --- picard/tagger.py | 2 +- picard/util/pipe.py | 40 +++++++++++++++++++--------------------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/picard/tagger.py b/picard/tagger.py index 240c1acb6..8312a1afd 100644 --- a/picard/tagger.py +++ b/picard/tagger.py @@ -1107,7 +1107,7 @@ def main(localedir=None, autoupdate=True): if not should_start: pipe_handler = pipe.Pipe(app_name=PICARD_APP_NAME, app_version=PICARD_FANCY_VERSION_STR, args=picard_args.FILE) - should_start = True in [pipe_handler.permission_error_happened, pipe_handler.is_pipe_owner] + should_start = True in {pipe_handler.permission_error_happened, pipe_handler.is_pipe_owner} # pipe has sent its args to existing one, doesn't need to start if not should_start: diff --git a/picard/util/pipe.py b/picard/util/pipe.py index 15e6e9389..16f74b65c 100644 --- a/picard/util/pipe.py +++ b/picard/util/pipe.py @@ -23,11 +23,12 @@ import concurrent.futures import os from typing import Optional -from picard.const.sys import ( - IS_MACOS, - IS_WIN, -) +#from picard.const.sys import ( +# IS_MACOS, +# IS_WIN, +#) +IS_WIN = False if IS_WIN: import win32pipe # type: ignore @@ -42,36 +43,36 @@ class PipeError(Exception): class PipeErrorInvalidArgs(PipeError): MESSAGE = "ERROR: Pipe() args argument has to be iterable" - def __init__(self, t): - raise PipeError(f"{self.MESSAGE}, {type(t)} provied.") + def __init__(self, msg): + super().__init__(f"{self.MESSAGE}: {msg}.") class PipeErrorNotFound(PipeError): MESSAGE = "ERROR: Pipe doesn't exist." def __init__(self): - raise PipeError(self.MESSAGE) + super().__init__(self.MESSAGE) class PipeErrorBroken(PipeError): MESSAGE = "ERROR: Pipe is broken." def __init__(self): - raise PipeError(self.MESSAGE) + super().__init__(self.MESSAGE) class PipeErrorInvalidResponse(PipeError): MESSAGE = "ERROR: Invalid response from pipe:" def __init__(self, response): - raise PipeError(f"{self.MESSAGE} {response}") + super().__init__(f"{self.MESSAGE} {response}") class PipeErrorWin(PipeError): MESSAGE = "ERROR: Windows API error\n" def __init__(self, winerror): - raise PipeError(f"{self.MESSAGE}{winerror}") + super().__init__(f"{self.MESSAGE}{winerror}") class Pipe: @@ -82,13 +83,13 @@ class Pipe: def __init__(self, app_name: str, app_version: str, args=None): if args is None: args = tuple() - elif args is str: + elif isinstance(args, str): args = (args,) else: try: args = tuple(args) - except TypeError: - raise PipeErrorInvalidArgs(args) + except TypeError as exc: + raise PipeErrorInvalidArgs(exc) if not args: args = (self.MESSAGE_TO_IGNORE,) @@ -252,11 +253,11 @@ class Pipe: except WinApiError as err: if err.winerror == self.__FILE_NOT_FOUND_ERROR_CODE: - raise PipeErrorNotFound + raise PipeErrorNotFound from None elif err.winerror == self.__BROKEN_PIPE_ERROR_CODE: - raise PipeErrorBroken + raise PipeErrorBroken from None else: - raise PipeErrorWin(f"{err.winerror}; {err.funcname}; {err.strerror}") + raise PipeErrorWin(f"{err.winerror}; {err.funcname}; {err.strerror}") from None # response[0] stores an exit code while response[1] an actual response if response: @@ -274,9 +275,6 @@ class Pipe: with open(self.path, 'r') as fifo: response = fifo.read().strip() except FileNotFoundError: - raise PipeErrorNotFound + raise PipeErrorNotFound from None - if response: - return response - else: - return Pipe.NO_RESPONSE_MESSAGE + return response or Pipe.NO_RESPONSE_MESSAGE