Exemplo n.º 1
0
 def from_serialized(cls, obj):
     if isinstance(obj, cls):
         return obj
     elif isinstance(obj, dict):
         # Parses the song id.
         pid = int(obj['id']) if 'id' in obj else None
         # Parses the name of the profile.
         name = obj['name'] if 'name' in obj else None
         # Parses the id of the jamendo profile.
         jamendo_id = int(
             obj['jamendo_id']) if 'jamendo_id' in obj else None
         if jamendo_id is None:
             raise DeserializableException(
                 'The jamendo id of the song profile must not be None.')
         # Parses the external link of the profile.
         external_link = obj[
             'external_link'] if 'external_link' in obj else None
         # Parses the cover link of the profile.
         cover_link = obj['cover'] if 'cover' in obj else None
         return JamendoSongProfile(id=pid,
                                   jamendo_id=jamendo_id,
                                   name=name,
                                   external_link=external_link,
                                   cover=cover_link)
     else:
         raise DeserializableException(
             'The given object %s can\'t be parsed. It is no dictionary or set (%s).'
             % (repr(obj), type(obj)))
Exemplo n.º 2
0
 def from_serialized(cls, obj):
     if isinstance(obj, cls):
         return obj
     elif isinstance(obj, dict):
         aid = int(
             obj['id']) if 'id' in obj and obj['id'] is not None else None
         # Parses the jamendo id of the album profile.
         jamendo_id = obj['jamendo_id'] if 'jamendo_id' in obj else None
         if jamendo_id is None:
             raise DeserializableException(
                 'The name of the album profile must not be None.')
         # Parses the name, cover, external link of the album profile.
         name = obj['name'] if 'name' in obj else None
         cover = obj['cover'] if 'cover' in obj else None
         external_link = obj[
             'external_link'] if 'external_link' in obj else None
         return JamendoAlbumProfile(id=aid,
                                    jamendo_id=jamendo_id,
                                    name=name,
                                    cover=cover,
                                    external_link=external_link)
     else:
         raise DeserializableException(
             'The given object %s can\'t be parsed. It is no dictionary (%s).'
             % (repr(obj), type(obj)))
Exemplo n.º 3
0
 def from_serialized(cls, obj):
     if isinstance(obj, cls):
         return obj
     elif isinstance(obj, dict):
         aid = (int(obj['id'])
                if 'id' in obj and obj['id'] is not None else None)
         # Parses the name of the artist.
         name = obj['name'] if 'name' in obj else None
         if name is None:
             raise DeserializableException(
                 'The name of the artist must not be None.')
         # Parses the abstract of the artist.
         abstract = obj['abstract'] if 'abstract' in obj else None
         # Parses the location of the artist.
         website = obj['website'] if 'website' in obj else None
         city = obj['city'] if 'city' in obj else None
         country_code = obj[
             'country_code'] if 'country_code' in obj else None
         # Parses the jamendo profile.
         jamendo_profile = None
         if 'jamendo_profile' in obj and obj['jamendo_profile'] is not None:
             jamendo_profile = JamendoArtistProfile.from_serialized(
                 obj['jamendo_profile'])
         return cls(id=aid,
                    name=name,
                    abstract=abstract,
                    city=city,
                    country_code=country_code,
                    website=website,
                    jamendo_profile=jamendo_profile)
     else:
         raise DeserializableException(
             'The given object %s can\'t be parsed. It is no dictionary (%s).'
             % (repr(obj), type(obj)))
Exemplo n.º 4
0
 def from_serialized(cls, obj):
     if isinstance(obj, cls):
         return obj
     elif isinstance(obj, dict):
         # Parses the song id.
         sid = (int(obj['id']) if 'id' in obj else None)
         # Parses the name of the song.
         name = obj['name'] if 'name' in obj else None
         if name is None:
             raise DeserializableException(
                 'The name of the song must not be None.')
         # Parses the artist of the song.
         artist = Artist.from_serialized(
             obj['artist']
         ) if 'artist' in obj and obj['artist'] is not None else None
         # Parses the album of the song.
         album = Album.from_serialized(
             obj['album']
         ) if 'album' in obj and obj['album'] is not None else None
         # Parses the duration of the song.
         duration = int(
             obj['duration']
         ) if 'duration' in obj and obj['duration'] is not None else None
         # Parses the release date of the song.
         release_date = None
         if 'release_date' in obj and obj['release_date']:
             release_date = obj['release_date']
             if isinstance(release_date, str):
                 release_date = datetime.strptime(release_date,
                                                  '%Y-%m-%dT%H:%M:%S')
             elif not isinstance(release_date, datetime):
                 raise DeserializableException(
                     'The given release date can\'t be parsed.')
         # Parses the cover of the song.
         cover = obj['cover'] if 'cover' in obj else None
         # Parses the jamendo profile of the song.
         jamendo_profile = JamendoSongProfile.from_serialized(
             obj['jamendo_profile']) if 'jamendo_profile' in obj and obj[
                 'jamendo_profile'] is not None else None
         # Encapsulate the song.
         song = cls(id=sid,
                    name=name,
                    artist=artist,
                    album=album,
                    cover=cover,
                    duration=duration,
                    release_date=release_date,
                    jamendo_profile=jamendo_profile)
         # Parses the tags of the song.
         if 'tags' in obj and obj['tags']:
             song.tags.add(
                 *[Tag.from_serialized(entry) for entry in obj['tags']])
         return song
     else:
         raise DeserializableException(
             'The given object %s can\'t be parsed. It is no dictionary (%s).'
             % (repr(obj), type(obj)))
Exemplo n.º 5
0
 def from_serialized(cls, obj):
     if isinstance(obj, cls):
         return obj
     elif isinstance(obj, dict):
         # Parses the id of the album.
         aid = (int(obj['id'])
                if 'id' in obj and obj['id'] is not None else None)
         # Parses the name of the album.
         name = obj['name'] if 'name' in obj else None
         if name is None:
             raise DeserializableException(
                 'The name of the album must not be None.')
         # Parses the cover of the album.
         cover = obj['cover'] if 'cover' in obj else None
         # Parses the artist of the album.
         artist = (Artist.from_serialized(obj['artist'])
                   if obj['artist'] else None)
         # Parses the release date of the album.
         release_date = obj[
             'release_date'] if 'release_date' in obj else None
         if release_date is not None:
             if isinstance(release_date, str):
                 try:
                     release_date = datetime.strptime(
                         obj['release_date'], '%Y-%m-%dT%H:%M:%S')
                 except Exception as e:
                     raise DeserializableException(
                         'The given release date can\'t be parsed. (%s)' %
                         e.args[0])
             elif not isinstance(release_date, datetime):
                 raise DeserializableException(
                     'The given release date can\'t be parsed. %s unsupported.'
                     % type(release_date))
         # Parses the jamendo profile of the album.
         jamendo_profile = None
         if 'jamendo_profile' in obj and obj['jamendo_profile'] is not None:
             jamendo_profile = JamendoAlbumProfile.from_serialized(
                 obj['jamendo_profile'])
         return cls(id=aid,
                    name=name,
                    artist=artist,
                    cover=cover,
                    release_date=release_date,
                    jamendo_profile=jamendo_profile)
     else:
         raise DeserializableException(
             'The given object %s can\'t be parsed. It is no dictionary (%s).'
             % (repr(obj), type(obj)))
Exemplo n.º 6
0
 def from_serialized(cls, obj):
     if isinstance(obj, cls):
         return obj
     elif isinstance(obj, dict):
         lid = int(obj['id']) if 'id' in obj and obj['id'] else None
         type = obj['type'] if 'type' in obj else None
         if type is None and type not in [
                 entry[0] for entry in cls.LICENSE_TYPE
         ]:
             raise DeserializableException(
                 'The given type \'%s\' of license is unknown. It must not be None.'
                 % type)
         return cls(id=lid, type=type)
     else:
         raise DeserializableException(
             'The given object %s can\'t be parsed. It is no dictionary (%s).'
             % (repr(obj), type(obj)))
Exemplo n.º 7
0
 def from_serialized(cls, obj):
     if isinstance(obj, cls):
         return obj
     elif isinstance(obj, dict):
         tid = (int(obj['id'])
                if 'id' in obj and obj['id'] is not None else None)
         name = obj['name'] if 'name' in obj else None
         return cls(id=tid, name=name)
     else:
         raise DeserializableException(
             'The given object %s can\'t be parsed. It is no dictionary or set (%s).'
             % (repr(obj), type(obj)))
Exemplo n.º 8
0
 def from_serialized(cls, obj):
     if isinstance(obj, cls):
         return obj
     elif isinstance(obj, dict):
         sid = (int(obj['id']) if 'id' in obj and obj['id'] else None)
         # Parses the type, codec and link of the source.
         type = obj['type'] if 'type' in obj else None
         if type is None and type not in [
                 entry[0] for entry in cls.SOURCE_TYPE
         ]:
             raise DeserializableException(
                 'The type \'%s\' of the source is unknown. It must not be None.'
                 % type)
         link = obj['link'] if 'link' in obj else None
         if link is None:
             raise DeserializableException(
                 'The link \'%s\' of the source must not be None.' % link)
         codec = obj['codec'] if 'codec' in obj else None
         if codec is None and codec not in [
                 entry[0] for entry in cls.CODEC_TYPE
         ]:
             raise DeserializableException(
                 'The codec \'%s\'of the source is unknown. It must not be None.'
                 % codec)
         # Parses the song of the source.
         song = None
         if 'song_id' in obj and obj['song_id']:
             song_query_set = Song.objects.filter(id=int(obj['song_id']))
             song = (song_query_set.first()
                     if song_query_set.exists() else None)
         if song is None:
             raise DeserializableException(
                 'The song \'%s\'of the source must not be None.' %
                 str(song))
         return cls(id=sid, type=type, link=link, song=song, codec=codec)
     else:
         raise DeserializableException(
             'The given object %s can\'t be parsed. It is no dictionary (%s).'
             % (repr(obj), type(obj)))
Exemplo n.º 9
0
 def from_serialized(cls, obj):
     if isinstance(obj, cls):
         return obj
     elif isinstance(obj, (dict, set)):
         try:
             execution_date = obj['execution_date']
             if isinstance(execution_date, str):
                 execution_date = datetime.strptime(obj['execution_date'],
                                                    '%Y-%m-%dT%H:%M:%S')
             elif not isinstance(execution_date, datetime):
                 raise DeserializableException(
                     'The given release date can\'t be parsed.')
             return cls(service=obj['service'],
                        execution_date=execution_date,
                        status=obj['status'],
                        exception=obj['exception'])
         except KeyError as e:
             raise DeserializableException(
                 'The given serialized representation is corrupted.') from e
     else:
         raise DeserializableException(
             'The given object %s can\'t be parsed. It is no dictionary or set (%s).'
             % (repr(obj), type(obj)))