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 

4import lzma 

5import os 

6 

7import zipfile 

8import os 

9import base64 

10import logging 

11import gidlogger as glog 

12from gidconfig.standard import ConfigRental 

13from gidappdata.standard_appdata.appdata_storager import AppDataStorager 

14from gidappdata.utility.functions import pathmaker 

15 

16# endregion [Imports] 

17 

18 

19# region [Logging] 

20 

21log = logging.getLogger('gidappdata') 

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

23 

24# endregion [Logging] 

25 

26# region [Constants] 

27 

28 

29# endregion [Constants] 

30 

31 

32# region [Configs] 

33 

34def unzip(root_dir, zip_file, overwrite: bool = False): 

35 # sourcery skip: simplify-boolean-comparison 

36 with zipfile.ZipFile(zip_file, 'r') as zipf: 

37 for item in zipf.namelist(): 

38 _info = zipf.getinfo(item) 

39 if _info.is_dir() is True: 

40 if os.path.isdir(pathmaker(root_dir, item)) is False: 

41 os.makedirs(pathmaker(root_dir, item)) 

42 log.debug("created folder '%s' because it did not exist", pathmaker(root_dir, item)) 

43 else: 

44 log.debug("folder '%s' already exists", pathmaker(root_dir, item)) 

45 else: 

46 if os.path.isfile(pathmaker(root_dir, item)) is False: 

47 zipf.extract(item, pathmaker(root_dir)) 

48 log.debug("extracted file '%s' because it didn't exist", pathmaker(root_dir, item)) 

49 elif overwrite is True: 

50 log.debug("file '%s' already exist and is overwriten because overwrite is 'True'", pathmaker(root_dir, item)) 

51 zipf.extract(item, pathmaker(root_dir)) 

52 else: 

53 log.debug("file '%s' is already existing and overwrite is 'False' so file was not extracted", pathmaker(root_dir, item)) 

54 

55 

56# endregion [Configs] 

57 

58 

59# region [Factories] 

60 

61class AppdataProvider: 

62 handler = None 

63 

64 @classmethod 

65 def setup_appdata(cls, author_name: str, app_name: str, folderlist: list = None, filelist: list = None, configs: dict = None, dev=None, redirect=None): 

66 if cls.handler is None: 

67 cls.handler = AppDataStorager(author_name, app_name, dev, redirect) 

68 if folderlist is not None: 

69 for _item in folderlist: 

70 if isinstance(_item, str): 

71 cls.handler.add_folder(_item) 

72 elif isinstance(_item, tuple): 

73 cls.handler.add_folder(_item[0], _item[1]) 

74 if filelist is not None: 

75 for _item in filelist: 

76 if _item[0].endswith('.json'): 

77 cls.handler.write_json(*_item) 

78 else: 

79 cls.handler.write(*_item) 

80 

81 if configs is not None: 

82 

83 cls.handler.generate_configs(**configs) 

84 ConfigRental.set_appdata(cls.handler) 

85 return cls.handler 

86 

87 @classmethod 

88 def archive_from_bin(cls, bin_data: str, name: str = 'user_data_archive', ext: str = 'zip', uses_base64: bool = False): 

89 _file = pathmaker(str(cls.handler), name + '.' + ext) 

90 with open(_file, 'wb') as archfile: 

91 if uses_base64 is True: 

92 bin_data = base64.b64decode(bin_data) 

93 archfile.write(bin_data) 

94 return _file 

95 

96 @classmethod 

97 def unpack_archive(cls, in_archive_file, clean: bool): 

98 unzip(str(cls.handler), in_archive_file, False) 

99 if clean: 

100 os.remove(in_archive_file) 

101 

102 @classmethod 

103 def setup_from_binarchive(cls, author_name: str, app_name: str, in_archive: str, uses_base64: bool, dev=None, redirect=None, clean=True): 

104 

105 if cls.handler is None: 

106 log.info("appdata, does not exist, creating from scratch") 

107 cls.handler = AppDataStorager(author_name, app_name, dev, redirect) 

108 _archive = cls.archive_from_bin(in_archive, uses_base64=uses_base64) 

109 cls.unpack_archive(_archive, clean=clean) 

110 ConfigRental.set_appdata(cls.handler) 

111 else: 

112 log.info("appdata, already existing so returning existing object") 

113 

114 return cls.handler 

115 

116 @classmethod 

117 def get_handler(cls): 

118 if cls.handler is not None: 

119 return cls.handler 

120 else: 

121 raise LookupError('AppDataStorage object has to be created first via "get_handler" of this factory') 

122 

123# endregion [Factories] 

124 

125 

126# region [Main_Exec] 

127if __name__ == '__main__': 

128 pass 

129 

130 

131# endregion [Main_Exec]