import mysql.connector as mysqlcon import pickle import os with open(os.path.realpath('.') + '/mysql_cfg.pickle', 'rb') as cfgFile: mysql_cfg = pickle.load(cfgFile) con = mysqlcon.connect(**mysql_cfg) def readConfig(): configProperties = {} configObj = {} con.connect() cur = con.cursor() query = "SELECT parameter, val FROM config GROUP BY parameter;" cur.execute(query) config = cur.fetchall() for x in config: configObj[x[0]] = x[1] try: configProperties['PLC_IP_ADDRESS'] = str(configObj['ip_address']) # print("FYI, using PLC IP Address from the database {0}".format(configProperties['PLC_IP_ADDRESS'])) except KeyError: print("FYI, there is no PLC IP Address stored in the database, defaulting to 192.168.1.10") # configProperties['PLC_IP_ADDRESS'] = "192.168.1.10" try: configProperties['plc_type'] = str(configObj['plc_type']) # print("FYI, using PLC Type from the database {0}".format(configProperties['plc_type'])) except KeyError: # print("FYI, there is no PLC Type stored in the database, defaulting to CLX") configProperties['plc_type'] = "CLX" try: configProperties['scan_rate'] = int(configObj['scan_rate']) # print("FYI, using Scan Rate from the database {0}".format(configProperties['scan_rate'])) except KeyError: # print("FYI, there is no Scan Rate stored in the database, defaulting to 10 seconds") configProperties['scan_rate'] = 10 try: sa_test = str(configObj['save_all']) if sa_test.lower() == "true": configProperties['save_all'] = True elif sa_test.lower() == "false": configProperties['save_all'] = False else: configProperties['save_all'] = "test" # print("FYI, value for save_all is {0}".format(configProperties['save_all'])) except KeyError: # print("FYI, there is no save_all value stored in the database, using 'test'") configProperties['save_all'] = 'test' return configProperties