From 073ee14e56d600bfa22ccc74bf7bab1a94a7c3aa Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sat, 23 Jun 2012 17:18:28 -0700 Subject: [PATCH] fix broken matching of path format queries (#405) A bug in the shlex module with Unicode strings made these queries unparseable and thus match every track. --- beets/library.py | 9 ++++++++- docs/changelog.rst | 4 +++- test/test_query.py | 9 +++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/beets/library.py b/beets/library.py index 47456fe9c..57e4ef0c1 100644 --- a/beets/library.py +++ b/beets/library.py @@ -595,7 +595,14 @@ class CollectionQuery(Query): """Creates a query based on a single string. The string is split into query parts using shell-style syntax. """ - return cls.from_strings(shlex.split(query)) + # A bug in Python < 2.7.3 prevents correct shlex splitting of + # Unicode strings. + # http://bugs.python.org/issue6988 + if isinstance(query, unicode): + pass + query = query.encode('utf8') + parts = [s.decode('utf8') for s in shlex.split(query)] + return cls.from_strings(parts) class AnySubstringQuery(CollectionQuery): """A query that matches a substring in any of a list of metadata diff --git a/docs/changelog.rst b/docs/changelog.rst index b1f26840f..42f0052bb 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -46,7 +46,9 @@ Changelog * When previewing changes during import, differences in track duration are now shown as "2:50 vs. 3:10" rather than separated with ``->`` like track numbers. This should clarify that beets isn't doing anything to modify lengths. -* :doc:`/plugins/chroma`: Fix tracking with ambiguous Acoustids. Some Acoustids +* Fix a problem with query-based path format matching where a field-qualified + pattern, like ``albumtype_soundtrack``, would match everything. +* :doc:`/plugins/chroma`: Fix matching with ambiguous Acoustids. Some Acoustids are identified with multiple recordings; beets now considers any associated recording a valid match. This should reduce some cases of errant track reordering when using chroma. diff --git a/test/test_query.py b/test/test_query.py index 3c61f9987..5a4debefc 100644 --- a/test/test_query.py +++ b/test/test_query.py @@ -421,6 +421,15 @@ class CountTest(unittest.TestCase): self.assertEqual(songs, 0) self.assertEqual(totaltime, 0.0) +class StringParseTest(unittest.TestCase): + def test_single_field_query(self): + q = beets.library.AndQuery.from_string(u'albumtype:soundtrack') + self.assertEqual(len(q.subqueries), 1) + subq = q.subqueries[0] + self.assertTrue(isinstance(subq, beets.library.SubstringQuery)) + self.assertEqual(subq.field, 'albumtype') + self.assertEqual(subq.pattern, 'soundtrack') + def suite(): return unittest.TestLoader().loadTestsFromName(__name__)