@@ -73,14 +73,59 @@ class ServerConfig:
7373 def from_dict (cls , config_dict : dict ):
7474 return cls (** config_dict )
7575
76+ @dataclass
77+ class SQLiteConfig :
78+ """Configuration for SQLite database."""
79+ db_path : str
80+
81+ @classmethod
82+ def from_dict (cls , config_dict : dict ):
83+ """Create a PostgresConfig instance from dictionary.
84+
85+ Args:
86+ config_dict (dict): Dictionary containing the database configuration.
87+
88+ Returns:
89+ DatabaseConfig: Instance of DatabaseConfig with provided configuration.
90+ """
91+ return cls (db_path = config_dict ['db_path' ])
92+
93+
94+ @dataclass
95+ class PostgresConfig :
96+ """Configuration for PostgreSQL database."""
97+ host : str
98+ port : int
99+ user : str
100+ password : str
101+ db : str
102+ @classmethod
103+ def from_dict (cls , config_dict : dict ):
104+ """Create a PostgresConfig instance from dictionary.
105+
106+ Args:
107+ config_dict (dict): Dictionary containing the database configuration.
108+
109+ Returns:
110+ DatabaseConfig: Instance of DatabaseConfig with provided configuration.
111+ """
112+ return cls (host = config_dict ['host' ],
113+ port = config_dict ['port' ],
114+ user = config_dict ['user' ],
115+ password = config_dict ['password' ],
116+ db = config_dict ['db' ])
117+
118+
76119@dataclass
77120class DatabaseConfig :
78121 """Dataclass to store the configuration for the database, including the file path."""
79- db_file : str
122+ db_type : str
80123 cache_translation : bool
81124 use_cached_translation : bool
82125 use_latest_records : bool
83126 init_latest_records : int
127+ sqlite_config : SQLiteConfig
128+ postgres_config : PostgresConfig
84129
85130 @classmethod
86131 def from_dict (cls , config_dict : dict ):
@@ -92,7 +137,18 @@ def from_dict(cls, config_dict: dict):
92137 Returns:
93138 DatabaseConfig: Instance of DatabaseConfig with provided configuration.
94139 """
95- return cls (** config_dict )
140+
141+ print (config_dict )
142+ return cls (
143+ db_type = config_dict ['db_type' ],
144+ cache_translation = config_dict ['cache_translation' ],
145+ use_cached_translation = config_dict ['use_cached_translation' ],
146+ use_latest_records = config_dict ['use_latest_records' ],
147+ init_latest_records = config_dict ['init_latest_records' ],
148+ sqlite_config = SQLiteConfig .from_dict (config_dict ['sqlite_config' ]),
149+ postgres_config = PostgresConfig .from_dict (config_dict ['postgres_config' ])
150+ )
151+
96152
97153@dataclass
98154class HistoryConfig :
@@ -202,11 +258,21 @@ def from_args(cls, args):
202258 "use_latest_history" : args .use_latest_history
203259 }),
204260 database_config = DatabaseConfig .from_dict ({
205- "db_file " : args .db_file ,
261+ "db_type " : args .db_type ,
206262 "cache_translation" : args .cache_translation ,
207263 "use_cached_translation" : args .use_cached_translation ,
208264 "use_latest_records" : args .use_latest_records ,
209- "init_latest_records" : args .init_latest_records
265+ "init_latest_records" : args .init_latest_records ,
266+ "sqlite_config" : {
267+ "db_path" : args .sqlite_db_path
268+ },
269+ "postgres_config" : {
270+ "host" : args .postgres_host ,
271+ "port" : args .postgres_port ,
272+ "user" : args .postgres_user ,
273+ "password" : args .postgres_password ,
274+ "db" : args .postgres_db
275+ }
210276 }),
211277 logging_config = LoggingConfig .from_dict ({
212278 "log_file" : args .log_file ,
@@ -255,11 +321,21 @@ def parse_args():
255321 parser .add_argument ("--use_latest_history" , action = "store_true" , help = "Use latest history records" )
256322
257323 # Database Config
258- parser .add_argument ("--db_file " , type = str , default = "translated_texts.db " , help = "Path to the database file " )
324+ parser .add_argument ("--db_type " , type = str , default = "sqlite " , help = "Database type to use " )
259325 parser .add_argument ("--cache_translation" , action = "store_true" , help = "Enable translation caching" )
260326 parser .add_argument ("--use_cached_translation" , action = "store_true" , help = "Use cached translations if available" )
261327 parser .add_argument ("--use_latest_records" , action = "store_true" , help = "Use latest database records" )
262328 parser .add_argument ("--init_latest_records" , type = int , default = 20 , help = "Number of initial latest records" )
329+
330+ # PostgreSQL Configs
331+ parser .add_argument ("--postgres_host" , type = str , default = "localhost" , help = "PostgreSQL server host" )
332+ parser .add_argument ("--postgres_port" , type = int , default = 5432 , help = "PostgreSQL server port" )
333+ parser .add_argument ("--postgres_user" , type = str , help = "PostgreSQL username" )
334+ parser .add_argument ("--postgres_password" , type = str , help = "PostgreSQL password" )
335+ parser .add_argument ("--postgres_db" , type = str , help = "PostgreSQL database name" )
336+
337+ # SQLite Config
338+ parser .add_argument ("--sqlite_db_path" , type = str , default = "translated_texts.db" , help = "Path to the SQLite database file" )
263339
264340 # Logging Config
265341 parser .add_argument ("--log_file" , type = str , help = "Log file path" )
0 commit comments