← Back to team overview

pytagsfs team mailing list archive

Re: multivalued tags

 

Hi Raphaël,

On Sun, May 22, 2011 at 06:50:57PM +0200, Raphaël wrote:
> AFAIK, mutagen_.py and maildir.py rely on the following scheme:
> - multiple tags are possible
> - in the case of mutagen, tag's value is usable
> - in the case of maildir, tags do not have value
> 
> In the case of xattr, one may perfectly want a multivalued field.
> Eg, you want to tag a video with:
> 'subjects' = [ 'holiday', 'family' ]
> 
> instead of:
> 'subject1' = 'holiday'
> 'subject2' = 'family'
> 
> I still fail to see how I *should* properly implement base classes, but
> here is what I currently do:
> 
> In my		class _XattrMetaStore(MetaStore),
> the 		def get(self, path)
> contains:
> 	values1 = dict ( xattr.get_all(path, namespace=xattr.NS_USER) )
> 	values2 = Values.from_flat_dict ( values1 )
> 
> The problem of separating multivalued tags lies in:
> class Values(dict):
> 	def from_flat_dict(cls, d):
>             if v is None:
>                 result[k] = []
>             else:
>                 result[k] = [v]
> 
> 
> that I would like to change to:
>             if v is None:
>                 result[k] = []
>             elif isinstance(v, (list, tuple)):
> 	            result[k] = v
>             else:
>                 result[k] = [v]
> 
> (patch attached)
> 
> Then, back in _XattrMetaStore:get(self, path),
>       between "values1 = ..." and "values2 = ...",
> I can use:
>    for k, v in values1.iteritems():
>             if re.search(",", v):
> 	       values1[k] = v.split(",")	# hardcoded xattr split scheme
> 
> without having the value nested twice in a list.
> 
> 
> what do you think about this ?

The patch is not necessary.  If you want multiple values per key, you can just
do this:

  d = dict(xattr.get_all(path, namespace=xattr.NS_USER))
  for k in d:
      d[k] = d[k].split(',')
  values = Values(d)

Values.from_flat_dict is a constructor that assumes a single value for each key,
which is why it forces the result to have values that are lists of zero or one
item, i.e.:

  Values.from_flat_dict({'foo': 'bar'}) -> Values({'foo': ['bar']})

Hope this helps.

Thanks,
Forest
-- 
Forest Bond
rapidrollout.com / alittletooquiet.net / pytagsfs.org

Attachment: signature.asc
Description: Digital signature


Follow ups

References