Browse Source

Track: added support for opus in ogg container

Fabian Peter Hammerle 4 years ago
parent
commit
d90516c7ab
3 changed files with 70 additions and 11 deletions
  1. 2 0
      symuid/__init__.py
  2. 31 0
      tests/test_sync.py
  3. 37 11
      tests/test_track.py

+ 2 - 0
symuid/__init__.py

@@ -57,6 +57,8 @@ class Track:
             return _tag_interface.ID3(mutagen_file)
         if isinstance(mutagen_file.tags, mutagen.mp4.MP4Tags):
             return _tag_interface.MP4(mutagen_file)
+        if isinstance(mutagen_file, mutagen.oggopus.OggOpus):
+            return _tag_interface.OggOpus(mutagen_file)
         raise NotImplementedError((track_path, type(mutagen_file)))
 
     @property

+ 31 - 0
tests/test_sync.py

@@ -0,0 +1,31 @@
+import os
+import re
+import unittest.mock
+
+from symuid import Track
+from symuid.sync import _main, sync
+
+DUMMY_PATH_IGNORE_REGEX = re.compile(r'\.jpg$')
+
+
+def test_sync(empty_ogg_opus_path):
+    assert Track(empty_ogg_opus_path).get_uuid() is None
+    sync(os.path.dirname(empty_ogg_opus_path),
+         path_ignore_regex=DUMMY_PATH_IGNORE_REGEX)
+    assert Track(empty_ogg_opus_path).get_uuid() is not None
+
+
+def test_sync_idempotent(empty_ogg_opus_path):
+    sync(os.path.dirname(empty_ogg_opus_path),
+         path_ignore_regex=DUMMY_PATH_IGNORE_REGEX)
+    uuid = Track(empty_ogg_opus_path).get_uuid()
+    sync(os.path.dirname(empty_ogg_opus_path),
+         path_ignore_regex=DUMMY_PATH_IGNORE_REGEX)
+    assert Track(empty_ogg_opus_path).get_uuid() == uuid
+
+
+def test_main(empty_ogg_opus_path):
+    assert Track(empty_ogg_opus_path).get_uuid() is None
+    with unittest.mock.patch('sys.argv', ['', os.path.dirname(empty_ogg_opus_path)]):
+        _main()
+    assert Track(empty_ogg_opus_path).get_uuid() is not None

+ 37 - 11
tests/test_track.py

@@ -1,5 +1,6 @@
 import datetime as dt
 import os
+import re
 
 import mutagen
 import pytest
@@ -22,20 +23,22 @@ def empty_id3_track(empty_id3_path):
     return symuid.Track(empty_id3_path)
 
 
-@pytest.mark.parametrize('path', [
-    os.path.join(TRACKS_DIR_PATH, 'id3v2.4-typical.mp3'),
-    os.path.join(TRACKS_DIR_PATH, 'id3v2.4-empty.mp3'),
+@pytest.mark.parametrize('track_name', [
+    'id3v2.4-typical.mp3',
+    'id3v2.4-empty.mp3',
+    'ogg-opus-typical.opus',
 ])
-def test_init(path):
-    symuid.Track(path)
+def test_init(tracks_dir_path, track_name):
+    symuid.Track(os.path.join(tracks_dir_path, track_name))
 
 
-@pytest.mark.parametrize(('path', 'expected_comment'), [
-    (os.path.join(TRACKS_DIR_PATH, 'id3v2.4-typical.mp3'), 'some comment'),
-    (os.path.join(TRACKS_DIR_PATH, 'id3v2.4-empty.mp3'), None),
+@pytest.mark.parametrize(('track_name', 'expected_comment'), [
+    ('id3v2.4-typical.mp3', 'some comment'),
+    ('id3v2.4-empty.mp3', None),
+    ('ogg-opus-typical.opus', 'some comment'),
 ])
-def test_get_comment(path, expected_comment):
-    track = symuid.Track(path)
+def test_get_comment(tracks_dir_path, track_name, expected_comment):
+    track = symuid.Track(os.path.join(tracks_dir_path, track_name))
     assert expected_comment == track.comment
 
 
@@ -51,7 +54,7 @@ def test_set_comment(empty_id3_track):
 @pytest.mark.parametrize(('play_count'), [
     symuid.PlayCount('pytest', 'lib', utc_dt(), 7),
 ])
-def test_register_play_count(empty_id3_track, play_count):
+def test_register_play_count_id3(empty_id3_track, play_count):
     empty_id3_track.register_play_count(play_count)
     tags = mutagen.File(empty_id3_track.path).tags
     assert len(tags) == 1
@@ -64,6 +67,21 @@ def test_register_play_count(empty_id3_track, play_count):
     assert tag.text == [str(play_count.count)]
 
 
+@pytest.mark.parametrize(('play_count'), [
+    symuid.PlayCount('pytest', 'lib', utc_dt(), 7),
+])
+def test_register_play_count_opus(empty_ogg_opus_path, play_count):
+    track = symuid.Track(empty_ogg_opus_path)
+    track.register_play_count(play_count)
+    tags = mutagen.File(track.path).tags
+    assert len(tags) == 1
+    expected_desc = 'symuid:pcnt:{}:{}:{}'.format(
+        play_count.player, play_count.library_id, int(
+            play_count.register_dt.timestamp()),
+    )
+    assert tags[expected_desc] == [str(play_count.count)]
+
+
 @pytest.mark.parametrize(('expected_counts'), [
     [],
     [symuid.PlayCount('player', 'lib', utc_dt(), 3)],
@@ -170,3 +188,11 @@ def test_increase_play_count_others(empty_id3_track):
     assert len(list(
         empty_id3_track._get_play_counts(player='a', library_id='1')
     )) == 2
+
+
+def test_walk(tracks_dir_path):
+    tracks = symuid.Track.walk(
+        tracks_dir_path,
+        path_ignore_regex=re.compile(r'typical'))
+    track_names = set(os.path.basename(t.path) for t in tracks)
+    assert track_names == {'id3v2.4-empty.mp3', 'ogg-opus-empty.opus'}