From 5aab757fd99c61077200e5c175fcafb7228b166f Mon Sep 17 00:00:00 2001 From: hhm Date: Wed, 22 Mar 2017 02:22:36 -0400 Subject: [PATCH] B"H rearrange vdir code in a clearer fashion --- mopidy_subidy/library.py | 48 ++++++++++++++++++++++++++++------------ mopidy_subidy/uri.py | 3 --- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/mopidy_subidy/library.py b/mopidy_subidy/library.py index a319a49..cd29d53 100644 --- a/mopidy_subidy/library.py +++ b/mopidy_subidy/library.py @@ -6,7 +6,35 @@ import logging logger = logging.getLogger(__name__) class SubidyLibraryProvider(backend.LibraryProvider): - root_directory = Ref.directory(uri=uri.ROOT_URI, name='Subsonic') + def __create_vdirs(): + vdir_templates = [ + dict(id="root", name="Subsonic"), + dict(id="artists", name="Artists"), + dict(id="albums", name="Albums"), + dict(id="rootdirs", name="Directories"), + ] + # Create a dict with the keys being the `id`s in `vdir_templates` + # and the values being objects containing the vdir `id`, + # the human readable name as `name`, and the URI as `uri`. + vdirs = {} + for template in vdir_templates: + vdir = template.copy() + vdir.update(uri=uri.get_vdir_uri(vdir["id"])) + vdirs[template['id']] = vdir + return vdirs + + _vdirs = __create_vdirs() + + def __raw_vdir_to_ref(vdir): + if vdir is None: + return None + return Ref.directory( + name=vdir['name'], + uri=vdir['uri']) + + root_directory = __raw_vdir_to_ref(_vdirs['root']) + + _raw_vdir_to_ref = staticmethod(__raw_vdir_to_ref) def __init__(self, *args, **kwargs): super(SubidyLibraryProvider, self).__init__(*args, **kwargs) @@ -37,19 +65,11 @@ class SubidyLibraryProvider(backend.LibraryProvider): return self.subsonic_api.get_artist_by_id(artist_id) def browse(self, browse_uri): - vdir_templates = [ - # ("root", ) - ("rootdirs", "Directories"), - ("artists", "Artists"), - ("albums", "Albums"), - ] - # Create a dict with the keys being the left sides in `vdir_templates` ("rootdirs", "artists", ...) - # and the values being objects containing the key as `id` as well as the human readable name as `name`. - vdirs = dict((template[0], dict(id=template[0], name=template[1])) for template in vdir_templates) - if browse_uri == uri.ROOT_URI: - # Sort directories by name. - root_vdirs = sorted((vdirs[vdir_name] for vdir_name in ["rootdirs", "artists", "albums"]), key=lambda a: a["name"]) - return [Ref.directory(name=vdir["name"], uri=uri.get_vdir_uri(vdir["id"])) for vdir in root_vdirs] + if browse_uri == uri.get_vdir_uri('root'): + root_vdir_names = ["rootdirs", "artists", "albums"] + root_vdirs = [self._vdirs[vdir_name] for vdir_name in root_vdir_names] + sorted_root_vdirs = sorted(root_vdirs, key=lambda vdir: vdir["name"]) + return [self._raw_vdir_to_ref(vdir) for vdir in sorted_root_vdirs] elif browse_uri == uri.get_vdir_uri("rootdirs"): return self.browse_rootdirs() elif browse_uri == uri.get_vdir_uri("artists"): diff --git a/mopidy_subidy/uri.py b/mopidy_subidy/uri.py index d9216ce..613f958 100644 --- a/mopidy_subidy/uri.py +++ b/mopidy_subidy/uri.py @@ -7,11 +7,8 @@ ALBUM = 'album' DIRECTORY = 'directory' VDIR = 'vdir' PREFIX = 'subidy' -ROOT = 'root' SEARCH = 'search' -ROOT_URI = '%s:%s:%s' % (PREFIX, VDIR, ROOT) - regex = re.compile(r'(\w+?):(\w+?)(?::|$)(.+?)?$') def is_type_result_valid(result):