style fixes for ihate (#486)

This commit is contained in:
Adrian Sampson 2014-02-08 12:00:23 -08:00
parent 5f2cb6b5cd
commit fe5c175cc3
3 changed files with 44 additions and 42 deletions

View file

@ -1,5 +1,5 @@
# This file is part of beets.
# Copyright 2013, Blemjhoo Tezoulbr <baobab@heresiarch.info>.
# Copyright 2014, Blemjhoo Tezoulbr <baobab@heresiarch.info>.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
@ -14,12 +14,10 @@
"""Warns you about things you hate (or even blocks import)."""
import re
import logging
from beets.plugins import BeetsPlugin
from beets.importer import action
from beets.dbcore.query import AndQuery
from beets.library import query_from_strings
from beets.library import get_query
from beets.library import Item
from beets.library import Album
@ -29,8 +27,6 @@ __version__ = '2.0'
class IHatePlugin(BeetsPlugin):
_instance = None
_log = logging.getLogger('beets')
def __init__(self):
@ -38,8 +34,8 @@ class IHatePlugin(BeetsPlugin):
self.register_listener('import_task_choice',
self.import_task_choice_event)
self.config.add({
'warn': {},
'skip': {},
'warn': [],
'skip': [],
})
@classmethod
@ -48,31 +44,29 @@ class IHatePlugin(BeetsPlugin):
task is hated and not whitelisted.
"""
if action_patterns:
for queryString in action_patterns:
blockQuery = None
for query_string in action_patterns:
query = None
if task.is_album:
blockQuery = query_from_strings(AndQuery,Album,queryString)
query = get_query(query_string, Album)
else:
blockQuery = query_from_strings(AndQuery,Item,queryString)
if any(blockQuery.match(item) for item in task.items):
query = get_query(query_string, Item)
if any(query.match(item) for item in task.items):
return True
return False
def job_to_do(self):
"""Return True if at least one pattern is defined."""
return any(self.config[l].as_str_seq() for l in ('warn', 'skip'))
def import_task_choice_event(self, session, task):
skip_queries = self.config['skip'].as_str_seq()
warn_queries = self.config['warn'].as_str_seq()
if task.choice_flag == action.APPLY:
if self.job_to_do():
if skip_queries or warn_queries:
self._log.debug('[ihate] processing your hate')
if self.do_i_hate_this(task, self.config['skip']):
if self.do_i_hate_this(task, skip_queries):
task.choice_flag = action.SKIP
self._log.info(u'[ihate] skipped: {0} - {1}'
.format(task.cur_artist, task.cur_album))
return
if self.do_i_hate_this(task, self.config['warn']):
if self.do_i_hate_this(task, warn_queries):
self._log.info(u'[ihate] you maybe hate this: {0} - {1}'
.format(task.cur_artist, task.cur_album))
else:

View file

@ -2,26 +2,26 @@ IHate Plugin
============
The ``ihate`` plugin allows you to automatically skip things you hate during
import or warn you about them. It supports any query, params will be checked
with OR chained logic, so as long as long as one element is satisfied the album
will be skipped.
There are two groups: warn and skip. The skip group is checked first.
import or warn you about them. You specify queries (see
:doc:`/reference/query`) and the plugin skips (or warns about) albums or items
that match any query.
To use the plugin, enable it by including ``ihate`` in the ``plugins`` line of
your beets config. Then, add an ``ihate:`` section to your configuration file::
To use the plugin, first enable it in your configuration (see
:ref:`using-plugins`). Then, add an ``ihate:`` section to your configuration
file::
ihate:
# you will be warned about these suspicious genres/artists:
# Print a warning message for these.
warn:
- artist:rnb
- genre: soul
#only warn about tribute albums in rock genre
# Only warn about tribute albums in rock genre.
- genre:rock album:tribute
# never import any of this:
# Never import any of this.
skip:
- genre:russian\srock
- genre::russian\srock
- genre:polka
- artist:manowar
- album:christmas
Note: The plugin will trust your decision in 'as-is' mode.
The plugin trusts your decision in "as-is" imports.

View file

@ -13,27 +13,35 @@ class IHatePluginTest(unittest.TestCase):
match_pattern = {}
testItem = Item(
genre='TestGenre',
album = u'TestAlbum',
artist = u'TestArtist')
album=u'TestAlbum',
artist=u'TestArtist')
task = ImportTask()
task.items = [testItem]
task.is_album = False
#empty query should let it pass
# Empty query should let it pass.
self.assertFalse(IHatePlugin.do_i_hate_this(task, match_pattern))
#1 query match
# 1 query match.
match_pattern = ["artist:bad_artist","artist:TestArtist"]
self.assertTrue(IHatePlugin.do_i_hate_this(task, match_pattern))
#2 query matches, either should trigger
# 2 query matches, either should trigger.
match_pattern = ["album:test","artist:testartist"]
self.assertTrue(IHatePlugin.do_i_hate_this(task, match_pattern))
#query is blocked by AND clause
# Query is blocked by AND clause.
match_pattern = ["album:notthis genre:testgenre"]
self.assertFalse(IHatePlugin.do_i_hate_this(task, match_pattern))
#both queries are blocked by AND clause with unmatched condition
match_pattern = ["album:notthis genre:testgenre","artist:testartist album:notthis"]
# Both queries are blocked by AND clause with unmatched condition.
match_pattern = ["album:notthis genre:testgenre",
"artist:testartist album:notthis"]
self.assertFalse(IHatePlugin.do_i_hate_this(task, match_pattern))
#only one query should fire
match_pattern = ["album:testalbum genre:testgenre","artist:testartist album:notthis"]
# Only one query should fire.
match_pattern = ["album:testalbum genre:testgenre",
"artist:testartist album:notthis"]
self.assertTrue(IHatePlugin.do_i_hate_this(task, match_pattern))