mirror of
https://github.com/beetbox/beets.git
synced 2025-12-29 12:02:44 +01:00
Merge pull request #1330 from brunal/path-query-case-sensitivity
Fix path query case sensitivity Conflicts: docs/changelog.rst
This commit is contained in:
commit
e14f28fdda
5 changed files with 61 additions and 9 deletions
|
|
@ -24,6 +24,7 @@ import unicodedata
|
|||
import time
|
||||
import re
|
||||
from unidecode import unidecode
|
||||
import platform
|
||||
|
||||
from beets import logging
|
||||
from beets.mediafile import MediaFile, MutagenError, UnreadableFileError
|
||||
|
|
@ -42,30 +43,50 @@ log = logging.getLogger('beets')
|
|||
# Library-specific query types.
|
||||
|
||||
class PathQuery(dbcore.FieldQuery):
|
||||
"""A query that matches all items under a given path."""
|
||||
"""A query that matches all items under a given path.
|
||||
|
||||
On Windows paths are case-insensitive by default, contrarly to UNIX
|
||||
platforms.
|
||||
"""
|
||||
|
||||
escape_re = re.compile(r'[\\_%]')
|
||||
escape_char = b'\\'
|
||||
|
||||
def __init__(self, field, pattern, fast=True):
|
||||
_is_windows = platform.system() == 'Windows'
|
||||
|
||||
def __init__(self, field, pattern, fast=True, case_sensitive=None):
|
||||
super(PathQuery, self).__init__(field, pattern, fast)
|
||||
|
||||
if case_sensitive is None:
|
||||
# setting this value as the default one would make it un-patchable
|
||||
# and therefore un-testable
|
||||
case_sensitive = not self._is_windows
|
||||
if not case_sensitive:
|
||||
pattern = pattern.lower()
|
||||
self.case_sensitive = case_sensitive
|
||||
|
||||
# Match the path as a single file.
|
||||
self.file_path = util.bytestring_path(util.normpath(pattern))
|
||||
# As a directory (prefix).
|
||||
self.dir_path = util.bytestring_path(os.path.join(self.file_path, b''))
|
||||
|
||||
def match(self, item):
|
||||
return (item.path == self.file_path) or \
|
||||
item.path.startswith(self.dir_path)
|
||||
path = item.path if self.case_sensitive else item.path.lower()
|
||||
return (path == self.file_path) or path.startswith(self.dir_path)
|
||||
|
||||
def col_clause(self):
|
||||
file_blob = buffer(self.file_path)
|
||||
|
||||
if self.case_sensitive:
|
||||
dir_blob = buffer(self.dir_path)
|
||||
return '({0} = ?) || (substr({0}, 1, ?) = ?)'.format(self.field), \
|
||||
(file_blob, len(dir_blob), dir_blob)
|
||||
|
||||
escape = lambda m: self.escape_char + m.group(0)
|
||||
dir_pattern = self.escape_re.sub(escape, self.dir_path)
|
||||
dir_pattern = buffer(dir_pattern + b'%')
|
||||
file_blob = buffer(self.file_path)
|
||||
dir_blob = buffer(dir_pattern + b'%')
|
||||
return '({0} = ?) || ({0} LIKE ? ESCAPE ?)'.format(self.field), \
|
||||
(file_blob, dir_pattern, self.escape_char)
|
||||
(file_blob, dir_blob, self.escape_char)
|
||||
|
||||
|
||||
# Library-specific field types.
|
||||
|
|
@ -1092,11 +1113,12 @@ def parse_query_string(s, model_cls):
|
|||
|
||||
The string is split into components using shell-like syntax.
|
||||
"""
|
||||
assert isinstance(s, unicode), "Query is not unicode: {0!r}".format(s)
|
||||
|
||||
# A bug in Python < 2.7.3 prevents correct shlex splitting of
|
||||
# Unicode strings.
|
||||
# http://bugs.python.org/issue6988
|
||||
if isinstance(s, unicode):
|
||||
s = s.encode('utf8')
|
||||
s = s.encode('utf8')
|
||||
try:
|
||||
parts = [p.decode('utf8') for p in shlex.split(s)]
|
||||
except ValueError as exc:
|
||||
|
|
|
|||
|
|
@ -76,6 +76,7 @@ Fixes:
|
|||
|
||||
* :doc:`/plugins/replaygain`: Stop applying replaygain directly to source files
|
||||
when using the mp3gain backend. :bug:`1316`
|
||||
* Path queries are case-sensitive on UNIX OSes. :bug:`1165`
|
||||
* :doc:`/plugins/lyrics`: Silence a warning about insecure requests in the new
|
||||
MusixMatch backend. :bug:`1204`
|
||||
* Fix a crash when ``beet`` is invoked without arguments. :bug:`1205`
|
||||
|
|
|
|||
|
|
@ -184,6 +184,8 @@ Note that this only matches items that are *already in your library*, so a path
|
|||
query won't necessarily find *all* the audio files in a directory---just the
|
||||
ones you've already added to your beets library.
|
||||
|
||||
Such queries are case-sensitive on UNIX and case-insensitive on Microsoft
|
||||
Windows.
|
||||
|
||||
.. _query-sort:
|
||||
|
||||
|
|
|
|||
|
|
@ -1098,6 +1098,10 @@ class ParseQueryTest(unittest.TestCase):
|
|||
self.assertIsInstance(raised.exception,
|
||||
beets.dbcore.query.ParsingError)
|
||||
|
||||
def test_parse_bytes(self):
|
||||
with self.assertRaises(AssertionError):
|
||||
beets.library.parse_query_string(b"query", None)
|
||||
|
||||
|
||||
def suite():
|
||||
return unittest.TestLoader().loadTestsFromName(__name__)
|
||||
|
|
|
|||
|
|
@ -17,6 +17,9 @@
|
|||
from __future__ import (division, absolute_import, print_function,
|
||||
unicode_literals)
|
||||
|
||||
from functools import partial
|
||||
from mock import patch
|
||||
|
||||
from test import _common
|
||||
from test._common import unittest
|
||||
from test import helper
|
||||
|
|
@ -461,6 +464,26 @@ class PathQueryTest(_common.LibTestCase, TestHelper, AssertsMixin):
|
|||
results = self.lib.albums(q)
|
||||
self.assert_albums_matched(results, ['album with backslash'])
|
||||
|
||||
def test_case_sensitivity(self):
|
||||
self.add_album(path='/A/B/C2.mp3', title='caps path')
|
||||
|
||||
makeq = partial(beets.library.PathQuery, 'path', '/A/B')
|
||||
|
||||
results = self.lib.items(makeq(case_sensitive=True))
|
||||
self.assert_items_matched(results, ['caps path'])
|
||||
|
||||
results = self.lib.items(makeq(case_sensitive=False))
|
||||
self.assert_items_matched(results, ['path item', 'caps path'])
|
||||
|
||||
# test platform-aware default sensitivity
|
||||
with patch('beets.library.PathQuery._is_windows', False):
|
||||
q = makeq()
|
||||
self.assertEqual(q.case_sensitive, True)
|
||||
|
||||
with patch('beets.library.PathQuery._is_windows', True):
|
||||
q = makeq()
|
||||
self.assertEqual(q.case_sensitive, False)
|
||||
|
||||
|
||||
class IntQueryTest(unittest.TestCase, TestHelper):
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue