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 abc import ABC 

22from typing import List 

23from puffotter.os import listdir 

24from toktokkie.enums import IdType 

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

26from toktokkie.metadata.base.Renamer import Renamer 

27from toktokkie.metadata.comic.ComicExtras import ComicExtras 

28 

29 

30class ComicRenamer(Renamer, ComicExtras, ABC): 

31 """ 

32 Implements the Renamer functionality for comic metadata 

33 """ 

34 

35 def create_rename_operations(self) -> List[RenameOperation]: 

36 """ 

37 Creates renaming operations for book series metadata 

38 :return: The renaming operations 

39 """ 

40 main_content = listdir(self.main_path, no_dirs=True) 

41 max_chapter_length = len(str(len(main_content))) 

42 

43 operations = [] 

44 

45 for i, (old_name, old_path) in enumerate(main_content): 

46 ext = old_name.rsplit(".", 1)[1] 

47 new_name = "{} - Chapter {}.{}".format( 

48 self.name, 

49 str(i + 1).zfill(max_chapter_length), 

50 ext 

51 ) 

52 operations.append(RenameOperation(old_path, new_name)) 

53 

54 if not os.path.isdir(self.special_path): 

55 return operations 

56 

57 special_content = listdir(self.special_path, no_dirs=True) 

58 

59 if len(special_content) != len(self.special_chapters): 

60 self.logger.warning( 

61 "Invalid amount of special chapters!!! {} != {}".format( 

62 len(special_content), len(self.special_chapters) 

63 ) 

64 ) 

65 return operations 

66 else: 

67 special_max_length = len(max( 

68 self.special_chapters, 

69 key=lambda x: len(x) 

70 )) 

71 for i, (old_name, old_path) in enumerate(special_content): 

72 chap_guess, ext = old_name.rsplit(".", 1) 

73 

74 chapter_num = self.special_chapters[i] 

75 

76 # Tries to infer the chapter number from local files. 

77 # Useful if a newly added chapter does not fit correctly 

78 # in the lexicological order 

79 try: 

80 chap_guess = chap_guess.rsplit(" - Chapter ", 1)[1] 

81 while chap_guess.startswith("0"): 

82 chap_guess = chap_guess[1:] 

83 if chap_guess in self.special_chapters: 

84 chapter_num = chap_guess 

85 

86 except IndexError: 

87 pass 

88 

89 new_name = "{} - Chapter {}.{}".format( 

90 self.name, 

91 chapter_num.zfill(special_max_length), 

92 ext 

93 ) 

94 operations.append(RenameOperation(old_path, new_name)) 

95 

96 return operations 

97 

98 def resolve_title_name(self) -> str: 

99 """ 

100 If possible, will fetch the appropriate name for the 

101 metadata based on IDs, falling back to the 

102 directory name if this is not possible or supported. 

103 """ 

104 return self.load_title_and_year([ 

105 IdType.ANILIST 

106 ])[0]