def to_text(value, encoding=None): """Convert the given text or binary type to text using the given encoding or the default encoding for the Avalon Music Server (UTF-8). :data:`None` values will be converted to an empty text string, text values will be left alone, and binary values will be converted using the specified or default encoding. All other types will result in a :class:`TypeError` being raised :param value: Possible text or binary value :param basestring encoding: Optional encoding to use for converting from bytes to a text type :return: The value converted to text :raises TypeError: If the type of the value isn't supported. """ encoding = avalon.DEFAULT_ENCODING if encoding is None else encoding if value is None: return six.text_type() if isinstance(value, six.text_type): return value if isinstance(value, six.binary_type): return value.decode(encoding) raise TypeError("Cannot convert type {0} to text".format(type(value)))
def _get_one(self, track): return avalon.tags.read.Metadata( path=six.text_type(uuid.uuid4()), album=random.choice(self._albums), artist=random.choice(self._artists), genre=random.choice(self._genres), title=track, track=random.randint(1, 30), year=random.randint(1970, 2038), length=random.randint(10, 500))
def test_get_songs_by_genre_id(self, track_elms, service_config, request): """Test that we can fetch tracks by genre UUID.""" genre_id = uuid.UUID(avalon.compat.to_uuid_input('26ce4d6b-af97-45a6-b7f6-d5c1cbbfd6b1')) service_config.track_store.get_by_genre.return_value = track_elms request.args['genre_id'] = six.text_type(genre_id) params = avalon.web.request.Parameters(request) service = avalon.web.services.AvalonMetadataService(service_config) results = service.get_songs(params) assert results == track_elms, 'Expected matching tracks returned' service_config.track_store.get_by_genre.assert_called_with(genre_id)
def test_get_songs_by_artist_id(self, track_elms, service_config, request): """Test that we can fetch tracks by artist UUID.""" artist_id = uuid.UUID(avalon.compat.to_uuid_input('75d590d1-9f3d-462d-8264-0d16af227860')) service_config.track_store.get_by_artist.return_value = track_elms request.args['artist_id'] = six.text_type(artist_id) params = avalon.web.request.Parameters(request) service = avalon.web.services.AvalonMetadataService(service_config) results = service.get_songs(params) assert results == track_elms, 'Expected matching tracks returned' service_config.track_store.get_by_artist.assert_called_with(artist_id)
def test_get_songs_by_album_id(self, track_elms, service_config, request): """Test that we can fetch tracks by album UUID.""" album_id = uuid.UUID(avalon.compat.to_uuid_input('37cac253-2bca-4a3a-be9f-2ac655e04ad8')) service_config.track_store.get_by_album.return_value = track_elms request.args['album_id'] = six.text_type(album_id) params = avalon.web.request.Parameters(request) service = avalon.web.services.AvalonMetadataService(service_config) results = service.get_songs(params) assert results == track_elms, 'Expected matching tracks returned' service_config.track_store.get_by_album.assert_called_with(album_id)
def default(self, o): if isinstance(o, uuid.UUID): return six.text_type(o) if isinstance(o, ApiErrorCode): return o.to_dict() return super(AvalonJsonEncoder, self).default(o)