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# region [Imports] 

2 

3 

4# * Standard Library Imports --> 

5import os 

6import json 

7import shutil 

8import pickle 

9# * Gid Imports --> 

10import gidlogger as glog 

11from functools import partial 

12import base64 

13# import numpy as np 

14# endregion[Imports] 

15 

16 

17# region [Logging] 

18 

19log = glog.logging.getLogger('gidappdata') 

20log.info(glog.imported(__name__)) 

21 

22# endregion[Logging] 

23 

24# region [Constants] 

25 

26# endregion[Constants] 

27 

28 

29def create_folder(in_folder, overwrite=False): 

30 if os.path.isdir(in_folder) is True: 

31 if overwrite is True: 

32 shutil.rmtree(in_folder) 

33 elif overwrite is False: 

34 log.debug('folder "%s" already exists', in_folder) 

35 if os.path.isdir(in_folder) is False: 

36 os.makedirs(in_folder) 

37 log.debug('created folder "%s"', in_folder) 

38 

39 

40def create_file(in_file, file_content, overwrite=False): 

41 if os.path.isfile(in_file) is False or overwrite is True: 

42 if os.path.splitext(in_file)[1] == '.json': 

43 writejson(file_content, in_file, sort_keys=False, indent=2) 

44 else: 

45 _write_mode = 'wb' if isinstance(file_content, bytes) else 'w' 

46 content = file_content if isinstance(file_content, bytes) else str(file_content) 

47 with open(in_file, _write_mode) as outfile: 

48 outfile.write(content) 

49 log.debug('created file "%s"', in_file) 

50 else: 

51 log.debug('file "%s" already exists', in_file) 

52 

53 

54def pathmaker(first_segment, *in_path_segments, rev=False): 

55 """ 

56 Normalizes input path or path fragments, replaces '\\\\' with '/' and combines fragments. 

57 

58 Parameters 

59 ---------- 

60 first_segment : str 

61 first path segment, if it is 'cwd' gets replaced by 'os.getcwd()' 

62 rev : bool, optional 

63 If 'True' reverts path back to Windows default, by default None 

64 

65 Returns 

66 ------- 

67 str 

68 New path from segments and normalized. 

69 """ 

70 _first = os.getcwd() if first_segment == 'cwd' else first_segment 

71 _path = os.path.join(_first, *in_path_segments) 

72 _path = _path.replace('\\\\', '/') 

73 _path = _path.replace('\\', '/') 

74 if rev is True: 

75 _path = _path.replace('/', '\\') 

76 

77 return _path.strip() 

78 

79 

80def writebin(in_file, in_data): 

81 """ 

82 Writes a string to binary. 

83 

84 Parameters 

85 ---------- 

86 in_file : str 

87 The target file path 

88 in_data : str 

89 The data to write 

90 """ 

91 with open(in_file, 'wb') as outbinfile: 

92 outbinfile.write(in_data) 

93 

94 

95def writeit(in_file, in_data, append=False, in_encoding='utf-8', in_errors=None): 

96 """ 

97 Writes to a file. 

98 

99 Parameters 

100 ---------- 

101 in_file : str 

102 The target file path 

103 in_data : str 

104 The data to write 

105 append : bool, optional 

106 If True appends the data to the file, by default False 

107 in_encoding : str, optional 

108 Sets the encoding, by default 'utf-8' 

109 """ 

110 _write_type = 'w' if append is False else 'a' 

111 with open(in_file, _write_type, encoding=in_encoding, errors=in_errors,) as _wfile: 

112 _wfile.write(in_data) 

113 

114 

115def appendwriteit(in_file, in_data, in_encoding='utf-8'): 

116 with open(in_file, 'a', encoding=in_encoding) as appendwrite_file: 

117 appendwrite_file.write(in_data) 

118 

119 

120def readbin(in_file): 

121 """ 

122 Reads a binary file. 

123 

124 Parameters 

125 ---------- 

126 in_file : str 

127 A file path 

128 

129 Returns 

130 ------- 

131 str 

132 the decoded file as string 

133 """ 

134 with open(pathmaker(in_file), 'rb') as binaryfile: 

135 return binaryfile.read() 

136 

137 

138def readit(in_file, per_lines=False, in_encoding='utf-8', in_errors='replace'): 

139 """ 

140 Reads a file. 

141 

142 Parameters 

143 ---------- 

144 in_file : str 

145 A file path 

146 per_lines : bool, optional 

147 If True, returns a list of all lines, by default False 

148 in_encoding : str, optional 

149 Sets the encoding, by default 'utf-8' 

150 in_errors : str, optional 

151 How to handle encoding errors, either 'strict' or 'ignore', by default 'strict' 

152 

153 Returns 

154 ------- 

155 str/list 

156 the read in file as string or list (if per_lines is True) 

157 """ 

158 with open(in_file, 'r', encoding=in_encoding, errors=in_errors) as _rfile: 

159 _content = _rfile.read() 

160 if per_lines is True: 

161 _content = _content.splitlines() 

162 

163 return _content 

164 

165 

166def linereadit(in_file, in_encoding='utf-8', in_errors='strict'): 

167 with open(in_file, 'r', encoding=in_encoding, errors=in_errors) as lineread_file: 

168 _out = lineread_file.read().splitlines() 

169 return _out 

170 

171 

172def clearit(in_file): 

173 """ 

174 Deletes the contents of a file. 

175 

176 Parameters 

177 ---------- 

178 in_file : str 

179 The target file path 

180 """ 

181 with open(in_file, 'w') as file_to_clear: 

182 file_to_clear.write('') 

183 log.debug(f"contents of file '{in_file}' was cleared") 

184 

185 

186def loadjson(in_file): 

187 with open(in_file, 'r') as jsonfile: 

188 _out = json.load(jsonfile) 

189 return _out 

190 

191 

192def writejson(in_object, in_file, sort_keys=True, indent=0): 

193 with open(in_file, 'w') as jsonoutfile: 

194 json.dump(in_object, jsonoutfile, sort_keys=sort_keys, indent=indent) 

195 

196 

197def pickleit(obj, in_path): 

198 """ 

199 saves an object as pickle file. 

200 

201 Parameters 

202 ---------- 

203 obj : object 

204 the object to save 

205 in_name : str 

206 the name to use for the pickled file 

207 in_dir : str 

208 the path to the directory to use 

209 """ 

210 with open(pathmaker(in_path), 'wb') as filetopickle: 

211 log.debug(f"saved object [{str(obj)}] as pickle file [{in_path}]") 

212 pickle.dump(obj, filetopickle, pickle.HIGHEST_PROTOCOL) 

213 

214 

215def get_pickled(in_path): 

216 """ 

217 loads a pickled file. 

218 

219 Parameters 

220 ---------- 

221 in_path : str 

222 the file path to the pickle file 

223 

224 Returns 

225 ------- 

226 object 

227 the pickled object 

228 """ 

229 with open(pathmaker(in_path), 'rb') as pickletoretrieve: 

230 log.debug(f"loaded pickle file [{in_path}]") 

231 return pickle.load(pickletoretrieve) 

232 

233 

234# def np_readbin(in_path): 

235# return np.frombuffer(readbin(in_path)) 

236 

237 

238def read_file(in_file): 

239 _read_strategies = {'.txt': readit, 

240 '.ini': readit, 

241 '.json': loadjson, 

242 '.jpg': readbin, 

243 '.png': readbin, 

244 '.tga': readbin, 

245 '.ico': readbin, 

246 '.pkl': readbin, 

247 '.py': readit, 

248 '.cmd': readit, 

249 '.env': readit, 

250 '.log': readit, 

251 '.errors': readit, 

252 '.bat': readit, 

253 '.md': readit} 

254 _ext = os.path.splitext(in_file)[1] 

255 return _read_strategies.get(_ext, readbin)(in_file) 

256 

257 

258def to_attr_name(in_name): 

259 

260 replace_dict = {' ': '_', 

261 '-': '_', 

262 '.': '__', 

263 '/': '_', 

264 '\\': '_', 

265 '*': '', 

266 '{': '_', 

267 '}': '_', 

268 '[': '_', 

269 ']': '_', 

270 '(': '_', 

271 ')': '_', 

272 '>': '_', 

273 '<': '_', 

274 '#': '_', 

275 '+': '_', 

276 '&': '_', 

277 '$': '_', 

278 "'": '', 

279 '"': '', } 

280 

281 attr_name = in_name.strip() 

282 

283 for to_replace, replacement in replace_dict.items(): 

284 if to_replace in attr_name: 

285 for amount in reversed(range(1, 10)): 

286 if to_replace * amount in attr_name: 

287 

288 attr_name = attr_name.lstrip(to_replace * amount).rstrip(to_replace * amount).replace(to_replace * amount, replacement) 

289 return attr_name.casefold() 

290 

291 

292def filename_to_attr_name(in_file, keep_ext=False): 

293 attr_name = in_file 

294 if os.path.sep in attr_name or '/' in attr_name: 

295 attr_name = os.path.basename(attr_name) 

296 if keep_ext is False: 

297 attr_name = os.path.splitext(attr_name)[0] 

298 return to_attr_name(attr_name) 

299# region[Main_Exec] 

300 

301 

302if __name__ == '__main__': 

303 pass 

304 

305# endregion[Main_Exec]