74 lines
3 KiB
Python
Executable file
74 lines
3 KiB
Python
Executable file
import os
|
|
import json
|
|
|
|
def check_and_create_folder_and_json(folder_path, json_file_name, default_json_content):
|
|
# Check if the folder exists
|
|
if not os.path.exists(folder_path):
|
|
# Create the folder if it doesn't exist
|
|
os.makedirs(folder_path)
|
|
print(f"Folder '{folder_path}' created.")
|
|
else:
|
|
print(f"Folder '{folder_path}' already exists.")
|
|
|
|
# Full path for the JSON file
|
|
json_file_path = os.path.join(folder_path, json_file_name)
|
|
|
|
# Check if the JSON file exists
|
|
if not os.path.exists(json_file_path):
|
|
# If it doesn't exist, then create the JSON file with "hello world"
|
|
with open(json_file_path, 'w') as json_file:
|
|
json.dump(default_json_content, json_file, indent=4)
|
|
print(f"JSON file '{json_file_name}' created with content: {{'message': 'hello world'}}.")
|
|
else:
|
|
print(f"JSON file '{json_file_name}' already exists in '{folder_path}'.")
|
|
|
|
## Load JSON file and return it as a dictionary.
|
|
def load_json_as_dict(folder_path, json_file_name, default_json_content):
|
|
full_path_with_filename = f"{folder_path}/{json_file_name}"
|
|
if not os.path.exists(full_path_with_filename):
|
|
print(f"JSON file '{full_path_with_filename}' does not exist yet. So the program is now making it")
|
|
check_and_create_folder_and_json(folder_path, json_file_name, default_json_content)
|
|
|
|
# Regardless of it was just made or not, load it,
|
|
with open(full_path_with_filename, 'r') as json_file:
|
|
data = json.load(json_file)
|
|
return data
|
|
|
|
## Search for a key in the dictionary and return its value or a message if not found.
|
|
def search_key_in_json(json_dict, key, default_value_if_not_found):
|
|
return json_dict.get(key, default_value_if_not_found)
|
|
|
|
def get_the_path_of_the_folder_for(which_folder_type, subfolder_name):
|
|
HOME = os.path.expanduser('~') # Get the user's home directory
|
|
config_path = os.path.join(HOME, which_folder_type) # Create a path to the .config directory
|
|
path_for_configs = f"{config_path}/sms-exchange/{subfolder_name}"
|
|
return path_for_configs
|
|
|
|
|
|
def replace_and_save_value_in_json(folder_path, json_file_name, default_json_content, key_value_to_find, value_to_replace):
|
|
# Check if the folder exists
|
|
if not os.path.exists(folder_path):
|
|
# Create the folder if it doesn't exist
|
|
os.makedirs(folder_path)
|
|
|
|
# Full path for the JSON file
|
|
json_file_path = os.path.join(folder_path, json_file_name)
|
|
|
|
# Check if the JSON file exists,
|
|
if os.path.exists(json_file_path):
|
|
# and if so then load it:
|
|
with open(json_file_path, 'r') as json_file:
|
|
data = json.load(json_file)
|
|
else:
|
|
# otherwise use the default values:
|
|
data = json.load(json_file_path)
|
|
|
|
# Replace the value of the key "key_value_to_find" with "value_to_replace"
|
|
data[key_value_to_find] = value_to_replace
|
|
|
|
# Now write the entire thing into the JSON file:
|
|
with open(json_file_path, 'w') as json_file:
|
|
json.dump(data, json_file, indent=4)
|
|
|
|
return data
|
|
|