Merge pull request #342 from zthxxx/3.1-dev

feat: support parse config file with Variable Expansion for some secret keys
This commit is contained in:
Estrella Pan
2023-06-16 21:12:35 +08:00
committed by GitHub
6 changed files with 62 additions and 11 deletions

14
.gitattributes vendored Normal file
View File

@@ -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

5
.gitignore vendored
View File

@@ -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

4
backend/dev.sh Normal file → Executable file
View File

@@ -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

View File

@@ -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")

View File

@@ -1,3 +1,4 @@
import os
import sqlite3
from .delete import Delete
@@ -10,6 +11,10 @@ from module.conf import DATA_PATH
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)

View File

@@ -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)