11import datetime
22
3-
4- import pydantic
5- from pydantic import BaseModel , constr , Field
6-
7- if "2.0.0" <= pydantic .__version__ :
8- from pydantic import field_validator , model_validator , ConfigDict
9- else :
10- from pydantic import validator , root_validator
11-
3+ from pydantic import (
4+ BaseModel ,
5+ constr ,
6+ Field ,
7+ field_validator ,
8+ model_validator ,
9+ ConfigDict ,
10+ )
1211
1312from ._consts import LogLevelEnum
1413from ._utils import get_default_logs_dir , get_app_name
1514
1615
1716class ExtraBaseModel (BaseModel ):
18- if "2.0.0" <= pydantic .__version__ :
19- model_config = ConfigDict (extra = "allow" )
20- else :
21-
22- class Config :
23- extra = "allow"
17+ model_config = ConfigDict (extra = "allow" )
2418
2519
2620class StdHandlerPM (ExtraBaseModel ):
@@ -55,26 +49,13 @@ class LogHandlersPM(ExtraBaseModel):
5549 default = "{app_name}.std.err.log" , min_length = 4 , max_length = 1023
5650 )
5751
58- if "2.0.0" <= pydantic .__version__ :
59-
60- @model_validator (mode = "after" )
61- def _check_log_path (self ) -> "LogHandlersPM" :
62- if self .log_path == self .err_path :
63- raise ValueError (
64- f"`log_path` and `err_path` attributes are same: '{ self .log_path } ', must be different!"
65- )
66- return self
67-
68- else :
69-
70- @root_validator
71- def _check_log_path (cls , values ):
72- _log_path , _err_path = values .get ("log_path" ), values .get ("err_path" )
73- if _log_path == _err_path :
74- raise ValueError (
75- f"`log_path` and `err_path` attributes are same: '{ _log_path } ', must be different!"
76- )
77- return values
52+ @model_validator (mode = "after" )
53+ def _check_log_path (self ) -> "LogHandlersPM" :
54+ if self .log_path == self .err_path :
55+ raise ValueError (
56+ f"`log_path` and `err_path` attributes are same: '{ self .log_path } ', must be different!"
57+ )
58+ return self
7859
7960
8061class JsonHandlersPM (ExtraBaseModel ):
@@ -87,26 +68,13 @@ class JsonHandlersPM(ExtraBaseModel):
8768 default = "{app_name}.json.err.log" , min_length = 4 , max_length = 1023
8869 )
8970
90- if "2.0.0" <= pydantic .__version__ :
91-
92- @model_validator (mode = "after" )
93- def _check_log_path (self ) -> "JsonHandlersPM" :
94- if self .log_path == self .err_path :
95- raise ValueError (
96- f"`log_path` and `err_path` attributes are same: '{ self .log_path } ', must be different!"
97- )
98- return self
99-
100- else :
101-
102- @root_validator
103- def _check_log_path (cls , values ):
104- _log_path , _err_path = values .get ("log_path" ), values .get ("err_path" )
105- if _log_path == _err_path :
106- raise ValueError (
107- f"`log_path` and `err_path` attributes are same: '{ _log_path } ', must be different!"
108- )
109- return values
71+ @model_validator (mode = "after" )
72+ def _check_log_path (self ) -> "JsonHandlersPM" :
73+ if self .log_path == self .err_path :
74+ raise ValueError (
75+ f"`log_path` and `err_path` attributes are same: '{ self .log_path } ', must be different!"
76+ )
77+ return self
11078
11179
11280class FilePM (ExtraBaseModel ):
@@ -124,22 +92,12 @@ class FilePM(ExtraBaseModel):
12492 log_handlers : LogHandlersPM = Field (default_factory = LogHandlersPM )
12593 json_handlers : JsonHandlersPM = Field (default_factory = JsonHandlersPM )
12694
127- if "2.0.0" <= pydantic .__version__ :
128-
129- @field_validator ("rotate_time" , mode = "before" )
130- @classmethod
131- def _check_rotate_time (cls , val ):
132- if isinstance (val , str ):
133- val = datetime .time .fromisoformat (val )
134- return val
135-
136- else :
137-
138- @validator ("rotate_time" , pre = True , always = True )
139- def _check_rotate_time (cls , val ):
140- if val and isinstance (val , str ):
141- val = datetime .time .fromisoformat (val )
142- return val
95+ @field_validator ("rotate_time" , mode = "before" )
96+ @classmethod
97+ def _check_rotate_time (cls , val ):
98+ if isinstance (val , str ):
99+ val = datetime .time .fromisoformat (val )
100+ return val
143101
144102
145103class AutoLoadPM (ExtraBaseModel ):
0 commit comments