105 lines
3 KiB
Python
105 lines
3 KiB
Python
from core.utils.basic_operations.file_tools import read_a_file
|
|
|
|
from core.Constants import Constants
|
|
import os
|
|
import json
|
|
import ast
|
|
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 read_and_convert_with_eval(where_is_the_data: str) -> dict:
|
|
"""
|
|
Purpose:
|
|
Opens a file and uses literal_eval to convert it.
|
|
Why:
|
|
The py_ecc library format gives data in a weird format that traditional JSON tools struggle with.
|
|
Called by:
|
|
get_data (same module)
|
|
get_pub_key's get_local_copy_of_public_key
|
|
"""
|
|
raw_string_content = read_a_file(where_is_the_data)
|
|
|
|
if not raw_string_content:
|
|
return False
|
|
|
|
# Convert string to Python dict
|
|
python_dict = ast.literal_eval(raw_string_content)
|
|
return python_dict
|
|
|
|
|
|
|
|
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
|