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"""LICENSE 

2Copyright 2015 Hermann Krumrey <hermann@krumreyh.com> 

3 

4This file is part of toktokkie. 

5 

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. 

10 

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. 

15 

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

17along with toktokkie. If not, see <http://www.gnu.org/licenses/>. 

18LICENSE""" 

19 

20import os 

21from typing import Dict, List, Any 

22from toktokkie.exceptions import InvalidMetadata 

23from toktokkie.enums import IdType 

24from toktokkie.metadata.base.IdHelper import IdHelper 

25from toktokkie.metadata.base.components.JsonComponent import JsonComponent 

26 

27 

28class BookVolume(JsonComponent): 

29 """ 

30 Class that models a Volume in a Book Series 

31 """ 

32 

33 def __init__( 

34 self, 

35 volume_number: int, 

36 path: str, 

37 parent_ids: Dict[IdType, List[str]], 

38 ids: Dict[IdType, List[str]] 

39 ): 

40 """ 

41 Initializes the Book Volume 

42 :param volume_number: The volume number 

43 :param path: The path to the volume file 

44 :param parent_ids: The IDs of the parent BookSeries object 

45 :param ids: The specific IDs for this book volume 

46 """ 

47 self.number = volume_number 

48 self.path = path 

49 self.ids = IdHelper.fill_ids(ids, [], parent_ids) 

50 self.parent_ids = parent_ids 

51 

52 @property 

53 def name(self) -> str: 

54 """ 

55 The name of the volume 

56 :return: None 

57 """ 

58 return os.path.basename(self.path).rsplit(".", 1)[0] 

59 

60 @property 

61 def json(self) -> Dict[str, Any]: 

62 """ 

63 :return: A JSON-compatible dictionary representing this object 

64 """ 

65 return { 

66 "ids": IdHelper.stringify_ids( 

67 IdHelper.minimize_ids(self.ids, self.parent_ids) 

68 ) 

69 } 

70 

71 @classmethod 

72 def from_json( 

73 cls, 

74 volume_number: int, 

75 path: str, 

76 parent_ids: Dict[IdType, List[str]], 

77 json_data: Dict[str, Dict[str, List[str]]] 

78 ) -> "BookVolume": 

79 """ 

80 Generates a BookVolume object based on json data 

81 :param volume_number: The volume number 

82 :param path: The path to the volume file 

83 :param parent_ids: The IDs of the parent metadata 

84 :param json_data: The JSON data 

85 :return: None 

86 :raises InvalidMetadataException: If the provided JSON is invalid 

87 """ 

88 try: 

89 return cls( 

90 volume_number, 

91 path, 

92 parent_ids, 

93 IdHelper.objectify_ids(json_data["ids"]) 

94 ) 

95 except KeyError as e: 

96 raise InvalidMetadata(f"Attribute missing: {e}")