2023-02-09 14:46:55 +00:00
|
|
|
import json
|
|
|
|
import os
|
2023-02-13 11:36:18 +00:00
|
|
|
import glob
|
2023-02-09 14:46:55 +00:00
|
|
|
|
|
|
|
|
2023-02-10 13:13:11 +00:00
|
|
|
# def extract_string(text, delimiter):
|
|
|
|
# # Extract string between delimiters
|
|
|
|
# start_index = text.index(delimiter) + len(delimiter)
|
|
|
|
# end_index = text.index(delimiter, start_index)
|
|
|
|
# return text[start_index:end_index]
|
2023-02-09 14:46:55 +00:00
|
|
|
|
2023-02-10 13:13:11 +00:00
|
|
|
|
|
|
|
def extract_string(text, delimiter, force=False, join=True, split_mode=False):
|
2023-02-13 11:36:18 +00:00
|
|
|
# Check if delimiter is not in text
|
|
|
|
if delimiter not in text:
|
|
|
|
# If force is True, return empty string; otherwise, return the original text
|
|
|
|
return '' if force else text
|
|
|
|
# If split_mode is True, split text by delimiter and return the resulting list
|
|
|
|
elif split_mode:
|
|
|
|
return text.split(delimiter)
|
2023-02-09 14:46:55 +00:00
|
|
|
else:
|
2023-02-13 11:36:18 +00:00
|
|
|
substring = text.split(delimiter)
|
|
|
|
result = []
|
|
|
|
# Split text by delimiter and select every second item starting from the second one
|
|
|
|
for i in range(1, len(substring), 2):
|
|
|
|
result.append(substring[i])
|
|
|
|
# If join is True, join the resulting list into a string and return it; otherwise, return the list
|
|
|
|
return ''.join(result) if join else result
|
2023-02-10 13:13:11 +00:00
|
|
|
|
2023-02-09 14:46:55 +00:00
|
|
|
|
2023-02-13 11:36:18 +00:00
|
|
|
def remove_oldest_file(directory, max_files):
|
2023-02-13 16:12:27 +00:00
|
|
|
files = scan_directory(directory)
|
2023-02-13 11:36:18 +00:00
|
|
|
if len(files) >= max_files:
|
|
|
|
oldest_file = min(files, key=os.path.getctime)
|
|
|
|
os.remove(oldest_file)
|
|
|
|
|
|
|
|
|
2023-02-13 16:12:27 +00:00
|
|
|
def scan_directory(directory):
|
|
|
|
files = glob.glob(f'{directory}/*')
|
|
|
|
return files
|
|
|
|
|
|
|
|
|
|
|
|
# get file name without extension
|
|
|
|
def get_file_name(filepath):
|
|
|
|
return os.path.splitext(os.path.basename(filepath))[0]
|
|
|
|
|
|
|
|
|
2023-02-13 11:36:18 +00:00
|
|
|
def create_path_not_exist(path):
|
|
|
|
directory = os.path.dirname(path)
|
|
|
|
if not os.path.exists(directory):
|
|
|
|
os.makedirs(directory)
|
2023-02-09 14:46:55 +00:00
|
|
|
|
2023-02-10 13:13:11 +00:00
|
|
|
|
2023-02-09 14:46:55 +00:00
|
|
|
def create_file_not_exist(path):
|
|
|
|
if not os.path.exists(path):
|
|
|
|
write_file('', path)
|
|
|
|
|
2023-02-10 13:13:11 +00:00
|
|
|
|
2023-02-09 14:46:55 +00:00
|
|
|
def read_file(filepath, delimiter='', force=False):
|
|
|
|
with open(filepath, 'r', encoding='utf-8') as file:
|
|
|
|
data = file.read()
|
2023-02-10 13:13:11 +00:00
|
|
|
return parse_data(data, delimiter, force)
|
|
|
|
|
2023-02-09 14:46:55 +00:00
|
|
|
|
2023-02-10 13:13:11 +00:00
|
|
|
def parse_data(data, delimiter='', force=False):
|
|
|
|
if delimiter != '':
|
|
|
|
data = extract_string(data, delimiter, force)
|
|
|
|
return data
|
2023-02-09 14:46:55 +00:00
|
|
|
|
2023-02-10 13:13:11 +00:00
|
|
|
|
|
|
|
def read_files(file_dir, delimiter='', force=False, single_string=True):
|
2023-02-09 14:46:55 +00:00
|
|
|
contents = []
|
|
|
|
|
|
|
|
# Read all files in a directory
|
|
|
|
for root, dirs, files in os.walk(file_dir):
|
|
|
|
for file in files:
|
|
|
|
# extract file path
|
|
|
|
filepath = os.path.join(root, file)
|
|
|
|
# extract filename with extension
|
|
|
|
filename = os.path.basename(filepath)
|
|
|
|
# extract filename without extension
|
|
|
|
filename = os.path.splitext(filename)[0]
|
|
|
|
file_data = read_file(filepath, delimiter, force)
|
|
|
|
if force and file_data == '':
|
|
|
|
continue
|
2023-02-10 13:13:11 +00:00
|
|
|
|
2023-02-09 14:46:55 +00:00
|
|
|
content = [f'[{filename}]', file_data]
|
|
|
|
contents.append('\n\n'.join(content))
|
2023-02-10 13:13:11 +00:00
|
|
|
if single_string:
|
|
|
|
result = '\n\n\n\n'.join(contents)
|
|
|
|
else:
|
|
|
|
result = contents
|
2023-02-09 14:46:55 +00:00
|
|
|
return result
|
|
|
|
|
2023-02-10 13:13:11 +00:00
|
|
|
|
2023-02-09 14:46:55 +00:00
|
|
|
def write_file(content, filepath, mode='w'):
|
2023-02-13 11:36:18 +00:00
|
|
|
create_path_not_exist(filepath)
|
2023-02-09 14:46:55 +00:00
|
|
|
with open(filepath, mode, encoding='utf-8') as file:
|
|
|
|
file.write(content)
|
|
|
|
|
2023-02-10 13:13:11 +00:00
|
|
|
|
2023-02-14 11:04:51 +00:00
|
|
|
def delete_file(filepath):
|
|
|
|
if os.path.exists(filepath):
|
|
|
|
os.remove(filepath)
|
|
|
|
|
|
|
|
|
2023-02-09 14:46:55 +00:00
|
|
|
def create_json_not_exist(filepath, initial_value={}):
|
|
|
|
if not os.path.exists(filepath):
|
|
|
|
write_json_file(initial_value, filepath)
|
|
|
|
|
2023-02-10 13:13:11 +00:00
|
|
|
|
2023-02-09 14:46:55 +00:00
|
|
|
def write_json_file(content, filepath, mode='w'):
|
|
|
|
with open(filepath, mode) as file:
|
|
|
|
json.dump(content, file, indent=2)
|
|
|
|
|
|
|
|
|
|
|
|
def read_json_file(filepath):
|
2023-02-10 13:13:11 +00:00
|
|
|
try:
|
|
|
|
with open(filepath, 'r') as file:
|
|
|
|
return json.load(file)
|
|
|
|
except FileNotFoundError:
|
|
|
|
return {}
|
2023-02-09 14:46:55 +00:00
|
|
|
|
2023-02-10 13:13:11 +00:00
|
|
|
|
|
|
|
def read_json_at(filepath, key, default_value=''):
|
2023-02-09 14:46:55 +00:00
|
|
|
data = read_json_file(filepath)
|
2023-02-10 13:13:11 +00:00
|
|
|
try:
|
|
|
|
# if key is string, check if it is boolean or numeric
|
|
|
|
if isinstance(data[key], str):
|
|
|
|
if data[key] in ['true', 'false']:
|
|
|
|
return data[key] == 'true'
|
|
|
|
elif data[key].isnumeric():
|
|
|
|
return int(data[key])
|
|
|
|
elif data[key].replace('.', '', 1).isnumeric():
|
|
|
|
return float(data[key])
|
|
|
|
else:
|
|
|
|
return data[key]
|
|
|
|
else:
|
|
|
|
return data[key]
|
|
|
|
except KeyError:
|
|
|
|
# if key not found, create key with default value
|
|
|
|
data[key] = default_value
|
|
|
|
write_json_file(data, filepath)
|
2023-02-09 14:46:55 +00:00
|
|
|
return data[key]
|
|
|
|
|
2023-02-10 13:13:11 +00:00
|
|
|
|
2023-02-09 14:46:55 +00:00
|
|
|
def update_json(filepath, key, value):
|
|
|
|
data = read_json_file(filepath)
|
|
|
|
data[key] = value
|
|
|
|
write_json_file(data, filepath)
|
|
|
|
|
|
|
|
|
|
|
|
def contains(list, item):
|
|
|
|
result = list.count(item)
|
|
|
|
return result > 0
|