diff --git a/beetsplug/bareasc.py b/beetsplug/bareasc.py new file mode 100644 index 000000000..9a23ac6d5 --- /dev/null +++ b/beetsplug/bareasc.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +# This file is part of beets. +# Copyright 2016, Philippe Mongeau. +# Copyright 2021, Graham R. Cobb +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and ascociated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# This module is adapted from Fuzzy in accordance to the licence of +# that module + +"""Provides a bare-ASCII matching query. +""" + +from __future__ import division, absolute_import, print_function + +from beets.plugins import BeetsPlugin +from beets.dbcore.query import StringFieldQuery +from unidecode import unidecode + + +class BareascQuery(StringFieldQuery): + @classmethod + def string_match(cls, pattern, val): + print('In BareascQuery' + ' ' + pattern + ' ' + val) + # smartcase + if pattern.islower(): + val = val.lower() + pattern = unidecode(pattern) + val = unidecode(val) + return pattern == val + + +class BareascPlugin(BeetsPlugin): + def __init__(self): + super(BareascPlugin, self).__init__() + self.config.add({ + 'prefix': '#', + }) + + def queries(self): + prefix = self.config['prefix'].as_str() + return {prefix: BareascQuery} diff --git a/test/test_bareasc.py b/test/test_bareasc.py new file mode 100644 index 000000000..20aceac3f --- /dev/null +++ b/test/test_bareasc.py @@ -0,0 +1,88 @@ +# -*- coding: utf-8 -*- + +"""Tests for the 'bareasc' plugin""" + +from __future__ import division, absolute_import, print_function + +import unittest + +from test.helper import TestHelper + +from beets import logging + + +class BareascPluginTest(unittest.TestCase, TestHelper): + + def setUp(self): + self.setup_beets() + self.log = logging.getLogger('beets.web') + self.config['bareasc']['prefix'] = u'#' + self.load_plugins('bareasc') + + # Add library elements. Note that self.lib.add overrides any "id=" + # and assigns the next free id number. + self.add_item(title=u'with accents', + album_id=2, + artist=u'dvořák') + self.add_item(title=u'without accents', + artist=u'dvorak') + self.add_item(title=u'with umlaut', + album_id=2, + artist=u'Brüggen') + self.add_item(title=u'without umlaut', + artist=u'Bruggen') + + def test_search_normal_noaccent(self): + items = self.lib.items('dvorak') + + self.assertEqual(len(items), 1) + self.assertEqual([items[0].title], [u'without accents']) + + def test_search_normal_accent(self): + items = self.lib.items('dvořák') + + self.assertEqual(len(items), 1) + self.assertEqual([items[0].title], [u'with accents']) + + def test_search_bareasc_noaccent(self): + items = self.lib.items('#dvorak') + + self.assertEqual(len(items), 2) + self.assertEqual( + {items[0].title, items[1].title}, + {u'without accents', u'with accents'} + ) + + def test_search_bareasc_accent(self): + items = self.lib.items('#dvořák') + + self.assertEqual(len(items), 2) + self.assertEqual( + {items[0].title, items[1].title}, + {u'without accents', u'with accents'} + ) + + def test_search_bareasc_noumlaut(self): + items = self.lib.items('#Bruggen') + + self.assertEqual(len(items), 2) + self.assertEqual( + {items[0].title, items[1].title}, + {u'without umlaut', u'with umlaut'} + ) + + def test_search_bareasc_umlaut(self): + items = self.lib.items('#Brüggen') + + self.assertEqual(len(items), 2) + self.assertEqual( + {items[0].title, items[1].title}, + {u'without umlaut', u'with umlaut'} + ) + + +def suite(): + return unittest.TestLoader().loadTestsFromName(__name__) + +if __name__ == '__main__': + unittest.main(defaultTest='suite')