]>
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 # this check belongs in Tags
47 if len(track_tags
) != 1:
48 sys
.stderr
.write('bogus TRACKNUMBER %s: %s\n' % (track_tags
, fn
))
50 track
= int(track_tags
[0])
51 for tag
, values
in tags
._global
.iteritems():
52 # Makes no sense to save TRACKNUMBER in coll_tags.
53 if tag
== 'TRACKNUMBER':
57 all_tags
[tag
].add(value
)
59 all_tags
[tag
] = set([value
])
60 coll_tags
.set(tag
, value
, track
)
61 for tag
, values
in all_tags
.iteritems():
63 # Only one value for this tag, so add it to global tags.
64 coll_tags
.set(tag
, list(values
)[0])
65 # And now remove it from each track tags.
66 for track
, tags
in coll_tags
._tracks
.iteritems():
68 print '\n'.join(coll_tags
.all())
73 tags
.load(open(args
.pop(0)))
74 if len(args
) != len(tags
):
75 sys
.stderr
.write('expected %d flac files, got %d\n'
76 % (len(tags
), len(args
)))
78 artist
= tags
.get_path_safe('ARTIST')
79 album
= tags
.get_path_safe('ALBUM'),
85 album_path
= artist
+ '/' + album
91 for i
, old_fn
in enumerate(args
):
93 fn
= '%s/%s/%s.flac' % (artist
, album
, tags
.make_filename(track
))
95 #os.rename(old_fn, fn)
101 if args
[1] == 'read':
102 return do_read(args
[2:])
103 if args
[1] == 'write':
104 return do_write(args
[2:])
110 if __name__
== '__main__':
111 sys
.exit(main(sys
.argv
))