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

83 lines
2.4 KiB
Python

from core.Constants import Constants
import os
import json
import ast
from core.errors.exceptions import *
from core.errors.logger import logger
def get_data(which_ticket, name_of_file_and_folder):
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_is_the_data = (
f"{folder_path}/{name_of_file_and_folder}_{which_ticket_as_string}.json"
)
try:
with open(where_is_the_data, "r") as file:
raw_string_content = file.read()
# Convert string to Python dict
python_dict = ast.literal_eval(raw_string_content)
return python_dict
except:
print("Error! Could not read from the file!")
return False
def get_raw_string(which_ticket, name_of_file_and_folder):
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_is_the_data = (
f"{folder_path}/{name_of_file_and_folder}_{which_ticket_as_string}.json"
)
try:
with open(where_is_the_data, "r") as file:
raw_string_content = file.read()
return raw_string_content
except:
return False
def get_data_as_int(which_ticket, name_of_file_and_folder):
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_is_the_data = (
f"{folder_path}/{name_of_file_and_folder}_{which_ticket_as_string}.json"
)
try:
with open(where_is_the_data, "r") as file:
raw_string_content = file.read()
# Convert to integer:
as_integer = int(raw_string_content)
return as_integer
except:
error_msg = f"Error! Could not read data from the file when going for {name_of_file_and_folder}! (inside get_data function, try-except block failed)."
logger.error(error_msg, exc_info=True)
print(error_msg)
return False