From a3ecd956b37e81063556b642c857d8fcc5ce476b Mon Sep 17 00:00:00 2001 From: Andy Zhang <37402126+AnzhiZhang@users.noreply.github.com> Date: Mon, 27 Jun 2022 00:02:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(config):=20=E2=9C=A8=20add=20config=20file?= =?UTF-8?q?=20to=20storage=20api=20key?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 4 ++-- main.py | 9 ++------- utils/config.py | 19 +++++++++++++++++++ utils/constant.py | 8 ++++++++ utils/factory.py | 8 +++++++- utils/requester.py | 8 +++++--- utils/window/main.py | 16 ++++++++++++++++ 7 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 utils/config.py diff --git a/.vscode/settings.json b/.vscode/settings.json index fcb52ff..1e7c79e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,7 +1,7 @@ { "conventionalCommits.scopes": [ - "requester", "ui", - "download" + "download", + "config" ] } \ No newline at end of file diff --git a/main.py b/main.py index fb3639f..a7854f7 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,4 @@ import os -import shutil from utils.constant import PATH from utils.factory import Factory @@ -7,16 +6,12 @@ def main(): - # 清理临时文件夹 - if os.path.isdir(PATH.TEMP_DIR_PATH): - shutil.rmtree(PATH.TEMP_DIR_PATH) - os.mkdir(PATH.TEMP_DIR_PATH) + if not os.path.isdir(PATH.TEMP_DIR_PATH): + os.mkdir(PATH.TEMP_DIR_PATH) factory = Factory() Main(factory).main() - shutil.rmtree(PATH.TEMP_DIR_PATH) - if __name__ == '__main__': main() diff --git a/utils/config.py b/utils/config.py new file mode 100644 index 0000000..93f4018 --- /dev/null +++ b/utils/config.py @@ -0,0 +1,19 @@ +import os +import yaml + +from utils.constant import CONFIG + + +class Config(dict): + def __init__(self): + if os.path.isfile(CONFIG.FILE_PATH): + with open(CONFIG.FILE_PATH, encoding='utf-8') as f: + super().__init__(yaml.safe_load(f)) + else: + super().__init__(CONFIG.DEFAULT) + self.save() + + def save(self): + """Save data""" + with open(CONFIG.FILE_PATH, 'w', encoding='utf-8') as f: + yaml.dump(self.copy(), f) diff --git a/utils/constant.py b/utils/constant.py index 9406737..6f67554 100644 --- a/utils/constant.py +++ b/utils/constant.py @@ -20,6 +20,7 @@ class PATH: BASE_DIR = BASE_DIR WORKING_DIR = os.getcwd() + DATA_DIR = os.path.join(WORKING_DIR, f'.{NAME}') if platform.system() == 'Windows': ICON_PATH = os.path.join(BASE_DIR, 'icon.ico') @@ -36,6 +37,13 @@ class WINDOW: SIZE = f'{WIDTH}x{HEIGHT}' +class CONFIG: + FILE_PATH = os.path.join(PATH.DATA_DIR, 'config.yml') + DEFAULT = { + 'curseForgeAPIKey': '' + } + + class SEARCH: VERSIONS = ['', '1.10.2', '1.12.2', '1.16.5', '1.18.2'] SORTING = { diff --git a/utils/factory.py b/utils/factory.py index eaffc40..43f0618 100644 --- a/utils/factory.py +++ b/utils/factory.py @@ -1,9 +1,15 @@ +from utils.config import Config from utils.requester import Requester class Factory: def __init__(self): - self.__requester = Requester() + self.__config = Config() + self.__requester = Requester(self) + + @property + def config(self): + return self.__config @property def requester(self): diff --git a/utils/requester.py b/utils/requester.py index 3d7d5d8..e0dd281 100644 --- a/utils/requester.py +++ b/utils/requester.py @@ -30,8 +30,10 @@ def json(self): class Requester: BASE_URL = 'https://api.curseforge.com' - @staticmethod - def get(url: str, params: Dict[str, Any] = None) -> Response: + def __init__(self, factory: 'Factory'): + self.__factory = factory + + def get(self, url: str, params: Dict[str, Any] = None) -> Response: """ Request using get method. :param url: URL. @@ -47,7 +49,7 @@ def get(url: str, params: Dict[str, Any] = None) -> Response: # send request headers = { 'Accept': 'application/json', - 'x-api-key': '*' + 'x-api-key': self.__factory.config['curseForgeAPIKey'] } request = Request(url, headers=headers, method='GET') return Response(urlopen(request)) diff --git a/utils/window/main.py b/utils/window/main.py index abb668a..0a9bcf3 100644 --- a/utils/window/main.py +++ b/utils/window/main.py @@ -2,6 +2,7 @@ import platform from tkinter import Tk from tkinter.messagebox import askokcancel +from tkinter.simpledialog import askstring from utils.constant import NAME_WITH_SPACE, LICENSE, WINDOW, PATH from utils.factory import Factory @@ -49,6 +50,21 @@ def main(self): if not ('--no-license' in sys.argv or askokcancel('版权声明', LICENSE)): return + # ask api key + if self.factory.config.get('curseForgeAPIKey') == '': + result = askstring( + 'Configuration', + '请输入 CurseForge API Key', + show='*', + parent=self + ) + if result is None: + self.quit() + else: + self.factory.requester.api_key = result + self.factory.config['curseForgeAPIKey'] = result + self.factory.config.save() + # update list self.show_frame.update_list(force=True)