]>
diplodocus.org Git - flac-archive/blob - rewrite-tags
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
, stderr
=None):
13 msg
= 'exited due to signal %d'
15 msg
= 'exit status %d'
16 Exception.__init
__(self
, msg
% (abs(status
),))
22 p
= Popen(['metaflac', '--no-utf8-convert', '--export-tags-to=-', fn
],
28 raise SubprocessError(status
, p
.stderr
)
32 def do_read(filenames
):
33 # Use this mapping of tag names to sets of tag values to detect global tags.
35 # Build the collated result in this Tags object.
37 # XXX The Tags interface is horrible. It's gotta be almost 10 years since
38 # I wrote it, so not surprising...
41 track_tags
= tags
.get('TRACKNUMBER')
42 if len(track_tags
) != 1:
43 raise Exception('bogus TRACKNUMBER %s for %s' % (track_tags
, fn
))
44 track
= int(track_tags
[0])
45 for tag
, values
in tags
._global
.iteritems():
48 all_tags
[tag
].add(value
)
50 all_tags
[tag
] = set([value
])
51 coll_tags
.set(tag
, value
, track
)
52 for tag
, values
in all_tags
.iteritems():
54 # Only one value for this tag, so add it to global tags.
55 coll_tags
.set(tag
, list(values
)[0])
56 # And now remove it from each track tags.
57 for track
, tags
in coll_tags
._tracks
.iteritems():
59 print '\n'.join(coll_tags
.all())
65 return do_read(args
[2:])
66 if args
[1] == 'write':
67 return do_write(args
[2:])
73 if __name__
== '__main__':
74 sys
.exit(main(sys
.argv
))