44from dataclasses import dataclass
55
66
7+ def stype (obj ):
8+ return type (obj ).__name__
9+
10+
711@dataclass
812class MysqlSettings :
913 host : str = 'localhost'
1014 port : int = 3306
1115 user : str = 'root'
1216 password : str = ''
1317
18+ def validate (self ):
19+ if not isinstance (self .host , str ):
20+ raise ValueError (f'mysql host should be string and not { stype (self .host )} ' )
21+
22+ if not isinstance (self .port , int ):
23+ raise ValueError (f'mysql port should be int and not { stype (self .port )} ' )
24+
25+ if not isinstance (self .user , str ):
26+ raise ValueError (f'mysql user should be string and not { stype (self .user )} ' )
27+
28+ if not isinstance (self .password , str ):
29+ raise ValueError (f'mysql password should be string and not { stype (self .password )} ' )
30+
1431
1532@dataclass
1633class ClickhouseSettings :
@@ -19,12 +36,35 @@ class ClickhouseSettings:
1936 user : str = 'root'
2037 password : str = ''
2138
39+ def validate (self ):
40+ if not isinstance (self .host , str ):
41+ raise ValueError (f'clickhouse host should be string and not { stype (self .host )} ' )
42+
43+ if not isinstance (self .port , int ):
44+ raise ValueError (f'clickhouse port should be int and not { stype (self .port )} ' )
45+
46+ if not isinstance (self .user , str ):
47+ raise ValueError (f'clickhouse user should be string and not { stype (self .user )} ' )
48+
49+ if not isinstance (self .password , str ):
50+ raise ValueError (f'clickhouse password should be string and not { stype (self .password )} ' )
51+
2252
2353@dataclass
2454class BinlogReplicatorSettings :
2555 data_dir : str = 'binlog'
2656 records_per_file : int = 100000
2757
58+ def validate (self ):
59+ if not isinstance (self .data_dir , str ):
60+ raise ValueError (f'binlog_replicator data_dir should be string and not { stype (self .data_dir )} ' )
61+
62+ if not isinstance (self .records_per_file , int ):
63+ raise ValueError (f'binlog_replicator records_per_file should be int and not { stype (self .data_dir )} ' )
64+
65+ if self .records_per_file <= 0 :
66+ raise ValueError ('binlog_replicator records_per_file should be positive' )
67+
2868
2969class Settings :
3070
@@ -48,6 +88,7 @@ def load(self, settings_file):
4888 assert isinstance (self .databases , str ) or isinstance (self .databases , list )
4989 assert isinstance (self .tables , str ) or isinstance (self .tables , list )
5090 self .binlog_replicator = BinlogReplicatorSettings (** data ['binlog_replicator' ])
91+ self .validate ()
5192
5293 @classmethod
5394 def is_pattern_matches (cls , substr , pattern ):
@@ -67,3 +108,8 @@ def is_database_matches(self, db_name):
67108
68109 def is_table_matches (self , table_name ):
69110 return self .is_pattern_matches (table_name , self .tables )
111+
112+ def validate (self ):
113+ self .mysql .validate ()
114+ self .clickhouse .validate ()
115+ self .binlog_replicator .validate ()
0 commit comments