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

Commit

Permalink
feat(config): ✨ add config file to storage api key
Browse files Browse the repository at this point in the history
  • Loading branch information
AnzhiZhang committed Jun 26, 2022
1 parent 1fd45d4 commit a3ecd95
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"conventionalCommits.scopes": [
"requester",
"ui",
"download"
"download",
"config"
]
}
9 changes: 2 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import os
import shutil

from utils.constant import PATH
from utils.factory import Factory
from utils.window.main import Main


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()
19 changes: 19 additions & 0 deletions utils/config.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 8 additions & 0 deletions utils/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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 = {
Expand Down
8 changes: 7 additions & 1 deletion utils/factory.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
8 changes: 5 additions & 3 deletions utils/requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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))
Expand Down
16 changes: 16 additions & 0 deletions utils/window/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit a3ecd95

Please sign in to comment.