[Tutor] How to map different keys together ?
spir ☣
denis.spir at gmail.com
Sun Apr 18 20:17:45 CEST 2010
On Sun, 18 Apr 2010 13:42:46 -0400
Damon Timm <damontimm at gmail.com> wrote:
> Hi Alan, et al - thanks for your response and your ideas. I sat down
> and did a little more coding so that I might tackle what I can and
> bring back where I am having trouble. I have implemented the basic
> 'tag_map' you suggested without a hitch using my own class and
> getitem/setitem builtins. Right now, if there is a one-to-one
> correlation between my *generic* key (as a standard between my music
> files) and the specific tag for the filetype, I am doing well.
>
> However, everything is not so clear cut in the world of metatagging!
> My first stumbling block is that M4A files store the track number and
> track total in a single tuple ... but I need them as separate fields
> (which is how some of the other formats do it). This is going to be
> one of many hurdles -- I need a way to accomplish more than a
> one-to-one data map.
>
> See my code, below, as well as some command line examples where I am
> having trouble. I feel there may be a way to pass functions through
> my tag_map dictionary (maybe a lambda?!) but I can't get my head
> around what approach is best (I can't think of any approach, right
> now, actually).
>
> Code follows. Thanks again.
>
> class Tags(object):
> '''Wrapper class for a mutagen music file object.'''
> tag_map = {}
>
> def __init__(self, mutagen):
> self._mutagen = mutagen
> self.tags = {}
>
> def keys(self):
> '''Get list of generic tag keys in use'''
> keys = []
> for k in self.tag_map.keys():
> try:
> self._mutagen[self.tag_map[k]]
> keys.append(k)
> except KeyError:
> pass
>
> return keys
>
> def save(self):
> '''Save the mutagen changes.'''
> self._mutagen.save()
>
> class MP4Tags(Tags):
> tag_map = {
> # GENERIC : SPECIFIC
> 'title' : '\xa9nam',
> 'album' : '\xa9alb',
> 'artist' : '\xa9ART',
> 'albumartist' : 'aART',
> 'comment' : '\xa9cmt',
> 'compilation' : 'cpil',
> 'composer' : '\xa9wrt',
> 'genre' : '\xa9gen',
> 'discnumber' : 'disk', # returns: (2,10) need lmbda or something ?!
> 'disctotal' : 'disk', # returns: (2,10) need lmbda or something ?!
> 'year' : '\xa9day',
> 'tracknumber' : 'trkn', # returns: (2,10) need lmbda or something ?!
> 'tracktotal' : 'trkn' # returns: (2,10) need lmbda or something ?!
>
> }
>
> def __getitem__(self, key):
> try:
> return self._mutagen[self.tag_map[key]]
> except KeyError:
> pass
>
> def __setitem__(self, key, value):
> self._mutagen[self.tag_map[key]] = value
>
> #EOF
All of this is not only plain data, but constant! The case of non-exisitng tag for a given format is, as shown by your 'pass', not to be handled here. Using a class is not only averkill but (wrong in my opininon and) misleading.
Anyway it won't work since you need more than simple lookup in the case above, meaning some process must be done, and also the case of mp3 (iirc) titles shown in your first post.
> **Here is how it works:
>
> >>> import tagging
> >>> from mutagen.mp4 import MP4
> >>> mp4 = MP4('../tests/data/Compressed/M4A-256.m4a')
> >>> mp4_tags = tagging.MP4Tags(mp4)
> >>> mp4_tags['title']
> [u'bob the builder'] # woo hoo! it works!
> >>> mp4_tags['title'] = [u'I can change the title!']
> >>> mp4_tags['title']
> [u'I can change the title!'] # changing the titles works too
> >>> mp4_tags['discnumber']
> [(1, 1)] # TODO - I need to return disk[0][0] ... not the tuple
> >>> mp4_tags.save()
>
> So, I need to modify how the data is shown to me as well as how I
> would go about writing the data something like:
>
> return_tag(disk): return disk[0][0]
> save_tag(num): return [(%s, %s)] % ( num,
> somehow_get_the_original_second_value_before_re_saving)
>
> Thanks again and any advice or guidance about how to approach this is
> greatly appreciated.
>
> Damon
________________________________
vit esse estrany ☣
spir.wikidot.com
More information about the Tutor
mailing list