From 251934a89e7796fb21bb223c4ae04d757082a89b Mon Sep 17 00:00:00 2001 From: kaliko Date: Sat, 14 Nov 2015 19:44:51 +0100 Subject: [PATCH] Fixed simadb_cli auth (Closes #8) --- sima/lib/simaecho.py | 10 ++++-- sima/lib/simafm.py | 8 +++-- simadb_cli | 82 ++++++++------------------------------------ 3 files changed, 28 insertions(+), 72 deletions(-) diff --git a/sima/lib/simaecho.py b/sima/lib/simaecho.py index 7e0bda4..393d723 100644 --- a/sima/lib/simaecho.py +++ b/sima/lib/simaecho.py @@ -50,6 +50,10 @@ class SimaEch: root_url = 'http://{host}/api/{version}'.format(**ECH) name = 'EchoNest' cache = False + """HTTP cache to use, in memory or persitent. + + :param BaseCache cache: Set a cache, defaults to `False`. + """ stats = {'etag':0, 'ccontrol':0, 'minrl':120, @@ -97,7 +101,8 @@ class SimaEch: def get_similar(self, artist): """Fetch similar artists - param: artist Artist: Artist object to get similarities from + :param sima.lib.meta.Artist artist: `Artist` to fetch similar artists from + :returns: generator of :class:`sima.lib.meta.Artist` """ payload = self._forge_payload(artist) # Construct URL @@ -111,7 +116,8 @@ class SimaEch: def get_toptrack(self, artist): """Fetch artist top tracks - param: artist Artist: Artist object to get top tracks from + :param sima.lib.meta.Artist artist: `Artist` to fetch top tracks from + :returns: generator of :class:`sima.lib.track.Track` """ payload = self._forge_payload(artist, top=True) # Construct URL diff --git a/sima/lib/simafm.py b/sima/lib/simafm.py index 0f32f08..a5f19d0 100644 --- a/sima/lib/simafm.py +++ b/sima/lib/simafm.py @@ -43,6 +43,10 @@ class SimaFM: root_url = 'http://{host}/{version}/'.format(**LFM) name = 'Last.fm' cache = False + """HTTP cache to use, in memory or persitent. + + :param BaseCache cache: Set a cache, defaults to `False`. + """ stats = {'etag':0, 'ccontrol':0, 'total':0} @@ -90,7 +94,7 @@ class SimaFM: def get_similar(self, artist): """Fetch similar artists - :param Artist artist: :class:`Artist` to fetch similar artists from + :param sima.lib.meta.Artist artist: `Artist` to fetch similar artists from :returns: generator of :class:`sima.lib.meta.Artist` """ payload = self._forge_payload(artist) @@ -112,7 +116,7 @@ class SimaFM: def get_toptrack(self, artist): """Fetch artist top tracks - :param Artist artist: :class:`Artist` to fetch top tracks from + :param sima.lib.meta.Artist artist: `Artist` to fetch top tracks from :returns: generator of :class:`sima.lib.track.Track` """ payload = self._forge_payload(artist, method='top') diff --git a/simadb_cli b/simadb_cli index 65ae284..b720a0d 100755 --- a/simadb_cli +++ b/simadb_cli @@ -20,12 +20,10 @@ # # -__version__ = '0.4.0' +__version__ = '0.4.1' # IMPORT# -import re - -from argparse import (ArgumentParser, SUPPRESS, Action) +from argparse import (ArgumentParser, SUPPRESS) from difflib import get_close_matches from locale import getpreferredencoding from os import (environ, chmod, makedirs) @@ -35,7 +33,7 @@ from sys import (exit, stdout, stderr) from sima.lib.track import Track from sima.utils import utils from sima.lib import simadb -from musicpd import MPDClient, ConnectionError +from musicpd import MPDClient, MPDError DESCRIPTION = """ @@ -140,13 +138,6 @@ class SimaDB_CLI(object): else: self.options.mpdport = 6600 - def _upgrade(self): - """Upgrades DB if necessary, create one if not existing.""" - if not isfile(self.dbfile): # No db file - return - db = simadb.SimaDB(db_path=self.dbfile) - db.upgrade() - def _declare_opts(self): """ Declare options in ArgumentParser object. @@ -183,14 +174,16 @@ class SimaDB_CLI(object): def _get_mpd_client(self): """""" - # TODO: encode properly host name host = self.options.mpdhost port = self.options.mpdport + passwd = self.options.passwd cli = MPDClient() try: - cli.connect(host=host, port=port) - except ConnectionError as err: - mess = 'ERROR: fail to connect MPD (host: %s:%s): %s' % ( + cli.connect(host, port) + if passwd: + cli.password(passwd) + except MPDError as err: + mess = 'ERROR: fail to connect MPD on %s:%s %s' % ( host, port, err) print(mess, file=stderr) exit(1) @@ -213,51 +206,9 @@ class SimaDB_CLI(object): return None return art_db - def _control_similarity(self): - """ - * Regex check of command line similarity - * Controls artist presence in MPD library - """ - usage = ('USAGE: "main artist,similar artist:,other' + - 'similar artist:,..."') - cli_sim = self.options.similarity - pattern = '^([^,]+?),([^:,]+?:\d{1,2},?)+$' - regexp = re.compile(pattern, re.U).match(cli_sim) - if not regexp: - mess = 'ERROR: similarity badly formated: "%s"' % cli_sim - print(mess, file=stderr) - print(usage, file=stderr) - exit(1) - if self.options.check_names: - if not self._control_artist_names(): - mess = 'ERROR: some artist names not found in MPD library!' - print(mess, file=stderr) - exit(1) - - def _control_artist_names(self): - """Controls artist names exist in MPD library""" - mpd_cli = self._get_mpd_client() - artists_list = mpd_cli.list('artist') - sim_formated = self._parse_similarity() - control = True - if sim_formated[0] not in artists_list: - mess = 'WARNING: Main artist not found in MPD: %s' % sim_formated[0] - print(mess) - control = False - for sart in sim_formated[1]: - art = sart.get('artist') - if art not in artists_list: - mess = str('WARNING: Similar artist not found in MPD: %s' % art) - print(mess) - control = False - mpd_cli.disconnect() - return control - def bl_artist(self): """Black list artist""" mpd_cli = self._get_mpd_client() - if not mpd_cli: - return False artists_list = mpd_cli.list('artist') # Unicode cli given artist name cli_artist_to_bl = self.options.bl_art @@ -275,8 +226,6 @@ class SimaDB_CLI(object): def bl_current_artist(self): """Black list current artist""" mpd_cli = self._get_mpd_client() - if not mpd_cli: - return False artist = mpd_cli.currentsong().get('artist', '') if not artist: print('No artist found.') @@ -288,8 +237,6 @@ class SimaDB_CLI(object): def bl_current_album(self): """Black list current artist""" mpd_cli = self._get_mpd_client() - if not mpd_cli: - return False track = Track(**mpd_cli.currentsong()) if not track.album: print('No album set for this track: %s' % track) @@ -301,8 +248,6 @@ class SimaDB_CLI(object): def bl_current_track(self): """Black list current artist""" mpd_cli = self._get_mpd_client() - if not mpd_cli: - return False track = Track(**mpd_cli.currentsong()) print('Black listing track: %s' % track) db = simadb.SimaDB(db_path=self.dbfile) @@ -363,12 +308,13 @@ class SimaDB_CLI(object): exit(0) -def main(): - SimaDB_CLI() - # Script starts here if __name__ == '__main__': - main() + try: + SimaDB_CLI() + except Exception as err: + print(err) + exit(1) # VIM MODLINE # vim: ai ts=4 sw=4 sts=4 expandtab -- 2.39.5