]> kaliko git repositories - mpd-sima.git/blobdiff - sima/plugins/internal/tags.py
Ensure metadata used in filter are available (closes #38)
[mpd-sima.git] / sima / plugins / internal / tags.py
index 005476728735464e9beb3a9dbe145390dadc5c93..5ea4726837d2462917fd0c4c028e418b5d13e775 100644 (file)
@@ -25,11 +25,13 @@ Add titles based on tags
 import random
 
 # third parties components
+from musicpd import CommandError
 
 # local import
 from ...lib.plugin import Plugin
 from ...lib.track import Track
-from ...utils.utils import PluginConfException, PluginException
+from ...utils.utils import PluginException
+
 
 def forge_filter(cfg):
     tags = set(cfg.keys()) & Tags.supported_tags
@@ -38,8 +40,10 @@ def forge_filter(cfg):
     if cfg_filter:
         mpd_filter.append(cfg_filter)
     for tag in tags:
+        if not cfg[tag]:  # avoid empty tags entries in config
+            continue
         if ',' in cfg[tag]:
-            patt = '|'.join(cfg[tag].split(','))
+            patt = '|'.join(map(str.strip, cfg[tag].split(',')))
             mpd_filter.append(f"({tag} =~ '({patt})')")
         else:
             mpd_filter.append(f"({tag} == '{cfg[tag].strip()}')")
@@ -58,23 +62,35 @@ class Tags(Plugin):
         super().__init__(daemon)
         self.daemon = daemon
         self._control_conf()
-        self._setup_tagsneeded()
         self.mpd_filter = forge_filter(self.plugin_conf)
+        self._setup_tagsneeded()
         self.log.debug('mpd filter: %s', self.mpd_filter)
 
     def _control_conf(self):
         sup_tags = Tags.supported_tags
+        config_tags = {k for k, v in self.plugin_conf.items()
+                       if (v and k not in ['filter', 'priority', 'track_to_add'])}
         if not self.plugin_conf.get('filter', None) and \
-                self.plugin_conf.keys().isdisjoint(sup_tags):
-            self.log.error(
-                'Found no config for %s plugin! Need at least "filter" or a supported tag' % self)
+                config_tags.isdisjoint(sup_tags):
+            self.log.error('Found no config for %s plugin! '
+                           'Need at least "filter" or a supported tag', self)
             self.log.info('Supported Tags are : %s', ', '.join(sup_tags))
-            raise PluginConfException('plugin misconfiguration')
+            raise PluginException('plugin misconfiguration')
+        if config_tags.difference(sup_tags):
+            self.log.error('Found unsupported tag in config: %s',
+                           config_tags.difference(sup_tags))
+            raise PluginException('plugin misconfiguration')
 
     def _setup_tagsneeded(self):
-        self.log.debug('%s plugin needs the followinng metadata: %s',
-                self, set(self.plugin_conf.keys()) & Tags.supported_tags)
-        tags = set(self.plugin_conf.keys()) & Tags.supported_tags
+        """Ensure needed tags are exposed by MPD"""
+        # At this point mpd_filter concatenetes {tags}+filter
+        config_tags = set()
+        for mpd_supp_tags in self.player.MPD_supported_tags:
+            if mpd_supp_tags.lower() in self.mpd_filter.lower():
+                config_tags.add(mpd_supp_tags.lower())
+        self.log.debug('%s plugin needs the following metadata: %s',
+                       self, config_tags)
+        tags = config_tags & Tags.supported_tags
         self.player.needed_tags |= tags
 
     def _get_history(self):
@@ -88,10 +104,18 @@ class Tags(Plugin):
     def start(self):
         if (0, 21, 0) > tuple(map(int, self.player.mpd_version.split('.'))):
             self.log.warning('MPD protocol version: %s < 0.21.0',
-                    self.player.mpd_version)
-            self.log.error('Need at least MPD 0.21 to use Tags plugin (filters required)')
+                             self.player.mpd_version)
+            self.log.error(
+                'Need at least MPD 0.21 to use Tags plugin (filters required)')
             self.player.disconnect()
             raise PluginException('MPD >= 0.21 required')
+        # Check filter is valid
+        try:
+            if self.plugin_conf['filter']:
+                self.player.find(self.plugin_conf['filter'])
+        except CommandError:
+            raise PluginException('Badly formated filter in tags plugin configuration: "%s"'
+                                  % self.plugin_conf['filter'])
 
     def callback_need_track(self):
         candidates = []