From: Marcel van der Veldt Date: Sat, 19 Dec 2020 10:09:18 +0000 (+0100) Subject: linting X-Git-Url: https://git.kitaultman.com/?a=commitdiff_plain;h=d31bb658751091ad04127f12fe5b57bbfcf017c5;p=music-assistant-server.git linting --- diff --git a/music_assistant/helpers/images.py b/music_assistant/helpers/images.py index fda62d86..424e0aeb 100644 --- a/music_assistant/helpers/images.py +++ b/music_assistant/helpers/images.py @@ -69,7 +69,7 @@ async def async_get_image_url( return await async_get_image_url( mass, item.album.item_id, item.album.provider, MediaType.Album ) - elif media_type == MediaType.Album and item.artist: + if media_type == MediaType.Album and item.artist: # try artist instead for albums return await async_get_image_url( mass, item.artist.item_id, item.artist.provider, MediaType.Artist diff --git a/music_assistant/helpers/repath.py b/music_assistant/helpers/repath.py index 12125a9f..f5857e6d 100644 --- a/music_assistant/helpers/repath.py +++ b/music_assistant/helpers/repath.py @@ -1,4 +1,9 @@ -"""Helper functionalities for path detection in api routes.""" +""" +Helper functionalities for path detection in api routes. + +Based on the original work by nickcoutsos and synacor +https://github.com/nickcoutsos/python-repath +""" import re import urllib import urllib.parse @@ -102,8 +107,7 @@ def tokens_to_function(tokens): if value is None: if key["optional"]: continue - else: - raise KeyError('Expected "{name}" to be defined'.format(**key)) + raise KeyError('Expected "{name}" to be defined'.format(**key)) if isinstance(value, list): if not key["repeat"]: @@ -112,10 +116,7 @@ def tokens_to_function(tokens): if not value: if key["optional"]: continue - else: - raise ValueError( - 'Expected "{name}" to not be empty'.format(**key) - ) + raise ValueError('Expected "{name}" to not be empty'.format(**key)) for i, val in enumerate(value): val = str(val) @@ -178,10 +179,10 @@ def tokens_to_pattern(tokens, options=None): strict = options.get("strict") end = options.get("end") is not False route = "" - lastToken = tokens[-1] - endsWithSlash = isinstance(lastToken, str) and lastToken.endswith("/") + last_token = tokens[-1] + ends_with_slash = isinstance(last_token, str) and last_token.endswith("/") - PATTERNS = dict( + patterns = dict( REPEAT="(?:{prefix}{capture})*", OPTIONAL="(?:{prefix}({name}{capture}))?", REQUIRED="{prefix}({name}{capture})", @@ -202,19 +203,19 @@ def tokens_to_pattern(tokens, options=None): parts["name"] = "?P<%s>" % re.escape(token["name"]) if token["repeat"]: - parts["capture"] += PATTERNS["REPEAT"].format(**parts) + parts["capture"] += patterns["REPEAT"].format(**parts) - template = PATTERNS["OPTIONAL" if token["optional"] else "REQUIRED"] + template = patterns["OPTIONAL" if token["optional"] else "REQUIRED"] route += template.format(**parts) if not strict: - route = route[:-1] if endsWithSlash else route + route = route[:-1] if ends_with_slash else route route += "(?:/(?=$))?" if end: route += "$" else: - route += "" if strict and endsWithSlash else "(?=/|$)" + route += "" if strict and ends_with_slash else "(?=/|$)" return "^%s" % route @@ -258,16 +259,6 @@ def path_to_pattern(path, keys=None, options=None): return string_to_pattern(path, keys, options) -def compile(string): - """Compile a string to a template function for the path.""" - return tokens_to_function(parse(string)) - - -def match(pattrn, requested_url_path): +def match_pattern(pattrn, requested_url_path): """Return shorthand to match function.""" return re.match(pattrn, requested_url_path) - - -def pattern(pathstr): - """Return shorthand to pattern function.""" - return path_to_pattern(pathstr) diff --git a/music_assistant/web/server.py b/music_assistant/web/server.py index 02c378a9..3c335217 100755 --- a/music_assistant/web/server.py +++ b/music_assistant/web/server.py @@ -56,7 +56,6 @@ class WebServer: self.config = mass.config.base["web"] self._runner = None self.api_routes = {} - self._discovered_servers = [] async def async_setup(self): """Perform async setup.""" @@ -106,7 +105,7 @@ class WebServer: def register_api_route(self, cmd: str, func: Awaitable): """Register a command(handler) to the websocket api.""" - pattern = repath.pattern(cmd) + pattern = repath.path_to_pattern(cmd) self.api_routes[pattern] = func def register_api_routes(self, cls: Any): @@ -218,9 +217,6 @@ class WebServer: self.mass.config.save() # fix discovery info await self.mass.async_setup_discovery() - for item in self._discovered_servers: - if item["id"] == self.server_id: - item["initialized"] = True return True @api_route("images/thumb") @@ -321,7 +317,7 @@ class WebServer: return await self.__async_handle_auth(ws_client, data) # work out handler for the given path/command for key in self.api_routes: - match = repath.match(key, command) + match = repath.match_pattern(key, command) if match: params = match.groupdict() handler = self.api_routes[key]