From: kaliko jack <kaliko@azylum.org>
Date: Sun, 2 Mar 2014 11:49:43 +0000 (+0100)
Subject: Optional persistent cache for last.fm
X-Git-Tag: mpd-sima/0.12.0pr4~6
X-Git-Url: https://git.kaliko.me/?a=commitdiff_plain;h=8bf0d37a99b80af5674a639bfbc155db18ac557d;p=mpd-sima.git

Optional persistent cache for last.fm

Fixed missing cache directory creation bug
Update documentation
---

diff --git a/doc/examples/all_settings.cfg b/doc/examples/all_settings.cfg
index 047ee3c..119d7f4 100644
--- a/doc/examples/all_settings.cfg
+++ b/doc/examples/all_settings.cfg
@@ -48,7 +48,6 @@ port = 6600
 # description: file to log to. Usually used in daemon mode.
 # default: unset, logging to stdin/stdout
 #logfile =
-##
 
 ## VERBOSITY
 # type: string
@@ -60,7 +59,6 @@ port = 6600
 #    * warning
 #    * error
 verbosity = info
-##
 
 #
 #######################################################################
@@ -76,6 +74,8 @@ verbosity = info
 #                contrib = Scrobble, AwesomePlugin,
 #                          ExperimentalTest, AnotherTest
 # default:
+#          internal = "Crop, Lastfm, RandomFallBack"
+#          contrib =
 # description: Plugins list declaration.
 #     Optional plugin's configuration must be in its own section.
 #     For instance a "AwesomePlugin" declared here
@@ -85,7 +85,7 @@ verbosity = info
 #     Two plugins sources are available, internal and contrib
 #
 internal = Crop, Lastfm, RandomFallBack
-#contrib = PlaceHolder
+#contrib =
 
 ## HISTORY_DURATION
 # type: integer (in hours)
@@ -94,32 +94,21 @@ internal = Crop, Lastfm, RandomFallBack
 #     track/title
 #
 history_duration = 8
-##
 
 ## USER_DB # NOT IMPLEMENTED #
 # type: boolean
 # description: Load user database to find similar artists
-#     User DB is loaded from $XDG_CONFIG_HOME/sima/sima.db
+#     User DB is loaded from $XDG_CONFIG_HOME/mpd_sima/sima.db
 #     Use simadb_cli to edit/add entries.
 user_db = false
-##
-
-#####################################################################
-# You do not need to set up options below.
-# But well, you got bored of the way sima is behaving, then go ahead
-# play with it :)
 
 ## QUEUE_LENGTH
 # type: integer
 # default: 1
 # description: Queue length triggering tracks addition
 queue_length = 1
-##
 
 ######################### PLUGINS #####################################
-#
-[placeholder]
-key = Value
 
 [crop]
 ## CONSUME
@@ -156,12 +145,12 @@ key = Value
 # EchoNest or LastFM
 #[echonest]
 [lastfm]
-## QUEUE_MODE # NOT COMPLETED #
+## QUEUE_MODE
 # type: string
 # description: The default is to queue random tracks from similar artists.
 # Possible values:
 #	track : Will queue tracks from similar artists (default).
-#	top   : Will queue top tracks from similar artists. # NOT IMPLEMENTED #
+#	top   : Will queue top tracks from similar artists.
 #	album : Will queue whole album from similar artists.
 queue_mode = track
 
@@ -197,8 +186,17 @@ track_to_add = 1
 # description: how many albums the plugin will try to get
 album_to_add = 1
 
-#
-#######################################################################
+## CACHE
+# type: boolean
+# description: whether or not to use on-disk persistent http cache
+#  * When set to "true", sima will use a persistent cache for its http client.
+#    The cache is written along with the dbfile in:
+#                $XDG_CONFIG_HOME/mpd_sima/http/<web_service>
+#    Toggling http cache is only available for last.fm. EchoNest have rate limits,
+#    we must then pay attention to bandwidth and use of caching is required.
+#  * If set to "false", caching is still done but in memory.
+# default: True
+cache = True
 
 #
 ####################### END OF CONFIGURATION ##########################
diff --git a/sima/lib/cache.py b/sima/lib/cache.py
index c4170de..e16ca0e 100644
--- a/sima/lib/cache.py
+++ b/sima/lib/cache.py
@@ -73,7 +73,7 @@ class FileCache:
         self.forever = forever
 
         if not os.path.isdir(self.directory):
-            os.mkdir(self.directory)
+            os.makedirs(self.directory)
 
     def encode(self, val):
         return md5(val.encode('utf-8')).hexdigest()
diff --git a/sima/lib/webserv.py b/sima/lib/webserv.py
index c4118c0..75b0a38 100644
--- a/sima/lib/webserv.py
+++ b/sima/lib/webserv.py
@@ -381,7 +381,9 @@ class WebService(Plugin):
             self.log.debug(repr(self.player.current))
             return None
         self.queue_mode()
-        self.log.debug(self.ws.stats)
+        msg = ' '.join(['{0}: {1:>3d}'.format(k, v) for
+                        k, v in sorted(self.ws.stats.items())])
+        self.log.debug(msg)
         candidates = self.to_add
         self.to_add = list()
         if self.plugin_conf.get('queue_mode') != 'album':
diff --git a/sima/plugins/internal/lastfm.py b/sima/plugins/internal/lastfm.py
index ba10a48..be87094 100644
--- a/sima/plugins/internal/lastfm.py
+++ b/sima/plugins/internal/lastfm.py
@@ -29,7 +29,7 @@ from os.path import join
 # local import
 from ...lib.simafm import SimaFM
 from ...lib.webserv import WebService
-from ...lib.cache import FileCache
+from ...lib.cache import FileCache, DictCache
 
 
 class Lastfm(WebService):
@@ -41,7 +41,11 @@ class Lastfm(WebService):
         self.ws = SimaFM
         # Set persitent cache
         vardir = daemon.config['sima']['var_dir']
-        SimaFM.cache = FileCache(join(vardir, 'http', 'LastFM'))
+        persitent_cache = daemon.config.getboolean('lastfm', 'cache')
+        if persitent_cache:
+            SimaFM.cache = FileCache(join(vardir, 'http', 'LastFM'))
+        else:
+            SimaFM.cache = DictCache()
 
 
 # VIM MODLINE
diff --git a/sima/utils/config.py b/sima/utils/config.py
index 157d7c3..cdaefd1 100644
--- a/sima/utils/config.py
+++ b/sima/utils/config.py
@@ -60,6 +60,9 @@ DEFAULT_CONF = {
             'verbosity': "info",
             'logfile': "",
             },
+        'crop': {
+            'consume': 10,
+            },
         'echonest': {
             'queue_mode': "track", #TODO control values
             'max_art': 15,
@@ -75,6 +78,7 @@ DEFAULT_CONF = {
             'track_to_add': 1,
             'album_to_add': 1,
             'depth': 1,
+            'cache': True,
             },
         'randomfallback': {
             'flavour': "sensible", # in pure, sensible