2022-12-19 14:22:52 +00:00
|
|
|
from tkinter import filedialog, Tk
|
2022-12-19 16:43:29 +00:00
|
|
|
import os
|
2022-12-19 14:22:52 +00:00
|
|
|
|
2022-12-20 02:50:05 +00:00
|
|
|
|
2022-12-19 14:22:52 +00:00
|
|
|
def get_file_path(file_path='', defaultextension='.json'):
|
|
|
|
current_file_path = file_path
|
|
|
|
# print(f'current file path: {current_file_path}')
|
2022-12-20 02:50:05 +00:00
|
|
|
|
2022-12-19 14:22:52 +00:00
|
|
|
root = Tk()
|
|
|
|
root.wm_attributes('-topmost', 1)
|
|
|
|
root.withdraw()
|
2022-12-20 02:50:05 +00:00
|
|
|
file_path = filedialog.askopenfilename(
|
|
|
|
filetypes=(('Config files', '*.json'), ('All files', '*')),
|
|
|
|
defaultextension=defaultextension,
|
|
|
|
)
|
2022-12-19 14:22:52 +00:00
|
|
|
root.destroy()
|
2022-12-20 02:50:05 +00:00
|
|
|
|
2022-12-19 14:22:52 +00:00
|
|
|
if file_path == '':
|
|
|
|
file_path = current_file_path
|
2022-12-16 18:16:23 +00:00
|
|
|
|
|
|
|
return file_path
|
|
|
|
|
2022-12-29 19:00:02 +00:00
|
|
|
def get_any_file_path(file_path=''):
|
|
|
|
current_file_path = file_path
|
|
|
|
# print(f'current file path: {current_file_path}')
|
|
|
|
|
|
|
|
root = Tk()
|
|
|
|
root.wm_attributes('-topmost', 1)
|
|
|
|
root.withdraw()
|
|
|
|
file_path = filedialog.askopenfilename()
|
|
|
|
root.destroy()
|
|
|
|
|
|
|
|
if file_path == '':
|
|
|
|
file_path = current_file_path
|
|
|
|
|
|
|
|
return file_path
|
|
|
|
|
2022-12-17 01:26:26 +00:00
|
|
|
|
2022-12-17 14:51:30 +00:00
|
|
|
def remove_doublequote(file_path):
|
|
|
|
if file_path != None:
|
|
|
|
file_path = file_path.replace('"', '')
|
2022-12-16 18:16:23 +00:00
|
|
|
|
2022-12-17 01:26:26 +00:00
|
|
|
return file_path
|
2022-12-19 14:22:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_folder_path(folder_path=''):
|
|
|
|
current_folder_path = folder_path
|
2022-12-20 02:50:05 +00:00
|
|
|
|
2022-12-19 14:22:52 +00:00
|
|
|
root = Tk()
|
|
|
|
root.wm_attributes('-topmost', 1)
|
|
|
|
root.withdraw()
|
|
|
|
folder_path = filedialog.askdirectory()
|
|
|
|
root.destroy()
|
2022-12-20 02:50:05 +00:00
|
|
|
|
2022-12-19 14:22:52 +00:00
|
|
|
if folder_path == '':
|
|
|
|
folder_path = current_folder_path
|
|
|
|
|
|
|
|
return folder_path
|
|
|
|
|
2022-12-20 02:50:05 +00:00
|
|
|
|
2022-12-19 14:22:52 +00:00
|
|
|
def get_saveasfile_path(file_path='', defaultextension='.json'):
|
|
|
|
current_file_path = file_path
|
|
|
|
# print(f'current file path: {current_file_path}')
|
2022-12-20 02:50:05 +00:00
|
|
|
|
2022-12-19 14:22:52 +00:00
|
|
|
root = Tk()
|
|
|
|
root.wm_attributes('-topmost', 1)
|
|
|
|
root.withdraw()
|
2022-12-20 02:50:05 +00:00
|
|
|
save_file_path = filedialog.asksaveasfile(
|
|
|
|
filetypes=(('Config files', '*.json'), ('All files', '*')),
|
|
|
|
defaultextension=defaultextension,
|
|
|
|
)
|
2022-12-19 14:22:52 +00:00
|
|
|
root.destroy()
|
2022-12-20 02:50:05 +00:00
|
|
|
|
|
|
|
# print(save_file_path)
|
|
|
|
|
|
|
|
if save_file_path == None:
|
2022-12-19 14:22:52 +00:00
|
|
|
file_path = current_file_path
|
2022-12-19 14:47:35 +00:00
|
|
|
else:
|
|
|
|
print(save_file_path.name)
|
|
|
|
file_path = save_file_path.name
|
|
|
|
|
2022-12-20 02:50:05 +00:00
|
|
|
# print(file_path)
|
2022-12-19 14:22:52 +00:00
|
|
|
|
2022-12-19 16:43:29 +00:00
|
|
|
return file_path
|
|
|
|
|
2022-12-20 02:50:05 +00:00
|
|
|
|
|
|
|
def add_pre_postfix(
|
|
|
|
folder='', prefix='', postfix='', caption_file_ext='.caption'
|
|
|
|
):
|
2022-12-22 16:51:34 +00:00
|
|
|
if prefix == '' and postfix == '':
|
|
|
|
return
|
|
|
|
|
2022-12-20 14:15:17 +00:00
|
|
|
# set caption extention to default in case it was not provided
|
|
|
|
if caption_file_ext == '':
|
|
|
|
caption_file_ext = '.caption'
|
2022-12-22 16:51:34 +00:00
|
|
|
|
2022-12-20 02:50:05 +00:00
|
|
|
files = [f for f in os.listdir(folder) if f.endswith(caption_file_ext)]
|
2022-12-19 16:43:29 +00:00
|
|
|
if not prefix == '':
|
|
|
|
prefix = f'{prefix} '
|
|
|
|
if not postfix == '':
|
|
|
|
postfix = f' {postfix}'
|
2022-12-20 02:50:05 +00:00
|
|
|
|
2022-12-19 16:43:29 +00:00
|
|
|
for file in files:
|
|
|
|
with open(os.path.join(folder, file), 'r+') as f:
|
|
|
|
content = f.read()
|
|
|
|
content = content.rstrip()
|
2022-12-20 02:50:05 +00:00
|
|
|
f.seek(0, 0)
|
2022-12-19 16:43:29 +00:00
|
|
|
f.write(f'{prefix}{content}{postfix}')
|
2022-12-20 02:50:05 +00:00
|
|
|
f.close()
|