]>
diplodocus.org Git - flac-archive/blob - rewrite-tags
4 from errno
import EEXIST
5 from subprocess
import Popen
, PIPE
7 from flac_archive
.tags
import Tags
9 class SubprocessError(Exception):
10 def __init__(self
, status
, command
=None, stderr
=None):
14 command_msg
= ': ' + ' '.join(command
)
16 msg
= 'exited due to signal %d%s'
18 msg
= 'exit status %d%s'
19 Exception.__init
__(self
, msg
% (abs(status
), command_msg
))
21 self
.command
= command
27 command
= ['metaflac', '--no-utf8-convert', '--export-tags-to=-', fn
]
28 p
= Popen(command
, stdout
=PIPE
)
33 raise SubprocessError(status
, command
=command
, stderr
=p
.stderr
)
37 def do_read(filenames
):
38 # Use this mapping of tag names to sets of tag values to detect global tags.
40 # Build the collated result in this Tags object.
42 # XXX The Tags interface is horrible. It's gotta be almost 10 years since
43 # I wrote it, so not surprising...
46 track_tags
= tags
.get('TRACKNUMBER')
47 # this check belongs in Tags
48 if len(track_tags
) != 1:
49 sys
.stderr
.write('bogus TRACKNUMBER %s: %s\n' % (track_tags
, fn
))
51 track
= int(track_tags
[0])
52 for tag
, values
in tags
._global
.iteritems():
53 # Makes no sense to save TRACKNUMBER in coll_tags.
54 if tag
== 'TRACKNUMBER':
58 all_tags
[tag
].add(value
)
60 all_tags
[tag
] = set([value
])
61 coll_tags
.set(tag
, value
, track
)
62 for tag
, values
in all_tags
.iteritems():
64 # Only one value for this tag, so add it to global tags.
65 coll_tags
.set(tag
, list(values
)[0])
66 # And now remove it from each track tags.
67 for track
, tags
in coll_tags
._tracks
.iteritems():
69 print '\n'.join(coll_tags
.all())
74 tags
.load(open(args
.pop(0)))
75 if len(args
) != len(tags
):
76 sys
.stderr
.write('expected %d flac files, got %d\n'
77 % (len(tags
), len(args
)))
79 artist
= tags
.get_path_safe('ARTIST')
80 album
= tags
.get_path_safe('ALBUM')
86 album_path
= artist
+ '/' + album
92 for i
, old_fn
in enumerate(args
):
94 fn
= '%s/%s/%s.flac' % (artist
, album
, tags
.make_filename(track
))
96 print tags
.track(track
)
97 #os.rename(old_fn, fn)
102 if args
[1] == 'read':
103 return do_read(args
[2:])
104 if args
[1] == 'write':
105 return do_write(args
[2:])
111 if __name__
== '__main__':
112 sys
.exit(main(sys
.argv
))