X-Git-Url: https://diplodocus.org/git/flac-archive/blobdiff_plain/18d637995eccbb024ead66d351b8654870865337..5fbfc29eae00bde7562adba75406161c9bf756d2:/rewrite-tags diff --git a/rewrite-tags b/rewrite-tags index a4650cf..e164f3f 100755 --- a/rewrite-tags +++ b/rewrite-tags @@ -3,29 +3,33 @@ import sys from subprocess import Popen, PIPE -from org.diplodocus.util import run_or_die - from flac_archive.tags import Tags class SubprocessError(Exception): - def __init__(self, status, stderr=None): + def __init__(self, status, command=None, stderr=None): + if command is None: + command_msg = None + else: + command_msg = ': ' + ' '.join(command) if status < 0: - msg = 'exited due to signal %d' + msg = 'exited due to signal %d%s' else: - msg = 'exit status %d' - Exception.__init__(self, msg % (status,)) + msg = 'exit status %d%s' + Exception.__init__(self, msg % (abs(status), command_msg)) self.status = status + self.command = command self.stderr = stderr def get_tags(fn): tags = Tags() - p = Popen(['metaflac', '--no-utf8-convert', '--export-tags-to=-', fn], - stdout=PIPE) + + command = ['metaflac', '--no-utf8-convert', '--export-tags-to=-', fn] + p = Popen(command, stdout=PIPE) tags.load(p.stdout) status = p.wait() if status != 0: - raise SubprocessError(status, p.stderr) + raise SubprocessError(status, command=command, stderr=p.stderr) return tags @@ -34,13 +38,20 @@ def do_read(filenames): all_tags = {} # Build the collated result in this Tags object. coll_tags = Tags() + # XXX The Tags interface is horrible. It's gotta be almost 10 years since + # I wrote it, so not surprising... for fn in filenames: tags = get_tags(fn) track_tags = tags.get('TRACKNUMBER') + # this check belongs in Tags if len(track_tags) != 1: - raise Exception('bogus TRACKNUMBER %s for %s' % (track_tags, fn)) + sys.stderr.write('bogus TRACKNUMBER %s: %s\n' % (track_tags, fn)) + return 3 track = int(track_tags[0]) for tag, values in tags._global.iteritems(): + # Makes no sense to save TRACKNUMBER in coll_tags. + if tag == 'TRACKNUMBER': + continue for value in values: if tag in all_tags: all_tags[tag].add(value) @@ -55,6 +66,34 @@ def do_read(filenames): for track, tags in coll_tags._tracks.iteritems(): del tags[tag] print '\n'.join(coll_tags.all()) + return 0 + +def do_write(args): + tags = Tags() + tags.load(open(args.pop(0))) + if len(args) != len(tags): + sys.stderr.write('expected %d flac files, got %d\n' + % (len(tags), len(args))) + return 4 + artist = tags.get_path_safe('ARTIST') + album = tags.get_path_safe('ALBUM'), + try: + os.mkdir(artist) + except OSError, e: + if e.errno != EEXIST: + raise + album_path = artist + '/' + album + try: + os.mkdir(album_path) + except OSError, e: + if e.errno != EEXIST: + raise + for i, old_fn in enumerate(args): + track = i + 1 + fn = '%s/%s/%s.flac' % (artist, album, tags.make_filename(track)) + if fn != old_fn: + #os.rename(old_fn, fn) + pass def main(args): if len(args) < 3: