Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""LICENSE
2Copyright 2015 Hermann Krumrey <hermann@krumreyh.com>
4This file is part of toktokkie.
6toktokkie is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
11toktokkie is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
16You should have received a copy of the GNU General Public License
17along with toktokkie. If not, see <http://www.gnu.org/licenses/>.
18LICENSE"""
20from typing import Dict, Any, List
21from toktokkie.exceptions import InvalidMetadata
22from toktokkie.enums import IdType
23from toktokkie.metadata.base.IdHelper import IdHelper
24from toktokkie.metadata.base.components.JsonComponent import JsonComponent
25from toktokkie.metadata.music.components.MusicAlbum import MusicAlbum
28class MusicThemeSong(JsonComponent):
29 """
30 Class that collects data on music theme songs (like anime openings etc)
31 """
33 def __init__(
34 self,
35 album: MusicAlbum,
36 name: str,
37 theme_type: str,
38 series_ids: Dict[IdType, List[str]]
39 ):
40 """
41 Initializes the object
42 :param album: The album object related to this theme song
43 :param name: The name of the song
44 :param theme_type: The type of theme song
45 :param series_ids: The IDs of the series this is a theme song for
46 """
47 self.album = album
48 self.name = name
49 self._theme_type = theme_type
51 if self.name != self.album.name:
52 self.logger.warning("Theme song {} does not match album {}"
53 .format(self.name, self.album.name))
54 self.series_ids = IdHelper.fill_ids(
55 series_ids, IdHelper.theme_song_id_types()
56 )
58 @property
59 def theme_type(self) -> str:
60 """
61 :return: The theme type
62 """
63 return self._theme_type.upper()
65 @property
66 def json(self) -> Dict[str, Any]:
67 """
68 Converts the component into a JSON-compatible dictionary
69 :return: The JSON-compatible dictionary
70 """
71 return {
72 "name": self.name,
73 "series_ids": IdHelper.stringify_ids(IdHelper.minimize_ids(
74 self.series_ids
75 )),
76 "theme_type": self.theme_type.lower()
77 }
79 @classmethod
80 def from_json(
81 cls,
82 album: MusicAlbum,
83 json_data: Dict[str, Any]
84 ) -> "MusicThemeSong":
85 """
86 Generates a new MusicThemeSong object based on JSON data
87 :param album: The corresponding album object
88 :param json_data: The JSON data
89 :return: The generated object
90 :raises InvalidMetadataException: If the provided JSON is invalid
91 """
92 try:
93 return cls(
94 album,
95 json_data["name"],
96 json_data["theme_type"],
97 IdHelper.objectify_ids(json_data["series_ids"])
98 )
99 except KeyError as e:
100 raise InvalidMetadata(f"Attribute missing: {e}")