B"H rearrange vdir code in a clearer fashion
This commit is contained in:
parent
55e0dd0609
commit
5aab757fd9
2 changed files with 34 additions and 17 deletions
|
@ -6,7 +6,35 @@ import logging
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class SubidyLibraryProvider(backend.LibraryProvider):
|
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):
|
def __init__(self, *args, **kwargs):
|
||||||
super(SubidyLibraryProvider, self).__init__(*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)
|
return self.subsonic_api.get_artist_by_id(artist_id)
|
||||||
|
|
||||||
def browse(self, browse_uri):
|
def browse(self, browse_uri):
|
||||||
vdir_templates = [
|
if browse_uri == uri.get_vdir_uri('root'):
|
||||||
# ("root", <no name>)
|
root_vdir_names = ["rootdirs", "artists", "albums"]
|
||||||
("rootdirs", "Directories"),
|
root_vdirs = [self._vdirs[vdir_name] for vdir_name in root_vdir_names]
|
||||||
("artists", "Artists"),
|
sorted_root_vdirs = sorted(root_vdirs, key=lambda vdir: vdir["name"])
|
||||||
("albums", "Albums"),
|
return [self._raw_vdir_to_ref(vdir) for vdir in sorted_root_vdirs]
|
||||||
]
|
|
||||||
# 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]
|
|
||||||
elif browse_uri == uri.get_vdir_uri("rootdirs"):
|
elif browse_uri == uri.get_vdir_uri("rootdirs"):
|
||||||
return self.browse_rootdirs()
|
return self.browse_rootdirs()
|
||||||
elif browse_uri == uri.get_vdir_uri("artists"):
|
elif browse_uri == uri.get_vdir_uri("artists"):
|
||||||
|
|
|
@ -7,11 +7,8 @@ ALBUM = 'album'
|
||||||
DIRECTORY = 'directory'
|
DIRECTORY = 'directory'
|
||||||
VDIR = 'vdir'
|
VDIR = 'vdir'
|
||||||
PREFIX = 'subidy'
|
PREFIX = 'subidy'
|
||||||
ROOT = 'root'
|
|
||||||
SEARCH = 'search'
|
SEARCH = 'search'
|
||||||
|
|
||||||
ROOT_URI = '%s:%s:%s' % (PREFIX, VDIR, ROOT)
|
|
||||||
|
|
||||||
regex = re.compile(r'(\w+?):(\w+?)(?::|$)(.+?)?$')
|
regex = re.compile(r'(\w+?):(\w+?)(?::|$)(.+?)?$')
|
||||||
|
|
||||||
def is_type_result_valid(result):
|
def is_type_result_valid(result):
|
||||||
|
|
Loading…
Add table
Reference in a new issue