audio: Add helper for converting taglists
Goal is simply to avoid leaking gst types to the rest of mopidy. Only part we will be leaking is the tag keys. Which we can live with.
This commit is contained in:
parent
2c3217685b
commit
f477e9176e
@ -1,11 +1,17 @@
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
import datetime
|
||||
import logging
|
||||
import numbers
|
||||
|
||||
import pygst
|
||||
pygst.require('0.10')
|
||||
import gst # noqa
|
||||
|
||||
from mopidy import compat
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def calculate_duration(num_samples, sample_rate):
|
||||
"""Determine duration of samples using GStreamer helper for precise
|
||||
@ -56,3 +62,47 @@ def supported_uri_schemes(uri_schemes):
|
||||
supported_schemes.add(uri)
|
||||
|
||||
return supported_schemes
|
||||
|
||||
|
||||
def convert_taglist(taglist):
|
||||
"""Convert a :class:`gst.Taglist` to plain python types.
|
||||
|
||||
Knows how to convert:
|
||||
- Dates
|
||||
- Buffers
|
||||
- Numbers
|
||||
- Strings
|
||||
- Booleans
|
||||
|
||||
Unknown types will be ignored and debug logged. Tag keys are all strings
|
||||
defined by GStreamer.
|
||||
|
||||
:param :class:`gst.Taglist` taglist: A GStreamer taglist to be converted.
|
||||
:rtype: dictionary of tag keys with a list of values.
|
||||
"""
|
||||
result = {}
|
||||
|
||||
# Taglists are not really dicts, hence the lack of .items() and
|
||||
# explicit use of .keys()
|
||||
for key in taglist.keys():
|
||||
result.setdefault(key, [])
|
||||
|
||||
values = taglist[key]
|
||||
if not isinstance(values, list):
|
||||
values = [values]
|
||||
|
||||
for value in values:
|
||||
if isinstance(value, gst.Date):
|
||||
try:
|
||||
date = datetime.date(value.year, value.month, value.day)
|
||||
result[key].append(date)
|
||||
except ValueError:
|
||||
logger.debug('Ignoring invalid date: %r = %r', key, value)
|
||||
elif isinstance(value, gst.Buffer):
|
||||
result[key].append(bytes(value))
|
||||
elif isinstance(value, (basestring, bool, numbers.Number)):
|
||||
result[key].append(value)
|
||||
else:
|
||||
logger.debug('Ignoring unknown data: %r = %r', key, value)
|
||||
|
||||
return result
|
||||
|
||||
Loading…
Reference in New Issue
Block a user