Skip to content

Commit

Permalink
Use os.fsdecode() to convert path to string
Browse files Browse the repository at this point in the history
Fixes #205
  • Loading branch information
mathiascode committed Mar 16, 2024
1 parent 2ca8431 commit 224acb1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion tinytag/tests/test_all.py
Expand Up @@ -146,7 +146,7 @@
'extra': {'love rating': 'L', 'publisher': 'Century Media', 'popm': 'MusicBee\x00Ä'},
'genre': 'Power Metal', 'title': 'Time What Is Time', 'track': 1,
'year': '1992'}),
('samples/nicotinetestdata.mp3',
('samples/non_ascii_filename_äää.mp3',
{'extra': {'encoder_settings': 'Lavf58.20.100'}, 'filesize': 80919, 'channels': 2,
'duration': 5.067755102040817, 'samplerate': 44100, 'bitrate': 127.6701030927835}),
('samples/chinese_id3.mp3',
Expand Down
24 changes: 10 additions & 14 deletions tinytag/tinytag.py
Expand Up @@ -76,7 +76,7 @@ class TinyTag:
'.aiff', '.aifc', '.aif', '.afc'
)
_EXTRA_PREFIX = 'extra.'
_file_extension_mapping: dict[tuple[bytes, ...], type[TinyTag]] | None = None
_file_extension_mapping: dict[tuple[str, ...], type[TinyTag]] | None = None
_magic_bytes_mapping: dict[bytes, type[TinyTag]] | None = None

def __init__(self) -> None:
Expand Down Expand Up @@ -161,21 +161,17 @@ def _get_parser_for_filename(
cls, filename: bytes | str | PathLike[Any]) -> type[TinyTag] | None:
if cls._file_extension_mapping is None:
cls._file_extension_mapping = {
(b'.mp1', b'.mp2', b'.mp3'): _ID3,
(b'.oga', b'.ogg', b'.opus', b'.spx'): _Ogg,
(b'.wav',): _Wave,
(b'.flac',): _Flac,
(b'.wma',): _Wma,
(b'.m4b', b'.m4a', b'.m4r', b'.m4v', b'.mp4', b'.aax', b'.aaxc'): _MP4,
(b'.aiff', b'.aifc', b'.aif', b'.afc'): _Aiff,
('.mp1', '.mp2', '.mp3'): _ID3,
('.oga', '.ogg', '.opus', '.spx'): _Ogg,
('.wav',): _Wave,
('.flac',): _Flac,
('.wma',): _Wma,
('.m4b', '.m4a', '.m4r', '.m4v', '.mp4', '.aax', '.aaxc'): _MP4,
('.aiff', '.aifc', '.aif', '.afc'): _Aiff,
}
filename = os.fspath(filename).lower()
if isinstance(filename, str):
filename_bytes = filename.encode('ascii')
else:
filename_bytes = filename
filename = os.fsdecode(filename).lower()
for ext, tagclass in cls._file_extension_mapping.items():
if filename_bytes.endswith(ext):
if filename.endswith(ext):
return tagclass
return None

Expand Down

0 comments on commit 224acb1

Please sign in to comment.