From b786ac7e26750853485eb84f21020d25e303e54a Mon Sep 17 00:00:00 2001 From: jan iversen Date: Sun, 24 Oct 2021 16:25:35 +0200 Subject: [PATCH] Please pylint for pytradfri/api (#369) * Please pylint for api. * Review comments. --- pytradfri/api/aiocoap_api.py | 60 ++++++++++++++++++------------------ pytradfri/api/libcoap_api.py | 32 +++++++++---------- 2 files changed, 46 insertions(+), 46 deletions(-) diff --git a/pytradfri/api/aiocoap_api.py b/pytradfri/api/aiocoap_api.py index 766f7e7c..15a44f6f 100644 --- a/pytradfri/api/aiocoap_api.py +++ b/pytradfri/api/aiocoap_api.py @@ -102,33 +102,33 @@ async def _get_response(self, msg): """Perform the request, get the response.""" try: protocol = await self._get_protocol() - pr = protocol.request(msg) - r = await asyncio.wait_for(pr.response, timeout=5.0) - return pr, r - except CredentialsMissingError as e: - await self._reset_protocol(e) + pr_req = protocol.request(msg) + pr_resp = await pr_req.response + return pr_req, pr_resp + except CredentialsMissingError as exc: + await self._reset_protocol(exc) await self._update_credentials() - raise ServerError("There was an error with the request.", e) - except ConstructionRenderableError as e: - raise ClientError("There was an error with the request.", e) - except asyncio.TimeoutError as e: - await self._reset_protocol(e) + raise ServerError("There was an error with the request.", exc) from exc + except ConstructionRenderableError as exc: + raise ClientError("There was an error with the request.", exc) from exc + except asyncio.TimeoutError as exc: + await self._reset_protocol(exc) await self._update_credentials() - raise RequestTimeout("Request timed out.", e) - except RequestTimedOut as e: - await self._reset_protocol(e) + raise RequestTimeout("Request timed out.", exc) + except RequestTimedOut as exc: + await self._reset_protocol(exc) await self._update_credentials() - raise RequestTimeout("Request timed out.", e) + raise RequestTimeout("Request timed out.", exc) from exc except LibraryShutdown: raise - except Error as e: - await self._reset_protocol(e) + except Error as exc: + await self._reset_protocol(exc) await self._update_credentials() - raise ServerError("There was an error with the request.", e) - except asyncio.CancelledError as e: - await self._reset_protocol(e) + raise ServerError("There was an error with the request.", exc) from exc + except asyncio.CancelledError as exc: + await self._reset_protocol(exc) await self._update_credentials() - raise e + raise exc async def _execute(self, api_command): """Execute the command.""" @@ -214,23 +214,23 @@ async def _observe(self, api_command): msg = Message(code=Code.GET, uri=url, observe=duration) # Note that this is necessary to start observing - pr, r = await self._get_response(msg) + pr_req, pr_rsp = await self._get_response(msg) - api_command.result = _process_output(r) + api_command.result = _process_output(pr_rsp) def success_callback(res): api_command.result = _process_output(res) - def error_callback(ex): - if isinstance(ex, LibraryShutdown): + def error_callback(exc): + if isinstance(exc, LibraryShutdown): _LOGGER.debug("Protocol is shutdown, stopping observation") return - err_callback(ex) + err_callback(exc) - ob = pr.observation - ob.register_callback(success_callback) - ob.register_errback(error_callback) - self._observations_err_callbacks.append(ob.error) + observation = pr_req.observation + observation.register_callback(success_callback) + observation.register_errback(error_callback) + self._observations_err_callbacks.append(observation.error) async def generate_psk(self, security_key): """Generate and set a psk from the security key.""" @@ -289,7 +289,7 @@ def _process_output(res, parse_json=True): if not res.code.is_successful(): if 128 <= res.code < 160: raise ClientError(output) - elif 160 <= res.code < 192: + if 160 <= res.code < 192: raise ServerError(output) if not parse_json: diff --git a/pytradfri/api/libcoap_api.py b/pytradfri/api/libcoap_api.py index 6686505b..90d028b6 100644 --- a/pytradfri/api/libcoap_api.py +++ b/pytradfri/api/libcoap_api.py @@ -53,7 +53,7 @@ def _execute(self, api_command, *, timeout=None): if api_command.observe: self._observe(api_command) - return + return None method = api_command.method path = api_command.path @@ -87,8 +87,9 @@ def _execute(self, api_command, *, timeout=None): return_value = subprocess.check_output(command, **kwargs) except subprocess.TimeoutExpired: raise RequestTimeout() from None - except subprocess.CalledProcessError as err: - raise RequestError("Error executing request: {}".format(err)) from None + except subprocess.CalledProcessError as exc: + msg = f"Error executing request: {exc}" + raise RequestError(msg) from None api_command.result = _process_output(return_value, parse_json) return api_command.result @@ -110,7 +111,7 @@ def _observe(self, api_command): """Observe an endpoint.""" path = api_command.path duration = api_command.observe_duration - if duration <= 0: + if duration <= 0: # pylint: disable=consider-using-assignment-expr raise ValueError("Observation duration has to be greater than 0.") url = api_command.url(self._host) err_callback = api_command.err_callback @@ -129,9 +130,12 @@ def _observe(self, api_command): "universal_newlines": True, } try: - proc = subprocess.Popen(command, **kwargs) - except subprocess.CalledProcessError as err: - raise RequestError("Error executing request: {}".format(err)) from None + proc = subprocess.Popen( # pylint: disable=consider-using-with + command, **kwargs + ) + except subprocess.CalledProcessError as exc: + msg = f"Error executing request: {exc}" + raise RequestError(msg) from None output = "" open_obj = 0 @@ -182,23 +186,18 @@ def _process_output(output, parse_json=True): if not output: return None - - elif "decrypt_verify" in output: + if "decrypt_verify" in output: raise RequestError( "Please compile coap-client without debug output. See " "instructions at " "https://github.com/ggravlingen/pytradfri#installation" ) - - elif output.startswith(CLIENT_ERROR_PREFIX): + if output.startswith(CLIENT_ERROR_PREFIX): raise ClientError(output) - - elif output.startswith(SERVER_ERROR_PREFIX): + if output.startswith(SERVER_ERROR_PREFIX): raise ServerError(output) - - elif not parse_json: + if not parse_json: return output - return json.loads(output) @@ -214,5 +213,6 @@ def retry_api(*args, **kwargs): except RequestTimeout: if i == retries: raise + return None return retry_api