Create plugin for "bare-ASCII" matching query

Signed-off-by: Graham R. Cobb <g+beets@cobb.uk.net>
This commit is contained in:
Graham R. Cobb 2021-03-15 00:55:14 +00:00
parent c4347960ea
commit 0c612f408b
2 changed files with 139 additions and 0 deletions

51
beetsplug/bareasc.py Normal file
View file

@ -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}

88
test/test_bareasc.py Normal file
View file

@ -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=<n>"
# 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')