From 7157d90acf56d819dabfba8b56e1eed4e103a2e7 Mon Sep 17 00:00:00 2001 From: David Trattnig <david.trattnig@o94.at> Date: Sat, 29 Feb 2020 13:37:12 +0100 Subject: [PATCH] Method to get database URI. --- libraries/base/config.py | 47 ++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/libraries/base/config.py b/libraries/base/config.py index 8432558b..afb6f061 100644 --- a/libraries/base/config.py +++ b/libraries/base/config.py @@ -62,27 +62,27 @@ class AuraConfig: def set(self, key, value): """ - Set a property - @type key: string - @param key: The Key - @type value: mixed - @param value: Beliebiger Wert + Setter for some specific config property. + + Args: + key (String): key + default (*): value """ try: self.__dict__[key] = int(value) except: self.__dict__[key] = str(value) - # ------------------------------------------------------------------------------------------ # + + def get(self, key, default=None): """ - get a loaded property - @type key: string - @param key: Der Key - @type default: mixed - @param default: Beliebiger Wert - """ + Getter for some specific config property. + Args: + key (String): key + default (*): value + """ if key not in self.__dict__: if default: self.set(key, default) @@ -109,21 +109,22 @@ class AuraConfig: return self.__dict__[key] - # ------------------------------------------------------------------------------------------ # + + def load_config(self): """ Set config defaults and load settings from file - :return: """ if not os.path.isfile(self.ini_path): self.logger.critical(self.ini_path + " not found :(") sys.exit(1) - # INI einlesen + # Read the file f = open(self.ini_path, 'r') ini_str = f.read() f.close() + # Parse the values config_parser = ConfigParser() try: config_parser.read_string(ini_str) @@ -136,5 +137,19 @@ class AuraConfig: v = config_parser.get(section, key).replace('"', '').strip() self.set(key, v) + # Custom overrides self.set("install_dir", os.path.realpath(__file__ + "../../../..")) - self.set("use_test_data", False) \ No newline at end of file + self.set("use_test_data", False) + + + + def get_database_uri(self): + """ + Retrieves the database connection string. + """ + db_name = self.get("db_name") + db_user = self.get("db_user") + db_pass = self.get("db_pass") + db_host = self.get("db_host") + db_charset = self.get("db_charset", "utf8") + return "mysql://" + db_user + ":" + db_pass + "@" + db_host + "/" + db_name + "?charset=" + db_charset \ No newline at end of file -- GitLab