Explorar o código

tag_interface.ID3/MP4.get_free_int(): return None instead of throwing KeyError

Fabian Peter Hammerle %!s(int64=6) %!d(string=hai) anos
pai
achega
56b31af8d0
Modificáronse 3 ficheiros con 19 adicións e 15 borrados
  1. 2 1
      symuid-sync
  2. 4 4
      symuid/__init__.py
  3. 13 10
      symuid/tag_interface.py

+ 2 - 1
symuid-sync

@@ -31,7 +31,8 @@ def symuid_sync(path, path_ignore_regex, show_ignored=False):
             root_path=path,
             path_ignore_regex=path_ignore_regex,
             ignored_cb=lambda p: show_ignored and log_path(p, 'ignored'),
-            unsupported_cb=lambda p, e: log_path_error(p, 'unsupported type, skipped'),
+            unsupported_cb=lambda p, e:
+                log_path_error(p, 'unsupported type, skipped'),
         ):
         if track.get_uuid() is None:
             track.assign_uuid(generate_uuid())

+ 4 - 4
symuid/__init__.py

@@ -97,14 +97,14 @@ class Track:
         tag_label = 'symuid:pcnt:{}:{}:{:.0f}'.format(
             player, library_id, register_dt.timestamp(),
         )
-        try:
-            current_count = self._iface.get_free_int(tag_label)
-            assert current_count == play_count, (current_count, play_count)
-        except KeyError:
+        current_count = self._iface.get_free_int(tag_label)
+        if current_count is None:
             new_tag = self._iface.set_free_int(tag_label, play_count)
             self._iface.save()
             if tag_set_cb:
                 tag_set_cb(self, new_tag)
+        elif current_count != play_count:
+            raise Exception((current_count, play_count))
 
     @classmethod
     def walk(cls, root_path, path_ignore_regex, ignored_cb, unsupported_cb):

+ 13 - 10
symuid/tag_interface.py

@@ -34,7 +34,7 @@ class ID3(_mutagen):
     def get_free_int(self, tag_label):
         tags = [t for t in self.get_free_ints(tag_label)]
         if len(tags) == 0:
-            raise KeyError(tag_label)
+            return None
         else:
             assert len(tags) == 1, tags
             assert tags[0][0] == tag_label, tag
@@ -94,16 +94,22 @@ class MP4(_mutagen):
     def _get_free(self, tag_label):
         # freeform keys start with '----'
         # http://mutagen.readthedocs.io/en/latest/api/mp4.html
-        tags = self._mutagen_file.tags['----:' + tag_label]
-        assert len(tags) == 1, tags
-        return tags[0]
+        try:
+            tags = self._mutagen_file.tags['----:' + tag_label]
+        except KeyError:
+            return None
+        if len(tags) == 1:
+            return tags[0]
+        else:
+            raise ValueError(tags)
 
     def get_free_int(self, tag_label):
-        return MP4._freeform_to_int(self._get_free(tag_label))
+        t = self._get_free(tag_label)
+        return None if t is None else MP4._freeform_to_int(t)
 
     def _get_free_uuid(self, tag_label):
         tag = self._get_free(tag_label)
-        assert tag.dataformat == mutagen.mp4.AtomDataType.UUID, tag.dataformat
+        assert tag is None or tag.dataformat == mutagen.mp4.AtomDataType.UUID, tag.dataformat
         return tag
 
     def set_free_int(self, tag_label, data):
@@ -128,10 +134,7 @@ class MP4(_mutagen):
         return tag
 
     def get_track_uuid(self):
-        try:
-            return self._get_free_uuid(self._UUID_TAG_KEY)
-        except KeyError:
-            return None
+        return self._get_free_uuid(self._UUID_TAG_KEY)
 
     def set_track_uuid(self, uuid):
         return self._set_free_uuid(self._UUID_TAG_KEY, uuid)