From 70716a19f7f2362da1b1049dff7eadd6765808db Mon Sep 17 00:00:00 2001 From: zthxxx Date: Fri, 9 Jun 2023 02:18:00 +0800 Subject: [PATCH] feat: support parse config file with Variable Expansion for some secret key --- .gitattributes | 14 +++++++ .gitignore | 5 ++- backend/dev.sh | 4 +- backend/src/module/api/config.py | 2 +- backend/src/module/database/orm/connector.py | 5 +++ backend/src/module/models/config.py | 43 ++++++++++++++++---- 6 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 .gitattributes mode change 100644 => 100755 backend/dev.sh diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000..e2aabb869 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,14 @@ +# Don't allow people to merge changes to these generated files, because the result +# may be invalid. You need to run "rush update" again. +pnpm-lock.yaml merge=binary +shrinkwrap.yaml merge=binary +npm-shrinkwrap.json merge=binary +yarn.lock merge=binary + +# Rush's JSON config files use JavaScript-style code comments. The rule below prevents pedantic +# syntax highlighters such as GitHub's from highlighting these comments as errors. Your text editor +# may also require a special configuration to allow comments in JSON. +# +# For more information, see this issue: https://github.com/microsoft/rushstack/issues/1088 +# +*.json linguist-language=JSON-with-Comments diff --git a/.gitignore b/.gitignore index 1e5de80b0..3dce47710 100644 --- a/.gitignore +++ b/.gitignore @@ -196,8 +196,11 @@ dist-ssr # Editor directories and files .vscode/* -!.vscode/extensions.json !.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json + .idea .DS_Store *.suo diff --git a/backend/dev.sh b/backend/dev.sh old mode 100644 new mode 100755 index 93cfcefbd..84cf8f804 --- a/backend/dev.sh +++ b/backend/dev.sh @@ -1,10 +1,10 @@ -#!/bin/bash +#!/usr/bin/env bash # This script is used to run the development environment. python3 -m venv venv -./venv/bin/python3 -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple install -r requirements-dev.txt +./venv/bin/python3 -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements-dev.txt # install git-hooks for pre-commit before committing. ./venv/bin/pre-commit install diff --git a/backend/src/module/api/config.py b/backend/src/module/api/config.py index 9519071e6..2c3969192 100644 --- a/backend/src/module/api/config.py +++ b/backend/src/module/api/config.py @@ -16,7 +16,7 @@ async def get_config(current_user=Depends(get_current_user)): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="invalid token" ) - return settings + return settings.dict() @router.post("/updateConfig") diff --git a/backend/src/module/database/orm/connector.py b/backend/src/module/database/orm/connector.py index 255aa290b..05be0df45 100644 --- a/backend/src/module/database/orm/connector.py +++ b/backend/src/module/database/orm/connector.py @@ -1,3 +1,4 @@ +import os import sqlite3 from .delete import Delete @@ -10,6 +11,10 @@ class Connector: def __init__(self, table_name: str, data: dict, database: str = DATA_PATH): + # Create folder if not exists + if not os.path.exists(os.path.dirname(DATA_PATH)): + os.makedirs(os.path.dirname(DATA_PATH)) + self._conn = sqlite3.connect(database) self._cursor = self._conn.cursor() self.update = Update(self, table_name, data) diff --git a/backend/src/module/models/config.py b/backend/src/module/models/config.py index 6e260c31e..a2e4d2a61 100644 --- a/backend/src/module/models/config.py +++ b/backend/src/module/models/config.py @@ -1,3 +1,4 @@ +from os.path import expandvars from pydantic import BaseModel, Field # Sub config @@ -12,21 +13,31 @@ class Program(BaseModel): class Downloader(BaseModel): type: str = Field("qbittorrent", description="Downloader type") host: str = Field("172.17.0.1:8080", description="Downloader host") - username: str = Field("admin", description="Downloader username") - password: str = Field("adminadmin", description="Downloader password") + username_: str = Field("admin", alias="username", description="Downloader username") + password_: str = Field("adminadmin", alias="password", description="Downloader password") path: str = Field("/downloads/Bangumi", description="Downloader path") ssl: bool = Field(False, description="Downloader ssl") + @property + def username(self): + return expandvars(self.username_) + + @property + def password(self): + return expandvars(self.password_) class RSSParser(BaseModel): enable: bool = Field(True, description="Enable RSS parser") type: str = Field("mikan", description="RSS parser type") - token: str = Field("token", description="RSS parser token") + token_: str = Field("token", alias="token", description="RSS parser token") custom_url: str = Field("mikanani.me", description="Custom RSS host url") parser_type: str = Field("parser", description="Parser type") filter: list[str] = Field(["720", r"\d+-\d"], description="Filter") language: str = "zh" + @property + def token(self): + return expandvars(self.token_) class BangumiManage(BaseModel): enable: bool = Field(True, description="Enable bangumi manage") @@ -45,16 +56,31 @@ class Proxy(BaseModel): type: str = Field("http", description="Proxy type") host: str = Field("", description="Proxy host") port: int = Field(0, description="Proxy port") - username: str = Field("", description="Proxy username") - password: str = Field("", description="Proxy password") + username_: str = Field("", alias="username", description="Proxy username") + password_: str = Field("", alias="password", description="Proxy password") + + @property + def username(self): + return expandvars(self.username_) + + @property + def password(self): + return expandvars(self.password_) class Notification(BaseModel): enable: bool = Field(False, description="Enable notification") type: str = Field("telegram", description="Notification type") - token: str = Field("", description="Notification token") - chat_id: str = Field("", description="Notification chat id") + token_: str = Field("", alias="token", description="Notification token") + chat_id_: str = Field("", alias="chat_id", description="Notification chat id") + @property + def token(self): + return expandvars(self.token_) + + @property + def chat_id(self): + return expandvars(self.chat_id_) class Config(BaseModel): program: Program = Program() @@ -64,3 +90,6 @@ class Config(BaseModel): log: Log = Log() proxy: Proxy = Proxy() notification: Notification = Notification() + + def dict(self, *args, by_alias=True, **kwargs): + return super().dict(*args, by_alias=by_alias, **kwargs)