import json import os import logging def buildSave(action, outputs): savedata_log = os.environ.get("LOGS_SAVE_DATA_ACCESS", "false").lower() == "true" import tempfile if hasattr(action, "savedata"): save_data = {} for save_name in action.savedata: save_data[save_name] = outputs[save_name] #ith open(f"{action.savepath}", "w") as save_file: # json.dump(save_data, save_file) temporary_path = None #directory = os.path.dirname(os.path.abspath(path)) directory = os.environ.get("TEMP_DIR") try: with tempfile.NamedTemporaryFile( mode="w", #dir=directory, dir=directory, delete=False ) as file: json.dump(save_data, file) temporary_path = file.name os.replace(temporary_path, action.savepath) finally: if temporary_path: try: os.remove(temporary_path) except FileNotFoundError: pass if savedata_log: logging.info(f"Data saved by {action.name} to {action.savepath}") return def loadSave(action, outputs): savedata_log = os.environ.get("LOGS_SAVE_DATA_ACCESS", "false").lower() == "true" if hasattr(action, "loaddata"): with open(f"{action.savepath}", "r") as save_file: load_data = json.load(save_file) for load_name in action.loaddata: outputs[load_name] = load_data[load_name] if savedata_log: logging.info(f"Data loaded by {action.name} from {action.savepath}") return def saveRow(action, outputs, resolved): import sqlite3 from contextlib import closing if hasattr(action, "saverow"): #print("Saverow activated") table = action.saverow["table"] columns = action.saverow["columns"] column_list = [] column_creation = [] for item in columns: for key, value in item.items(): column_list.append(key) columnattr = "" columnattr = key + " " + value #print(columnattr) column_creation.append(columnattr) rowitems = action.saverow["rowitems"] values = [] for item in rowitems: try: if "outputs" in item: value = outputs[item["outputs"]] elif "resolved" in item: value = resolved[item["resolved"]] except KeyError as error: logging.error(f"The save data for {error} was not located in its respective dictionary during {action.name}") logging.error("Bypassing database save") return False values.append(value) database = sqlite3.connect("savedata/rmmanager.db") columnitems = ", ".join(column_creation) database.execute(f""" CREATE TABLE IF NOT EXISTS {table} ( {columnitems} ) """) column_names = ", ".join(column_list) placeholders = ", ".join(["?"] * len(values)) query = f"INSERT INTO {table} ({column_names}) VALUES ({placeholders})" if "onconflict" in action.saverow: conflict = action.saverow["onconflict"] target = conflict["target"] columns = conflict["update"] preserve_null = conflict.get("preservenull", False) if columns: if preserve_null: assignments = ", ".join( f"{column} = COALESCE(excluded.{column}, {table}.{column})" for column in columns ) else: assignments = ", ".join( f"{column} = excluded.{column}" for column in columns ) query += f" ON CONFLICT({target}) DO UPDATE SET {assignments}" else: query += f" ON CONFLICT({target}) DO NOTHING" try: with closing(sqlite3.connect("savedata/rmmanager.db")) as database: with database: database.execute(query, tuple(values)) except sqlite3.IntegrityError as error: logging.error(f"Unable to complete database write during {action.name}: {error}") return False #database.execute( # f"INSERT INTO {table} ({column_names}) VALUES ({placeholders})", # tuple(values) #) #database.commit() #database.close() logging.info(f"Successfully saved data to {table} during {action.name}") return def readRow(action, resolved, outputs): import sqlite3 result = None if hasattr(action, "readrow"): table = action.readrow["table"] columns = action.readrow["columns"] column_list = [] column_creation = [] for item in columns: for key, value in item.items(): column_list.append(key) columnattr = "" columnattr = key + " " + value #print(columnattr) column_creation.append(columnattr) rowsearchvalue = action.readrow["rowsearchvalue"] searchcolumn = action.readrow["searchcolumn"] values = [] for item in rowsearchvalue: if "outputs" in item: value = outputs[item["outputs"]] elif "resolved" in item: value = resolved[item["resolved"]] values.append(value) exports = action.readrow["export"] for column_index, output_name in exports.items(): outputs.pop(output_name, None) database = sqlite3.connect("savedata/rmmanager.db") columnitems = ", ".join(column_creation) database.execute(f""" CREATE TABLE IF NOT EXISTS {table} ( {columnitems} ) """) cursor = database.execute( f""" SELECT * FROM {table} WHERE {searchcolumn} = ? """, (value,) ) logging.debug("DEBUG READROW TARGET ITEM") logging.debug(value) result = cursor.fetchone() ## extension for matching by unique item vs column find if result is not None: for column_index, output_name in exports.items(): dbtarget = result[column_index] outputs[output_name] = dbtarget# result[column_index] logging.debug("DEBUGGING DATABASE LINE EXTRACTION IN READROW") logging.debug(outputs[output_name]) logging.debug(dbtarget) return else: logging.info(f"Search target {value} not found in database table {table}") return return