4 from subprocess import Popen, PIPE
6 from org.diplodocus.util import run_or_die
8 from flac_archive.tags import Tags
10 class SubprocessError(Exception):
11 def __init__(self, status, command=None, stderr=None):
15 command_msg = ': ' + ' '.join(command)
17 msg = 'exited due to signal %d%s'
19 msg = 'exit status %d%s'
20 Exception.__init__(self, msg % (abs(status), command_msg))
22 self.command = command
28 command = ['metaflac', '--no-utf8-convert', '--export-tags-to=-', fn]
29 p = Popen(command, stdout=PIPE)
34 raise SubprocessError(status, command=command, stderr=p.stderr)
38 def do_read(filenames):
39 # Use this mapping of tag names to sets of tag values to detect global tags.
41 # Build the collated result in this Tags object.
43 # XXX The Tags interface is horrible. It's gotta be almost 10 years since
44 # I wrote it, so not surprising...
47 track_tags = tags.get('TRACKNUMBER')
48 if len(track_tags) != 1:
49 raise Exception('bogus TRACKNUMBER %s for %s' % (track_tags, fn))
50 track = int(track_tags[0])
51 for tag, values in tags._global.iteritems():
54 all_tags[tag].add(value)
56 all_tags[tag] = set([value])
57 coll_tags.set(tag, value, track)
58 for tag, values in all_tags.iteritems():
60 # Only one value for this tag, so add it to global tags.
61 coll_tags.set(tag, list(values)[0])
62 # And now remove it from each track tags.
63 for track, tags in coll_tags._tracks.iteritems():
65 print '\n'.join(coll_tags.all())
72 return do_read(args[2:])
73 if args[1] == 'write':
74 return do_write(args[2:])
80 if __name__ == '__main__':
81 sys.exit(main(sys.argv))