Your IP : 216.73.217.13


Current Path : /home/deltalab/PMS/recommendations/recommender-system-batch/_library/data_utils/
Upload File :
Current File : //home/deltalab/PMS/recommendations/recommender-system-batch/_library/data_utils/io_toolkit.py

from dotenv import dotenv_values, find_dotenv
from os import path, makedirs
import json
from sys import exit

def read_settings(file_name = 'settings.json'):
    folder_path = ""
    file_path = path.join(folder_path, file_name)

    with open(file_path) as setting_file:
        settings = json.load(setting_file)
        
    return settings

def read_credentials():
    
    # Read the credentials stored in the ENV file 
    # STRUCTURE --> KEY = VALUE
    # LOCATION: Local machine (not uploaded on the repository)
    file_path = find_dotenv()
    
    # Read the file
    if file_path != '':
        config_file = dotenv_values(file_path)
        
        # PARM 1: Hostname
        if 'DELTA_HOSTNAME' in config_file.keys():
            delta_hostname = config_file['DELTA_HOSTNAME']
        else:
            delta_hostname = None
            print('ERROR [PARAM 1]: the HOSTNAME parameter has not been found!')
        
        # PARAM 2: Username
        if 'DELTA_USERNAME' in config_file.keys():
            delta_username = config_file['DELTA_USERNAME']
        else:
            delta_username = None
            print('ERROR [PARAM 2]: the USERNAME parameter has not been found!')
            
        # PARAM 3: Password
        if 'DELTA_PASSWORD' in config_file.keys():
            delta_password = config_file['DELTA_PASSWORD']
        else:
            delta_password = None
            print('ERROR [PARAM 3]: the PASSWORD parameter has not been found!')
    else:
        print(f'ERROR: The file ".env" has not been found! It must be provided!!')
        delta_hostname, delta_username, delta_password = None, None, None
        exit(1)
    return delta_hostname, delta_username, delta_password

def read_indaco_credentials(db_type):
    
    # Read the credentials stored in the ENV file 
    # STRUCTURE --> KEY = VALUE
    # LOCATION: Local machine (not uploaded on the repository)
    file_path = find_dotenv()
    
    # Read the file
    if file_path != '':
        config_file = dotenv_values(file_path)
        k_hostname ="INDACO_HOSTNAME"
        k_port = "INDACO_PORT"
        k_user = "INDACO_USERNAME"
        k_password = "INDACO_PASSWORD"
        k_dbname = "INDACO_DB_NAME"
        
        if(db_type == "etl"):
            k_hostname ="ETL_HOSTNAME"
            k_port = "ETL_PORT"
            k_user = "ETL_USERNAME"
            k_password = "ETL_PASSWORD"
            k_dbname = "ETL_DB_NAME"
        # PARM 1: Hostname
        if k_hostname in config_file.keys():
            indaco_hostname = config_file[k_hostname]
        else:
            indaco_hostname = None
            print('ERROR [PARAM 1]: the HOSTNAME parameter has not been found!')
        
        # PARAM 2: PORT
        if k_port in config_file.keys():
            indaco_port = int(config_file[k_port])
        else:
            indaco_port = None
            print('ERROR [PARAM 2]: the PORT parameter has not been found!')

        if k_user in config_file.keys():
            indaco_username = config_file[k_user]
        else:
            indaco_username = None
            print('ERROR [PARAM 3]: the DATABASE USERNAME parameter has not been found!')

        if k_password in config_file.keys():
            indaco_password = config_file[k_password]
        else:
            indaco_password = None
            print('ERROR [PARAM 4]: the DATABASE PASSWORD parameter has not been found!')

        if k_dbname in config_file.keys():
            db_name = config_file[k_dbname]
        else:
            db_name = None
            print('ERROR [PARAM 5]: the DATABASE NAME parameter has not been found!')
            
    else:
        print(f'ERROR: The file ".env" has not been found! It must be provided!')
        indaco_hostname, indaco_port, indaco_username, indaco_password, db_name = None, None, None, None, None
        exit(1)
    
    return indaco_hostname, indaco_port, indaco_username, indaco_password, db_name

def save_recommendations(recom_dict, folder_path, file_name): 
    
    # Create the folder if not exits
    if not path.exists(folder_path):
        makedirs(folder_path)
    
    # File path
    file_path = path.join(folder_path, file_name)
    
    # Save the data as a JSON file
    with open(file_path, mode = 'w', encoding = 'utf-8') as json_file: #
        json.dump(obj = recom_dict, fp = json_file, indent = 4, ensure_ascii = False) 
        
        # Visualize outcome
        print("\n" +"-" * 88)
        print("-" * 31, "OK: Recommendations saved", "-" * 30)
        print("-" * 88)
        print("-" * 12, f"\{file_path}", "-" * 12)
        print("-" * 88, "\n\n")