Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
perf: ⚡️ add debug logging estimate
Browse files Browse the repository at this point in the history
  • Loading branch information
AnzhiZhang committed Jul 2, 2022
1 parent 6a5ffd9 commit 9d16b09
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 39 deletions.
74 changes: 42 additions & 32 deletions utils/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ def main(self):

# 新建临时文件夹
os.mkdir(self.dir_path)
self.factory.logger.debug(
'Successfully created temp dir %s for modpack %s',
self.dir_path,
self.name
)
if self.factory.logger.debug_mode:
self.factory.logger.debug(
'Successfully created temp dir %s for modpack %s',
self.dir_path,
self.name
)

# 工具
self.logger = Logger(self.name, self.log_file_path)
Expand Down Expand Up @@ -117,11 +118,12 @@ def clear():
if self.avatar_url:
with open(self.avatar_path, 'wb') as f:
f.write(self.requester.get(self.avatar_url).content)
self.logger.debug(
'Downloaded avatar from %s and saved to %s',
self.avatar_url,
self.avatar_path
)
if self.logger.debug_mode:
self.logger.debug(
'Downloaded avatar from %s and saved to %s',
self.avatar_url,
self.avatar_path
)

# 获取模组下载链接
download_urls = self.get_download_urls(update)
Expand Down Expand Up @@ -187,7 +189,8 @@ def get_download_urls(self, update: Callable):
)
result.append(url)
self.logger.info(f'获取模组下载链接({i}/{count})')
self.logger.debug(f'Got mod download url: %s', url)
if self.logger.debug_mode:
self.logger.debug(f'Got mod download url: %s', url)
return result

def download_mods(self, urls, update: Callable):
Expand All @@ -205,13 +208,14 @@ def download(url):
server_md5 = response.headers['ETag'].replace('"', '')
if calculated_md5 != server_md5:
failed_mods['verify'].append(f'{mod_name}{url})')
self.logger.debug(
'Mod %s\'s calculated md5 value %s '
'is difference with server provided md5 value %s!',
mod_name,
calculated_md5,
server_md5
)
if self.logger.debug_mode:
self.logger.debug(
'Mod %s\'s calculated md5 value %s '
'is difference with server provided md5 value %s!',
mod_name,
calculated_md5,
server_md5
)

# 写入文件
with open(mod_path, 'wb') as f:
Expand Down Expand Up @@ -301,11 +305,12 @@ def write_mmc_files(self):
}
with open(mmc_pack_path, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=4)
self.logger.debug(
'Saved following data into %s:\n%s',
mmc_pack_path,
data
)
if self.logger.debug_mode:
self.logger.debug(
'Saved following data into %s:\n%s',
mmc_pack_path,
data
)

# 写入 instance.cfg
instance_cfg_path = os.path.join(self.dir_path, 'instance.cfg')
Expand All @@ -314,25 +319,29 @@ def write_mmc_files(self):
content += f'iconKey={self.avatar_name}\n'
with open(instance_cfg_path, 'w', encoding='utf-8') as f:
f.write(content)
self.logger.debug(
'Saved following data into %s:\n%s',
instance_cfg_path,
content
)
if self.logger.debug_mode:
self.logger.debug(
'Saved following data into %s:\n%s',
instance_cfg_path,
content
)

def make_zip(self):
# 清理 CF 文件
modlist_path = os.path.join(self.dir_path, 'modlist.html')
if os.path.isfile(modlist_path):
os.remove(modlist_path)
self.logger.debug('Deleted file %s', modlist_path)
if self.logger.debug_mode:
self.logger.debug('Deleted file %s', modlist_path)
os.remove(self.manifest_path)
self.logger.debug('Deleted file %s', self.manifest_path)
if self.logger.debug_mode:
self.logger.debug('Deleted file %s', self.manifest_path)
os.rename(
self.overrides_dir_path,
os.path.join(self.dir_path, '.minecraft')
)
self.logger.debug('Renamed overrides to .minecraft')
if self.logger.debug_mode:
self.logger.debug('Renamed overrides to .minecraft')

# 压缩
with ZipFile(self.zip_file_path, mode='w', compression=ZIP_STORED) as z:
Expand All @@ -344,7 +353,8 @@ def make_zip(self):
self.dir_path.split(os.sep)[-1]
)
z.write(zf_path, arcname=arcname)
self.logger.debug('Made zip %s', self.zip_file_path)
if self.logger.debug_mode:
self.logger.debug('Made zip %s', self.zip_file_path)

# Move to out of temp dir
if PATH.DOWNLOADING_DIR_PATH in self.zip_file_path:
Expand Down
7 changes: 4 additions & 3 deletions utils/requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ def get(self, url: str, params: Dict[str, Any] = None) -> Response:
:return: Response.
"""
url = quote(url, safe=':/')
self.__factory.logger.debug(
'Sending GET request to %s with params %s', url, params
)
if self.__factory.logger.debug_mode:
self.__factory.logger.debug(
'Sending GET request to %s with params %s', url, params
)

# add params
if params:
Expand Down
9 changes: 5 additions & 4 deletions utils/window/frames/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ def run():
avatar_url = self.__main_window.show_frame.selected_avatar_url
file_path = os.path.join(PATH.DOWNLOADING_DIR_PATH, file_name)

self.__main_window.factory.logger.debug(
'Downloading modpack %s',
file_name
)
if self.__main_window.factory.logger.debug_mode:
self.__main_window.factory.logger.debug(
'Downloading modpack %s',
file_name
)

# Start download file
thread = Thread(target=run, name='Download')
Expand Down

0 comments on commit 9d16b09

Please sign in to comment.