Browse Source

added symuid.Track.walk()

Fabian Peter Hammerle 5 years ago
parent
commit
30d7ec8e63
2 changed files with 36 additions and 26 deletions
  1. 19 25
      symuid-sync
  2. 17 1
      symuid/__init__.py

+ 19 - 25
symuid-sync

@@ -9,6 +9,8 @@ import re
 import subprocess
 import sys
 
+import symuid
+
 # http://id3.org/id3v2.4.0-frames#4.1.
 TRACK_UUID_ID3_OWNER_ID = 'symuid'
 # freeform keys start with '----'
@@ -61,33 +63,25 @@ def get_or_assign_uuid_mp4(mp4_file):
     return mp4_file[TRACK_UUID_MP4_TAG][0]
 
 
+def log_path(track_path, msg, stream=sys.stdout):
+    stream.write("{!r}: {}\n".format(track_path, msg))
+
+def log_path_error(track_path, msg):
+    log_path(track_path, msg, stream=sys.stderr)
+
 def symuid_sync(path, path_ignore_regex, show_ignored=False):
-    if path_ignore_regex.search(path):
-        if show_ignored:
-            print("{!r}: path matches ignore pattern".format(path))
-    elif os.path.isdir(path):
-        for dirpath, dirnames, filenames in os.walk(path):
-            for filename in filenames:
-                symuid_sync(
-                    os.path.join(dirpath, filename),
-                    path_ignore_regex,
-                    show_ignored,
-                )
-    else:
-        try:
-            f = mutagen.File(filename=path)
-        except Exception:
-            raise Exception(path)
-        if not f:
-            sys.stderr.write(
-                "{!r}: unsupported filetype, skipped\n".format(path),
-            )
-        elif isinstance(f, mutagen.mp4.MP4):
-            get_or_assign_uuid_mp4(f)
-        elif isinstance(f.tags, mutagen.id3.ID3):
-            get_or_assign_uuid_id3(f.tags)
+    for track in symuid.Track.walk(
+            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'),
+        ):
+        if isinstance(track._iface, symuid.tag_interface.MP4):
+            get_or_assign_uuid_mp4(track._iface._mutagen_file)
+        elif isinstance(track._iface, symuid.tag_interface.ID3):
+            get_or_assign_uuid_id3(track._iface._mutagen_file.tags)
         else:
-            raise Exception(f)
+            raise Exception(track)
 
 
 def _init_argparser():

+ 17 - 1
symuid/__init__.py

@@ -3,6 +3,7 @@
 import datetime as dt
 import dateutil.tz
 import mutagen
+import os
 
 import symuid.tag_interface
 
@@ -31,7 +32,9 @@ class Track:
 
     def __init__(self, path):
         mutagen_file = mutagen.File(filename=path)
-        if isinstance(mutagen_file.tags, mutagen.id3.ID3):
+        if mutagen_file is None:
+            raise NotImplementedError(path)
+        elif isinstance(mutagen_file.tags, mutagen.id3.ID3):
             self._iface = symuid.tag_interface.ID3(mutagen_file)
         elif isinstance(mutagen_file.tags, mutagen.mp4.MP4Tags):
             self._iface = symuid.tag_interface.MP4(mutagen_file)
@@ -93,3 +96,16 @@ class Track:
             self._iface.save()
             if tag_set_cb:
                 tag_set_cb(self, new_tag)
+
+    @classmethod
+    def walk(cls, root_path, path_ignore_regex, ignored_cb, unsupported_cb):
+        for dirpath, dirnames, filenames in os.walk(root_path):
+            for filename in filenames:
+                track_path = os.path.join(dirpath, filename)
+                if path_ignore_regex.search(track_path):
+                    ignored_cb(track_path)
+                else:
+                    try:
+                        yield cls(track_path)
+                    except NotImplementedError as e:
+                        unsupported_cb(track_path, e)