30
30
from beets import util
31
31
32
32
if TYPE_CHECKING :
33
- from beets .dbcore import Model
34
- from beets .dbcore .db import AnyModel
33
+ from beets .dbcore .db import AnyModel , Model
35
34
36
35
P = TypeVar ("P" , default = Any )
37
36
else :
@@ -283,13 +282,11 @@ class PathQuery(FieldQuery[bytes]):
283
282
and case-sensitive otherwise.
284
283
"""
285
284
286
- def __init__ (self , field , pattern , fast = True ):
285
+ def __init__ (self , field : str , pattern : bytes , fast : bool = True ) -> None :
287
286
"""Create a path query.
288
287
289
288
`pattern` must be a path, either to a file or a directory.
290
289
"""
291
- super ().__init__ (field , pattern , fast )
292
-
293
290
path = util .normpath (pattern )
294
291
295
292
# Case sensitivity depends on the filesystem that the query path is located on.
@@ -304,13 +301,10 @@ def __init__(self, field, pattern, fast=True):
304
301
# from `col_clause()` do the same thing.
305
302
path = path .lower ()
306
303
307
- # Match the path as a single file.
308
- self .file_path = path
309
- # As a directory (prefix).
310
- self .dir_path = os .path .join (path , b"" )
304
+ super ().__init__ (field , path , fast )
311
305
312
- @classmethod
313
- def is_path_query (cls , query_part ) :
306
+ @staticmethod
307
+ def is_path_query (query_part : str ) -> bool :
314
308
"""Try to guess whether a unicode query part is a path query.
315
309
316
310
Condition: separator precedes colon and the file exists.
@@ -328,22 +322,20 @@ def is_path_query(cls, query_part):
328
322
329
323
return os .path .exists (util .syspath (util .normpath (query_part )))
330
324
331
- def match (self , item ):
332
- path = item .path if self .case_sensitive else item .path .lower ()
333
- return (path == self .file_path ) or path .startswith (self .dir_path )
334
-
335
- def col_clause (self ):
336
- file_blob = BLOB_TYPE (self .file_path )
337
- dir_blob = BLOB_TYPE (self .dir_path )
325
+ def match (self , obj : Model ) -> bool :
326
+ path = obj .path if self .case_sensitive else obj .path .lower ()
327
+ return path .startswith (self .pattern )
338
328
329
+ def col_clause (self ) -> tuple [str , Sequence [SQLiteType ]]:
339
330
if self .case_sensitive :
340
331
query_part = "({0} = ?) || (substr({0}, 1, ?) = ?)"
341
332
else :
342
333
query_part = "(BYTELOWER({0}) = BYTELOWER(?)) || \
343
334
(substr(BYTELOWER({0}), 1, ?) = BYTELOWER(?))"
344
335
336
+ dir_blob = BLOB_TYPE (os .path .join (self .pattern , b"" ))
345
337
return query_part .format (self .field ), (
346
- file_blob ,
338
+ BLOB_TYPE ( self . pattern ) ,
347
339
len (dir_blob ),
348
340
dir_blob ,
349
341
)
0 commit comments