Loads the configuration from the config file, including the database type and also the connection information.
55 def __read_config_file(self, path):
56 ret = {}
57
58 if not path:
59 home_dir = str(os.getenv('FAIRSHIP'))
60 path = home_dir + "/conditionsDatabase/config.yml"
61 else:
62 path_details = path.split(".")
63 file_extention = path_details[len(path_details) - 1]
64 if not(file_extention == "yml" or file_extention == "yaml"):
65 print("The file extension is incorrect. A YAML file is required.")
66 return None
67
68 try:
69 with open(path, 'r') as ymlfile:
70 cfg = yaml.load(ymlfile, Loader=yaml.FullLoader)
71 except IOError:
72 print("The configuration file does not exit or Invalid path to "
73 "the file:", str(sys.exc_info()[0]))
74 return None
75
76 connection_dict = {'db_name': '', 'user': None, 'password': None, 'host': "", 'port': 0}
77 db_type = None
78
79 try:
80 db_type = cfg["db_type"]
81 if db_type is None:
82 print("db type should be defined")
83 return None
84
85 if db_type not in cfg.keys():
86 print("db config is not defined")
87 return None
88
89 connection_dict["host"] = cfg[db_type]["host"]
90 connection_dict["db_name"] = cfg[db_type]["db_name"]
91 connection_dict["user"] = cfg[db_type]["user"]
92 connection_dict["password"] = cfg[db_type]["password"]
93 connection_dict["port"] = cfg[db_type]["port"]
94 ret[db_type.lower()] = connection_dict
95
96
97 return ret
98 except KeyError:
99 print("Incorrect configuration file, missing some parameters. it should contain: "
100 "db_type, db_name, user, password, host, and port.")
101 return None