From d39331be3f55876a8a6fc31c5a10f2d68eefe0b0 Mon Sep 17 00:00:00 2001 From: Satoshi Fujii Date: Tue, 15 Jun 2021 13:19:51 +0000 Subject: Reformat code Use 4 spaces for indentation and put spaces for better readability. Signed-off-by: Satoshi Fujii Issue-ID: DCAEGEN2-2833 Change-Id: I99aa4df83a32b077e2a3f336d17b6b64184c3c12 --- miss_htbt_service/config_notif.py | 134 +++++++++++++++++++++----------------- 1 file changed, 73 insertions(+), 61 deletions(-) (limited to 'miss_htbt_service/config_notif.py') diff --git a/miss_htbt_service/config_notif.py b/miss_htbt_service/config_notif.py index 913d8a5..087315d 100644 --- a/miss_htbt_service/config_notif.py +++ b/miss_htbt_service/config_notif.py @@ -36,27 +36,29 @@ import psycopg2 import mod.trapd_get_cbs_config import mod.trapd_settings as tds -hb_properties_file = path.abspath(path.join(__file__, "../config/hbproperties.yaml")) +hb_properties_file = path.abspath(path.join(__file__, "../config/hbproperties.yaml")) -def postgres_db_open(username,password,host,port,database_name): + +def postgres_db_open(username, password, host, port, database_name): envPytest = os.getenv('pytest', "") if (envPytest == 'test'): return True try: - connection = psycopg2.connect(database=database_name, user = username, password = password, host = host, port =port) + connection = psycopg2.connect(database=database_name, user=username, password=password, host=host, port=port) except Exception as e: print("HB_Notif::postgress connect error: %s" % e) connection = True return connection -def db_table_creation_check(connection_db,table_name): + +def db_table_creation_check(connection_db, table_name): envPytest = os.getenv('pytest', "") if (envPytest == 'test'): return True cur = None try: cur = connection_db.cursor() - query_db = "select * from information_schema.tables where table_name='%s'" %(table_name) + query_db = "select * from information_schema.tables where table_name='%s'" % (table_name) cur.execute(query_db) database_names = cur.fetchone() if (database_names is not None) and (table_name in database_names): @@ -73,45 +75,49 @@ def db_table_creation_check(connection_db,table_name): if cur: cur.close() + def commit_and_close_db(connection_db): envPytest = os.getenv('pytest', "") if (envPytest == 'test'): return True try: - connection_db.commit() # <--- makes sure the change is shown in the database + connection_db.commit() # <--- makes sure the change is shown in the database connection_db.close() return True except psycopg2.DatabaseError as e: return False + def read_hb_properties_default(): - #Read the hbproperties.yaml for postgress and CBS related data - s=open(hb_properties_file, 'r') - a=yaml.full_load(s) - if((os.getenv('pg_ipAddress') is None) or (os.getenv('pg_portNum') is None) or (os.getenv('pg_userName') is None) or (os.getenv('pg_passwd') is None)): - ip_address = a['pg_ipAddress'] - port_num = a['pg_portNum'] - user_name = a['pg_userName'] - password = a['pg_passwd'] - else: - ip_address = os.getenv('pg_ipAddress') - port_num = os.getenv('pg_portNum') - user_name = os.getenv('pg_userName') - password = os.getenv('pg_passwd') + # Read the hbproperties.yaml for postgress and CBS related data + s = open(hb_properties_file, 'r') + a = yaml.full_load(s) + if ((os.getenv('pg_ipAddress') is None) or (os.getenv('pg_portNum') is None) or ( + os.getenv('pg_userName') is None) or (os.getenv('pg_passwd') is None)): + ip_address = a['pg_ipAddress'] + port_num = a['pg_portNum'] + user_name = a['pg_userName'] + password = a['pg_passwd'] + else: + ip_address = os.getenv('pg_ipAddress') + port_num = os.getenv('pg_portNum') + user_name = os.getenv('pg_userName') + password = os.getenv('pg_passwd') + + dbName = a['pg_dbName'] + db_name = dbName.lower() + cbs_polling_required = a['CBS_polling_allowed'] + cbs_polling_interval = a['CBS_polling_interval'] + s.close() + # TODO: there is a mismatch here between read_hb_properties_default and read_hb_properties. + # read_hb_properties() forces all of the variables returned here to be strings, while the code here does not. + return ip_address, port_num, user_name, password, db_name, cbs_polling_required, cbs_polling_interval - dbName = a['pg_dbName'] - db_name = dbName.lower() - cbs_polling_required = a['CBS_polling_allowed'] - cbs_polling_interval = a['CBS_polling_interval'] - s.close() - # TODO: there is a mismatch here between read_hb_properties_default and read_hb_properties. - # read_hb_properties() forces all of the variables returned here to be strings, while the code here does not. - return ip_address, port_num, user_name, password, db_name, cbs_polling_required, cbs_polling_interval def read_hb_properties(jsfile): try: with open(jsfile, 'r') as outfile: - cfg = json.load(outfile) + cfg = json.load(outfile) except(Exception) as err: print("Json file read error - %s" % err) return read_hb_properties_default() @@ -125,13 +131,14 @@ def read_hb_properties(jsfile): cbs_polling_required = str(cfg['CBS_polling_allowed']) cbs_polling_interval = str(cfg['CBS_polling_interval']) if "SERVICE_NAME" in cfg: - os.environ['SERVICE_NAME'] = str(cfg['SERVICE_NAME']) + os.environ['SERVICE_NAME'] = str(cfg['SERVICE_NAME']) except(Exception) as err: print("Json file read parameter error - %s" % err) return read_hb_properties_default() return ip_address, port_num, user_name, password, db_name, cbs_polling_required, cbs_polling_interval -def read_hb_common(user_name,password,ip_address,port_num,db_name): + +def read_hb_common(user_name, password, ip_address, port_num, db_name): envPytest = os.getenv('pytest', "") if (envPytest == 'test'): hbc_pid = 10 @@ -139,7 +146,7 @@ def read_hb_common(user_name,password,ip_address,port_num,db_name): hbc_time = 1541234567 hbc_state = "RUNNING" return hbc_pid, hbc_state, hbc_srcName, hbc_time - connection_db = postgres_db_open(user_name,password,ip_address,port_num,db_name) + connection_db = postgres_db_open(user_name, password, ip_address, port_num, db_name) cur = connection_db.cursor() query_value = "SELECT process_id,source_name,last_accessed_time,current_state FROM hb_common;" cur.execute(query_value) @@ -154,22 +161,25 @@ def read_hb_common(user_name,password,ip_address,port_num,db_name): cur.close() return hbc_pid, hbc_state, hbc_srcName, hbc_time -def update_hb_common(update_flg, process_id, state, user_name,password,ip_address,port_num,db_name): + +def update_hb_common(update_flg, process_id, state, user_name, password, ip_address, port_num, db_name): current_time = int(round(time.time())) source_name = socket.gethostname() source_name = source_name + "-" + str(os.getenv('SERVICE_NAME', "")) envPytest = os.getenv('pytest', "") if (envPytest == 'test'): return True - connection_db = postgres_db_open(user_name,password,ip_address,port_num,db_name) + connection_db = postgres_db_open(user_name, password, ip_address, port_num, db_name) cur = connection_db.cursor() - query_value = "UPDATE hb_common SET PROCESS_ID='%d',SOURCE_NAME='%s', LAST_ACCESSED_TIME='%d',CURRENT_STATE='%s'" %(process_id, source_name, current_time, state) + query_value = "UPDATE hb_common SET PROCESS_ID='%d',SOURCE_NAME='%s', LAST_ACCESSED_TIME='%d',CURRENT_STATE='%s'" % ( + process_id, source_name, current_time, state) cur.execute(query_value) commit_and_close_db(connection_db) cur.close() return True -def fetch_json_file(download_json = "../etc/download1.json", config_json = "../etc/config.json"): + +def fetch_json_file(download_json="../etc/download1.json", config_json="../etc/config.json"): # use the fully qualified name here to let monkeypatching work # if get_cbs_config(): if mod.trapd_get_cbs_config.get_cbs_config(): @@ -188,31 +198,33 @@ def fetch_json_file(download_json = "../etc/download1.json", config_json = "../e print("Config_N: The json file is - %s" % jsfile) return jsfile -#if __name__ == "__main__": + +# if __name__ == "__main__": def config_notif_run(): - jsfile = fetch_json_file() - ip_address, port_num, user_name, password, db_name, cbs_polling_required, cbs_polling_interval = read_hb_properties(jsfile) - envPytest = os.getenv('pytest', "") - if (envPytest == 'test'): + jsfile = fetch_json_file() + ip_address, port_num, user_name, password, db_name, cbs_polling_required, cbs_polling_interval = read_hb_properties( + jsfile) + envPytest = os.getenv('pytest', "") + if (envPytest == 'test'): return True - connection_db = postgres_db_open(user_name,password,ip_address,port_num,db_name) - cur = connection_db.cursor() - if(db_table_creation_check(connection_db,"hb_common") == False): - print("HB_Notif::ERROR::hb_common table not exists - No config download") - connection_db.close() - else: - hbc_pid, hbc_state, hbc_srcName, hbc_time = read_hb_common(user_name,password,ip_address,port_num,db_name) - state = "RECONFIGURATION" - update_flg = 1 - ret = update_hb_common(update_flg, hbc_pid, state, user_name,password,ip_address,port_num,db_name) - # TODO: There is no way for update_hb_common() to return false - if (ret == True): - print("HB_Notif::hb_common table updated with RECONFIGURATION state") - commit_and_close_db(connection_db) - return True - else: - print("HB_Notif::Failure updating hb_common table") - commit_and_close_db(connection_db) - return False - - cur.close() + connection_db = postgres_db_open(user_name, password, ip_address, port_num, db_name) + cur = connection_db.cursor() + if (db_table_creation_check(connection_db, "hb_common") == False): + print("HB_Notif::ERROR::hb_common table not exists - No config download") + connection_db.close() + else: + hbc_pid, hbc_state, hbc_srcName, hbc_time = read_hb_common(user_name, password, ip_address, port_num, db_name) + state = "RECONFIGURATION" + update_flg = 1 + ret = update_hb_common(update_flg, hbc_pid, state, user_name, password, ip_address, port_num, db_name) + # TODO: There is no way for update_hb_common() to return false + if (ret == True): + print("HB_Notif::hb_common table updated with RECONFIGURATION state") + commit_and_close_db(connection_db) + return True + else: + print("HB_Notif::Failure updating hb_common table") + commit_and_close_db(connection_db) + return False + + cur.close() -- cgit 1.2.3-korg