From 2c8d86d04c45b291ebd13dcce4dcf558caa07156 Mon Sep 17 00:00:00 2001 From: Kazephil Date: Sat, 16 Jul 2022 12:18:06 +0900 Subject: [PATCH] Add 'common' module for functions used by more than one script. --- omegat_tools/common.py | 70 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 omegat_tools/common.py diff --git a/omegat_tools/common.py b/omegat_tools/common.py new file mode 100644 index 0000000..64075f7 --- /dev/null +++ b/omegat_tools/common.py @@ -0,0 +1,70 @@ +# -*- coding: utf-8 -*- + +'''Module that defines file-related dialogs used across scripts.''' + +import tkinter as tk +from tkinter import filedialog +from pathlib import Path +from configparser import ConfigParser + +# Constants +CONFIGFILE = Path('./config/omegat-tools.conf') +USER_HOME = Path.home() +DEFAULT_DOCHOME = Path(USER_HOME/'Documents') + +def read_config(configfile): + '''Load the configuration file''' + + parser = ConfigParser() + parser.read(configfile, encoding='utf-8') + + return parser + + +def set_basepath(candidates): + '''Define the default starting path for documents.''' + + # Give priority to path set in config file, if valid. + # If not, use the user's 'Documents' folder if it exists, + # and the user folder if not, as a default. + + basepath = None + + while basepath is None: + if candidates[0].exists(): + basepath = candidates[0] + else: + candidates.pop(0) + + return basepath + + +def set_root_window(): + '''Define a root window file dialog.''' + + rootWin = tk.Tk() + rootWin.attributes('-topmost', True) + rootWin.withdraw() + + +def select_folder(basepath, title): + '''Ask user to choose a folder.''' + + set_root_window() + folder = Path(filedialog.askdirectory(initialdir=basepath, + title=title)) + + return folder + + +def get_save_file_name(basepath, filetypes, title): + '''Ask for the name of the file to save.''' + + set_root_window() + save_file_name = Path(filedialog.asksaveasfilename(initialdir=basepath, + filetypes = filetypes, title=title)) + + return save_file_name + + +config = read_config(CONFIGFILE) \ No newline at end of file