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.metadata.tv.components.TvEpisode import TvEpisode
22from toktokkie.metadata.base.components.JsonComponent import JsonComponent
23from toktokkie.exceptions import InvalidMetadata
26class TvEpisodeRange(JsonComponent):
27 """
28 Class that models a TV Episode Range
29 """
31 def __init__(self, season: int, start_episode: int, end_episode: int):
32 """
33 Initializes the TvEpisodeRange object
34 :param season: The season
35 :param start_episode: The first episode in the range
36 :param end_episode: The last episode in the range
37 """
38 self.season = season
39 self.start_episode = start_episode
40 self.end_episode = end_episode
42 @property
43 def episodes(self) -> List[TvEpisode]:
44 """
45 :return: A list of episodes included in this episode range
46 """
47 episodes = []
48 min_episode = min(self.start_episode, self.end_episode)
49 max_episode = max(self.start_episode, self.end_episode)
51 for episode in range(min_episode, max_episode + 1):
52 episodes.append(TvEpisode(self.season, episode))
54 return episodes
56 @property
57 def json(self) -> Dict[str, Any]:
58 """
59 :return: A JSON-compatible dictionary representing the object
60 """
61 return {
62 "season": self.season,
63 "start_episode": self.start_episode,
64 "end_episode": self.end_episode
65 }
67 @classmethod
68 def from_json(cls, json_data: Dict[str, Any]) -> "TvEpisodeRange":
69 """
70 Generates a TvEpisodeRange object based on json data
71 :param json_data: The JSON data
72 :return: The generated TvEpisodeRange object
73 :raises InvalidMetadataException: If the provided JSON is invalid
74 """
75 try:
76 return cls(
77 json_data["season"],
78 json_data["start_episode"],
79 json_data["end_episode"]
80 )
81 except (KeyError, TypeError) as e:
82 raise InvalidMetadata(f"Attribute Missing/Invalid: {e}")