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 2019 Hermann Krumrey <hermann@krumreyh.com> 

3 

4This file is part of puffotter. 

5 

6puffotter 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 

11puffotter 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 puffotter. If not, see <http://www.gnu.org/licenses/>. 

18LICENSE""" 

19 

20import os 

21import shutil 

22from pathlib import Path 

23from typing import Tuple, List, Optional 

24 

25 

26def listdir( 

27 path: str, 

28 no_files: bool = False, 

29 no_dirs: bool = False, 

30 no_dot: bool = True 

31) -> List[Tuple[str, str]]: 

32 """ 

33 Improves on the standard os.listdir function. 

34 By default, files and directories starting with `.` are ignored and 

35 one can disable checking for files or directories. 

36 Instead of just the filenames, the function returns tuples of filenames 

37 and relative file paths. 

38 :param path: The path of which to list the contents 

39 :param no_files: If set to True, will ignore files 

40 :param no_dirs: If set to True, will ignore directories 

41 :param no_dot: If set to True, will ignore files starting with `.` 

42 :return: A sorted list of the contents of the directory, consisting of 

43 tuples of the file/directory name and their relative path. 

44 """ 

45 content = [] 

46 for child in sorted(os.listdir(path)): 

47 child_path = os.path.join(path, child) 

48 

49 if no_files and os.path.isfile(child_path): 

50 continue 

51 elif no_dirs and os.path.isdir(child_path): 

52 continue 

53 elif no_dot and child.startswith("."): 

54 continue 

55 else: 

56 content.append((child, child_path)) 

57 return content 

58 

59 

60def makedirs(path: str, delete_before: bool = False): 

61 """ 

62 A more pleasant to use makedirs function. 

63 Only calls os.makedirs if the directory does not already exist 

64 :param path: The path to the directory to create 

65 :param delete_before: If True, deletes the directory beforehand, 

66 if it exists 

67 :return: None 

68 """ 

69 if delete_before and os.path.isdir(path): 

70 shutil.rmtree(path) 

71 

72 if not os.path.isdir(path): 

73 os.makedirs(path) 

74 

75 

76def replace_illegal_ntfs_chars(string: str) -> str: 

77 """ 

78 Replaces illegal characters in NTFS file systems with 

79 appropriate substitutes 

80 :param string: Te string in which to replace the illegal characters 

81 :return: The sanitized string 

82 """ 

83 illegal_characters = { 

84 "/": "ǁ", 

85 "\\": "ǁ", 

86 "?": "‽", 

87 "<": "←", 

88 ">": "→", 

89 ":": ";", 

90 "*": "∗", 

91 "|": "ǁ", 

92 "\"": "“" 

93 } 

94 for illegal_character, replacement in illegal_characters.items(): 

95 string = string.replace(illegal_character, replacement) 

96 return string 

97 

98 

99def create_file(path: str): 

100 """ 

101 Creates an empty file 

102 :param path: The path to the file 

103 :return: None 

104 """ 

105 if not os.path.isfile(path): 

106 with open(path, "w") as f: 

107 f.write("") 

108 

109 

110def get_ext(filename: str) -> Optional[str]: 

111 """ 

112 Gets the file extension of a file 

113 :param filename: The filename for which to get the file extension 

114 :return: The file extension or None if the file has no extension 

115 """ 

116 try: 

117 return filename.rsplit(".", 1)[1] 

118 except IndexError: 

119 return None 

120 

121 

122def touch(path: str): 

123 """ 

124 Ensures that a file exists 

125 :param path: Path to the file to ensure it exists 

126 """ 

127 Path(path).touch()