scanner: Have Metadata gracefully handle self.file == NULL

So we don't need to wrap the context manager in a try / except block

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-08-25 11:13:07 -04:00
parent a28d4d4b8e
commit 73646fc106
2 changed files with 7 additions and 2 deletions

View File

@ -9,11 +9,11 @@ class Metadata:
def __enter__(self):
self.file = mutagen.File(self.path)
return self
return self if self.file else None
def __exit__(self, exp_type, exp_value, traceback):
self.file = None
return exp_type == None
return (exp_type == None) or (exp_type == AttributeError)
def album(self):
return self.file.get("album", [ "Unknown Album" ])[0]

View File

@ -6,6 +6,7 @@ import unittest
test_tracks = pathlib.Path("./data/Test Album")
track_01 = test_tracks / "01 - Test Track.ogg"
track_02 = test_tracks / "02 - Test {Disc 2}.ogg"
text_txt = test_tracks / "text.txt"
class TestMetadata(unittest.TestCase):
def test_metadata_init(self):
@ -34,3 +35,7 @@ class TestMetadata(unittest.TestCase):
self.assertEqual(mdf.discsubtitle(), "Electric Boogaloo")
self.assertEqual(mdf.genres(), [ "Test", "Genre", "List" ])
self.assertEqual(mdf.year(), 2019)
def test_metadata_text_txt(self):
with scanner.metadata.Metadata(text_txt) as mdf:
mdf.artist()