sp-hydra-veil-core/core/utils/save_data.py

50 lines
1.5 KiB
Python

from core.Constants import Constants
from core.utils.basic_operations.write_string_to_text_file import write_string_to_text_file
import os
def save_data(which_ticket, name_of_file_and_folder, data_to_save):
data_folder = Constants.HV_TICKETING_DATA_HOME
folder_path = f"{data_folder}/{name_of_file_and_folder}"
# make sure folder exists:
if not os.path.exists(folder_path):
os.makedirs(folder_path)
# setup path:
which_ticket_as_string = str(which_ticket)
where_to_save_it = (
f"{folder_path}/{name_of_file_and_folder}_{which_ticket_as_string}.json"
)
# save it, or else return false if it failed:
try:
did_it_save = write_string_to_text_file(data_to_save, where_to_save_it)
return did_it_save
except:
return False
def save_data_as_json(which_ticket, name_of_file_and_folder, data_to_save):
data_folder = Constants.HV_TICKETING_DATA_HOME
folder_path = f"{data_folder}/{name_of_file_and_folder}"
# make sure folder exists:
if not os.path.exists(folder_path):
os.makedirs(folder_path)
# setup path:
which_ticket_as_string = str(which_ticket)
where_to_save_it = (
f"{folder_path}/{name_of_file_and_folder}_{which_ticket_as_string}.json"
)
# save it, or else return false if it failed:
try:
import json
with open(where_to_save_it, "w") as file:
json.dump(data_to_save, file, indent=4)
return True
except:
return False