|
5 | 5 | import sys |
6 | 6 | import time |
7 | 7 | import unicodedata |
8 | | -from contextlib import suppress |
9 | 8 | from functools import cached_property |
10 | 9 | from pathlib import Path |
11 | 10 | from typing import TYPE_CHECKING, Any, ClassVar |
12 | 11 |
|
13 | 12 | from mediafile import MediaFile, UnreadableFileError |
14 | 13 |
|
15 | 14 | import beets |
16 | | -from beets import dbcore, logging, plugins, util |
| 15 | +from beets import context, dbcore, logging, plugins, util |
17 | 16 | from beets.dbcore import types |
18 | 17 | from beets.util import ( |
19 | 18 | MoveOperation, |
@@ -93,22 +92,16 @@ def _setitem(self, key: str, value: Any): |
93 | 92 | # Store paths relative to the music directory |
94 | 93 | # Check for absolute path because item may be initialised with |
95 | 94 | # a relative path already |
96 | | - if os.path.isabs(value): |
97 | | - # Suppress these errors since tests may initialise an Item |
98 | | - # without the db attribute |
99 | | - with suppress(ValueError, AttributeError): |
100 | | - value = os.path.relpath(value, self.db.directory) |
| 95 | + if os.path.isabs(value) and (music_dir := context.get_music_dir()): |
| 96 | + value = os.path.relpath(value, music_dir) |
101 | 97 |
|
102 | 98 | return super()._setitem(key, value) |
103 | 99 |
|
104 | 100 | def __getitem__(self, key: str): |
105 | 101 | value = super().__getitem__(key) |
106 | 102 | if key == "path" and value: |
107 | 103 | # Return absolute paths. |
108 | | - # Suppress these errors since tests may initialise an Item |
109 | | - # without the db attribute |
110 | | - with suppress(ValueError, AttributeError): |
111 | | - value = os.path.join(self.db.directory, value) |
| 104 | + value = normpath(os.path.join(context.get_music_dir(), value)) |
112 | 105 |
|
113 | 106 | return value |
114 | 107 |
|
|
0 commit comments