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 

21import logging 

22from colorama import Fore, Style 

23from puffotter.os import replace_illegal_ntfs_chars 

24 

25 

26class RenameOperation: 

27 """ 

28 Class that models a renaming operation 

29 """ 

30 

31 def __init__(self, source_path: str, new_name: str): 

32 """ 

33 Initializes the RenameOperation object 

34 :param source_path: The currently existing path to the file/directory 

35 :param new_name: The new name of the file/directory 

36 """ 

37 self.logger = logging.getLogger(self.__class__.__name__) 

38 self.parent = os.path.dirname(source_path) 

39 self.source = source_path 

40 sanitized = self.sanitize(self.parent, new_name) 

41 self.dest = os.path.join(self.parent, sanitized) 

42 

43 def rename(self): 

44 """ 

45 Renames the episode file to the new name 

46 :return: None 

47 """ 

48 if self.source == self.dest: 

49 return 

50 

51 while os.path.exists(self.dest): 

52 self.logger.warning( 

53 "{}Destination file '{}' already exists!{}".format( 

54 Fore.LIGHTRED_EX, self.dest, Style.RESET_ALL 

55 ) 

56 ) 

57 name, ext = self.dest.rsplit(".", 1) 

58 name += "_" 

59 self.dest = "{}.{}".format(name, ext) 

60 

61 print("Renaming: {}".format(self)) 

62 os.rename(self.source, self.dest) 

63 

64 def __str__(self) -> str: 

65 """ 

66 :return: A string representation of the operation 

67 """ 

68 start = "" 

69 end = "" 

70 if self.source != self.dest: 

71 start = Fore.LIGHTYELLOW_EX 

72 end = Style.RESET_ALL 

73 

74 return "{}{} ---> {}{}".format(start, self.source, self.dest, end) 

75 

76 @staticmethod 

77 def sanitize(parent: str, filename: str) -> str: 

78 """ 

79 Replaces all illegal file system characters with valid ones. 

80 Also, limits the length of the resulting file path to 250 characters, 

81 if at all possible 

82 :param parent: The parent directory 

83 :param filename: The filename to sanitize 

84 :return: The sanitized string 

85 """ 

86 sanitized = replace_illegal_ntfs_chars(filename) 

87 

88 try: 

89 name, ext = sanitized.rsplit(".", 1) 

90 ext = "." + ext 

91 except (IndexError, ValueError): 

92 name, ext = [sanitized, ""] 

93 

94 if len(sanitized) > 250 > len(parent) + len(ext): 

95 max_file_length = 250 - (len(parent) + len(ext)) 

96 name = name[0:max_file_length] 

97 

98 return name + ext