]>
diplodocus.org Git - flac-archive/blob - rewrite-tags
4 from subprocess
import Popen
, PIPE
6 from flac_archive
.tags
import Tags
8 class SubprocessError(Exception):
9 def __init__(self
, status
, command
=None, stderr
=None):
13 command_msg
= ': ' + ' '.join(command
)
15 msg
= 'exited due to signal %d%s'
17 msg
= 'exit status %d%s'
18 Exception.__init
__(self
, msg
% (abs(status
), command_msg
))
20 self
.command
= command
26 command
= ['metaflac', '--no-utf8-convert', '--export-tags-to=-', fn
]
27 p
= Popen(command
, stdout
=PIPE
)
32 raise SubprocessError(status
, command
=command
, stderr
=p
.stderr
)
36 def do_read(filenames
):
37 # Use this mapping of tag names to sets of tag values to detect global tags.
39 # Build the collated result in this Tags object.
41 # XXX The Tags interface is horrible. It's gotta be almost 10 years since
42 # I wrote it, so not surprising...
45 track_tags
= tags
.get('TRACKNUMBER')
46 if len(track_tags
) != 1:
47 sys
.stderr
.write('bogus TRACKNUMBER %s: %s\n' % (track_tags
, fn
))
49 track
= int(track_tags
[0])
50 for tag
, values
in tags
._global
.iteritems():
53 all_tags
[tag
].add(value
)
55 all_tags
[tag
] = set([value
])
56 coll_tags
.set(tag
, value
, track
)
57 for tag
, values
in all_tags
.iteritems():
59 # Only one value for this tag, so add it to global tags.
60 coll_tags
.set(tag
, list(values
)[0])
61 # And now remove it from each track tags.
62 for track
, tags
in coll_tags
._tracks
.iteritems():
64 print '\n'.join(coll_tags
.all())
69 tags
.load(open(args
.pop(0)))
70 if len(args
) != len(tags
):
71 sys
.stderr
.write('expected %d flac files, got %d\n'
72 % (len(tags
), len(args
)))
74 artist
= tags
.get_path_safe('ARTIST')
75 album
= tags
.get_path_safe('ALBUM'),
81 album_path
= artist
+ '/' + album
87 for i
, old_fn
in enumerate(args
):
89 fn
= '%s/%s/%s.flac' % (artist
, album
, tags
.make_filename(track
))
91 #os.rename(old_fn, fn)
97 return do_read(args
[2:])
98 if args
[1] == 'write':
99 return do_write(args
[2:])
105 if __name__
== '__main__':
106 sys
.exit(main(sys
.argv
))