Hide keyboard shortcuts

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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

"""LICENSE 

Copyright 2018 Hermann Krumrey <hermann@krumreyh.com> 

 

This file is part of anime-list-apis. 

 

anime-list-apis is free software: you can redistribute it and/or modify 

it under the terms of the GNU General Public License as published by 

the Free Software Foundation, either version 3 of the License, or 

(at your option) any later version. 

 

anime-list-apis is distributed in the hope that it will be useful, 

but WITHOUT ANY WARRANTY; without even the implied warranty of 

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

GNU General Public License for more details. 

 

You should have received a copy of the GNU General Public License 

along with anime-list-apis. If not, see <http://www.gnu.org/licenses/>. 

LICENSE""" 

 

from enum import Enum 

from typing import Dict, List, Tuple, Set, Optional 

from anime_list_apis.models.Serializable import Serializable 

 

 

class IdType(Enum): 

""" 

Enumeration of different ID types 

""" 

MYANIMELIST = 1 

ANILIST = 2 

KITSU = 3 

 

 

class Id(Serializable): 

""" 

Class that models an ID. Has the capability to store different ID types 

""" 

 

def __init__(self, ids: Dict[IdType, int]): 

""" 

Initializes the ID. The IDs are set using a dictionary mapping 

IDs to the different ID types. At least one entry is required. 

Missing IDs will be replaced with None 

:param ids: The IDs mapped to an IdType 

:raises TypeError: If an invalid parameter type was provided 

:raises ValueError: In case no valid ID was provided 

""" 

self.ensure_type(ids, dict) 

ids = { 

key: value for key, value in ids.items() 

if self.type_check(value, int) 

} 

if len(ids) == 0: 

raise ValueError("At least one ID required") 

 

self.__ids = ids 

for id_type in IdType: 

if id_type not in self.__ids: 

# noinspection PyTypeChecker 

self.__ids[id_type] = None 

 

def get(self, id_type: IdType) -> Optional[int]: 

""" 

Retrieves an ID for a given ID type 

:param id_type: The ID type to get 

:return: The ID. If it does not exist, return None 

""" 

self.ensure_type(id_type, IdType) 

return self.__ids[id_type] 

 

def set(self, _id: int, id_type: IdType): 

""" 

Sets an ID for a given ID type 

:param _id: The ID to set 

:param id_type: The type of ID for which to set the ID 

:return: None 

:raises TypeError: If the provided ID is not an integer 

""" 

self.ensure_type(_id, int) 

self.__ids[id_type] = _id 

 

def _serialize(self) -> Dict[str, Optional[str or int or float or bool 

or Dict or List or Tuple or Set]]: 

""" 

Serializes the object into a dictionary 

:return: The serialized form of this object 

""" 

data = {} 

for key, value in self.__ids.items(): 

data[key.name] = value 

return data 

 

@classmethod 

def _deserialize(cls, data: Dict[str, Optional[str or int or float or bool 

or Dict or List or Tuple or Set]]): 

""" 

Deserializes a dictionary into an object of this type 

:param data: The data to deserialize 

:return: The deserialized object 

:raises TypeError: If a type error occurred 

:raises ValueError: If the data could not be deserialized 

""" 

des = {} 

for key, value in data.items(): 

des[IdType[key]] = value 

generated = cls(des) # type: Id 

return generated 

 

def __eq__(self, other: object) -> bool: 

""" 

Checks for equality with another object. Needed to make IDs hashable 

:param other: The other object 

:return: True if equal, False otherwise 

""" 

if not isinstance(other, type(self)): 

return False 

else: 

return self.serialize() == other.serialize() 

 

def __hash__(self) -> int: 

""" 

Generates a hash so that IDs can be used as keys in dictionaries 

:return: The hash 

""" 

return hash(str(self))