1- from typing import Literal
1+ from typing import Self
22
3- from pydantic import BaseModel , Field , validator
4- from pydantic import ConfigDict as ConfigDict
3+ from pydantic import BaseModel , ConfigDict , Field , field_validator , model_validator
54
65
76class Program (BaseModel ):
87 # rss_time must be greater than 300,if less than 300,it will be set to 300
98 rss_time : int = Field (default = 900 , description = "Sleep time" )
10- # rename_time must be greater than 30,if less than 0,it will be set to 30
11- rename_time : int = Field (default = 60 , description = "Rename times in one loop" )
129 webui_port : int = Field (default = 7892 , description = "WebUI port" )
1310
14- @validator ("rss_time" )
15- def validate_rss_time (cls , v : int ) -> int :
16- if v < 300 :
17- return 300
18- return v
19-
20- @validator ("rename_time" )
21- def validate_rename_time (cls , v : int ) -> int :
22- if v < 30 :
23- return 30
24- return v
11+ @model_validator (mode = "after" )
12+ def validate_rss_time (self ) -> Self :
13+ self .rss_time = max (self .rss_time , 300 )
14+ return self
2515
2616
2717class Downloader (BaseModel ):
2818 type : str = Field (default = "qbittorrent" , description = "Downloader type" )
2919 path : str = Field (default = "/downloads/Bangumi" , description = "Downloader path" )
30- host : str = Field (default = "172.17.0.1:8080" , alias = "host" , description = "Downloader host" )
31- ssl : bool = Field (default = False , description = "Downloader ssl" )
32-
33- class Config :
34- extra : str = "allow" # This allows extra fields not defined in the model
35-
36- @validator ("host" , pre = True )
37- def validate_host (cls , value : str ) -> str :
38- # 如果输入值没有以 http:// 或 https:// 开头,自动加上 http://
39- if not value .startswith (("http://" , "https://" )):
40- value = f"http://{ value } "
41- return value
42-
43- # username: str = Field("admin", alias="username", description="Downloader username")
44- # password: str = Field(
45- # "adminadmin", alias="password", description="Downloader password"
46- # )
47-
48-
49- class QbDownloader (Downloader ):
50- type : str = Field (default = "qbittorrent" , description = "Downloader type" )
5120 host : str = Field (
5221 default = "172.17.0.1:8080" , alias = "host" , description = "Downloader host"
5322 )
54- username : str = Field (
55- default = "admin" , alias = "username" , description = "Downloader username"
56- )
57- password : str = Field (
58- default = "adminadmin" , alias = "password" , description = "Downloader password"
59- )
60- path : str = Field (default = "/downloads/Bangumi" , description = "Downloader path" )
6123 ssl : bool = Field (default = False , description = "Downloader ssl" )
6224
25+ model_config = ConfigDict (extra = "allow" )
6326
64- class TrDownloader (Downloader ):
65- type : str = Field (default = "transmission" , description = "Downloader type" )
66- host_ : str = Field (
67- default = "172.17.0.1:9091" , alias = "host" , description = "Downloader host"
68- )
69- username_ : str = Field (
70- default = "admin" , alias = "username" , description = "Downloader username"
71- )
72- password_ : str = Field (
73- default = "admin" , alias = "password" , description = "Downloader password"
74- )
75- path : str = Field (default = "/downloads/Bangumi" , description = "Downloader path" )
76- ssl : bool = Field (default = False , description = "Downloader ssl" )
27+ @model_validator (mode = "after" )
28+ def validate_host (self ) -> Self :
29+ # 如果输入值没有以 http:// 或 https:// 开头,自动加上 http://
30+ if not self .host .startswith (("http://" , "https://" )):
31+ self .host = f"http://{ self .host } "
32+ return self
7733
7834
7935class RSSParser (BaseModel ):
@@ -108,14 +64,6 @@ class Proxy(BaseModel):
10864 username : str = Field (default = "" , alias = "username" , description = "Proxy username" )
10965 password : str = Field (default = "" , alias = "password" , description = "Proxy password" )
11066
111- # @property
112- # def username(self):
113- # return expandvars(self.username_)
114- #
115- # @property
116- # def password(self):
117- # return expandvars(self.password_)
118-
11967
12068class Notification (BaseModel ):
12169 enable : bool = Field (default = False , description = "Enable notification" )
@@ -125,33 +73,25 @@ class Notification(BaseModel):
12573 default = "" , alias = "chat_id" , description = "Notification chat id"
12674 )
12775
128- # @property
129- # def token(self):
130- # return expandvars(self.token_)
131- #
132- # @property
133- # def chat_id(self):
134- # return expandvars(self.chat_id_)
135-
13676
137- class ExperimentalOpenAI (BaseModel ):
138- enable : bool = Field (False , description = "Enable experimental OpenAI" )
139- api_key : str = Field ("" , description = "OpenAI api key" )
140- api_base : str = Field (
141- "https://api.openai.com/v1" , description = "OpenAI api base url"
142- )
143- api_type : Literal ["azure" , "openai" ] = Field (
144- "openai" , description = "OpenAI api type, usually for azure"
145- )
146- api_version : str = Field (
147- "2023-05-15" , description = "OpenAI api version, only for Azure"
148- )
149- model : str = Field (
150- "gpt-3.5-turbo" , description = "OpenAI model, ignored when api type is azure"
151- )
152- deployment_id : str = Field (
153- "" , description = "Azure OpenAI deployment id, ignored when api type is openai"
154- )
77+ # class ExperimentalOpenAI(BaseModel):
78+ # enable: bool = Field(False, description="Enable experimental OpenAI")
79+ # api_key: str = Field("", description="OpenAI api key")
80+ # api_base: str = Field(
81+ # "https://api.openai.com/v1", description="OpenAI api base url"
82+ # )
83+ # api_type: Literal["azure", "openai"] = Field(
84+ # "openai", description="OpenAI api type, usually for azure"
85+ # )
86+ # api_version: str = Field(
87+ # "2023-05-15", description="OpenAI api version, only for Azure"
88+ # )
89+ # model: str = Field(
90+ # "gpt-3.5-turbo", description="OpenAI model, ignored when api type is azure"
91+ # )
92+ # deployment_id: str = Field(
93+ # "", description="Azure OpenAI deployment id, ignored when api type is openai"
94+ # )
15595
15696
15797class Config (BaseModel ):
@@ -163,16 +103,6 @@ class Config(BaseModel):
163103 proxy : Proxy = Proxy ()
164104 notification : Notification = Notification ()
165105
166- class Config :
167- extra = "allow" # This allows extra fields not defined in the model
106+ model_config = ConfigDict (extra = "allow" )
168107
169108 # experimental_openai: ExperimentalOpenAI = ExperimentalOpenAI()
170-
171- # @override
172- # def model_dump(self, *args, by_alias=True, **kwargs):
173- # return super().model_dump(*args, by_alias=by_alias, **kwargs)
174-
175-
176- if __name__ == "__main__" :
177- pass
178- # t = Program(rss_time="1")
0 commit comments