-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory.py
More file actions
33 lines (24 loc) · 986 Bytes
/
factory.py
File metadata and controls
33 lines (24 loc) · 986 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import os
import json
from exception import NameNotFoundError
from abstracts.factory import IDatabaseFactory
from database import ConnectConfig, OracleDatabase, PostgresDatabase
class DatabaseFactory(IDatabaseFactory):
def __init__(self, config_filename=""):
self.__config_filename = config_filename
self.__config = None
self.__init_config()
def __init__config(self):
with open(self.__config_filename, "r", encoding="utf-8") as f:
self.__config = json.load(f)
def get_database(self, db_type="ORACLE", db_name="") -> IDatabase:
try:
if not self.__config:
return None
conn_config = ConnectConfig(**self.__config[db_type][db_name])
db = OracleDatabase(conn_config) if db_type == "ORACLE" else PostgresDatabase(conn_config)
return db
except KeyError as e:
raise NameNotFoundError(e)
except Exception as e:
raise e