]> diplodocus.org Git - flac-archive/blobdiff - flac2mp3
Move Tags class into new flac_archive package, fix a bug in it, and
[flac-archive] / flac2mp3
index 1e29ad0bc04f727d947667bf6dc8107d747cc02e..3f0ef90be5c4b132668cacc9a7c1b5d3f6660f5f 100755 (executable)
--- a/flac2mp3
+++ b/flac2mp3
@@ -58,6 +58,8 @@ import org.diplodocus.jobs
 from org.diplodocus import flac, taglib
 from org.diplodocus.util import run_or_die
 
+from flac_archive.tags import Tags
+
 ################################################################################
 # The child processes
 
@@ -146,42 +148,6 @@ def get_decode_args(fn):
 
     return args
 
-# XXX other things should usue this; flac files, for example, should
-# get PART as part of the filelname, same as mp3s.
-class Tags(object):
-    def __init__(self):
-        self._global = {}
-        self._tags = {}
-    def __len__(self):
-        # All files have at least one track.
-        return max(1, len(self._tags))
-    def get(self, key, track=None):
-        key = key.upper()
-        try:
-            try:
-                return self._tags[track][key]
-            except KeyError:
-                return self._global[key]
-        except KeyError:
-            return None
-    def gets(self, key, track=None):
-        value = self.get(key, track)
-        if value == None:
-            return None
-        return '\n'.join(value)
-    def set(self, key, value, track=None):
-        key = key.upper()
-        if track == None:
-            tags = self._global
-        else:
-            try:
-                tags = self._tags[track]
-            except KeyError:
-                tags = self._tags[track] = {}
-        if key not in tags:
-            tags[key] = []
-        tags[key].append(value)
-
 def get_tags(fn):
     """Return the ARTIST, ALBUM, and DATE tags followed by the TITLE tags
     in the file FN."""
@@ -189,17 +155,8 @@ def get_tags(fn):
     tags = Tags()
 
     p = Popen(['metaflac', '--export-tags-to=-', fn], stdout=PIPE)
-    for line in (x.rstrip() for x in p.stdout):
-        (tag, value) = line.split('=', 1)
-
-        m = re.search(r'\[([0-9]+)]$', tag)
-        if m != None:
-            tag = tag[:m.start()]
-            track = int(m.group(1))
-        else:
-            track = None
+    tags.load(p.stdout)
 
-        tags.set(tag, value, track)
     # XXX dataloss!  check status
     status = p.wait()