linting
authorMarcel van der Veldt <m.vanderveldt@outlook.com>
Sat, 19 Dec 2020 10:09:18 +0000 (11:09 +0100)
committerMarcel van der Veldt <m.vanderveldt@outlook.com>
Sat, 19 Dec 2020 10:09:18 +0000 (11:09 +0100)
music_assistant/helpers/images.py
music_assistant/helpers/repath.py
music_assistant/web/server.py

index fda62d86677110a2769af93814383d81cccafc5e..424e0aebcc2422a70de22967f47771b5337993fe 100644 (file)
@@ -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
index 12125a9ff488ce6acad89991a8ec5b3697463192..f5857e6dc08ad7b5626a4d197daea704b6a9357c 100644 (file)
@@ -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)
index 02c378a94c5a6e98842556062ad5343cd03c61e5..3c335217482be74ddd6c9b790170653fdbebbb69 100755 (executable)
@@ -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]