files.py
Nov 25, 2022 3:08:08 GMT -8
Post by Uncle Buddy on Nov 25, 2022 3:08:08 GMT -8
<drive>:\treebard\app\python\files.py Last Changed 2024-03-03
# files.py
from sys import argv
from os import path, rename, mkdir, listdir, remove, rmdir
from shutil import copy2
import sqlite3
from utilities import titlize
from query_strings import (
select_closing_state_family_tree, update_closing_state_tree,
update_closing_state_recent_files,
select_closing_state_recent_files, select_current_image_directory)
import dev_tools as dt
from dev_tools import look, seeline
def get_current_drive():
return "{}/".format(argv[0].split('\\')[0])
current_drive = get_current_drive()
def get_treebard_path():
return f"{current_drive}treebard"
treebard_path = get_treebard_path()
def get_app_path():
return f"{current_drive}treebard/app/python/"
app_path = get_app_path()
def get_tree_path():
return f"{current_drive}treebard/data"
tree_path = get_tree_path()
def get_appwide_db_path():
return f"{current_drive}treebard/data/settings/appwide.db"
appwide_db_path = get_appwide_db_path()
def get_default_new_tree():
return f"{current_drive}treebard/data/settings/default_new_tree.db"
default_new_tree = get_default_new_tree()
def get_current_file(): # DEPRECATED--USE OR IMPROVE `get_current_tree()` instead.
""" As soon as a file opens, set it as the prior file. So the prior file is
the current file if a file is open and is the last one opened.
"""
prior_file, directory = get_prior_file()
if prior_file is None:
return None
if prior_file == "default_new_tree.db":
tree = prior_file
settings_dir = f'{current_drive}treebard/data/settings'
return tree, settings_dir
elif len(prior_file) > 0:
tree = f"{current_drive}treebard/data/{directory}/{prior_file}"
return tree, directory
else:
tree = ""
return tree, directory
def get_current_tree(family_tree_id, cur):
cur.execute(
"SELECT full_path, directory FROM family_tree WHERE family_tree_id = ?",
(family_tree_id,))
full_path, directory = cur.fetchone()
tree = f"{current_drive}{full_path}"
return tree, directory
def get_prior_file():
conn = sqlite3.connect(appwide_db_path)
cur = conn.cursor()
cur.execute(select_closing_state_family_tree)
result = cur.fetchone()
if result:
directory = result[0]
prior_file = f"{directory}.tbd"
else:
prior_file = ""
cur.close()
conn.close()
return prior_file, directory
def set_current_file(family_tree_id, conn, cur):
if family_tree_id is None:
return
cur.execute(update_closing_state_tree, (family_tree_id,))
conn.commit()
def get_image_dir(family_tree_id):
conn = sqlite3.connect(appwide_db_path)
cur = conn.cursor()
current_tree = get_current_tree(family_tree_id, cur)[0]
cur.execute("ATTACH ? as tree", (current_tree,))
cur.execute(select_current_image_directory)
imagedir = cur.fetchone()[0]
cur.execute("DETACH tree")
cur.close()
conn.close()
return imagedir
def get_recent_files():
""" Run this whenever a tree is opened. """
recent_trees = []
conn = sqlite3.connect(appwide_db_path)
cur = conn.cursor()
cur.execute(select_closing_state_recent_files)
result = cur.fetchone()
if result:
cur.close()
conn.close()
recent_trees = result[0].split("_+_")
return recent_trees
if len(recent_trees) == 0:
cur.close()
conn.close()
return []
def save_recent_tree(tree_title, recent_files, conn, cur):
""" Don't change recent_files, change the database and recreate recent_files. """
if tree_title not in recent_files:
recent_files.insert(0, tree_title)
else:
recent_files.insert(0, recent_files.pop(recent_files.index(tree_title)))
if len(recent_files) > 20:
recent_files = recent_files[0:20]
stored_string = "_+_".join(recent_files)
cur.execute(update_closing_state_recent_files, (stored_string,))
conn.commit()
if __name__ == "__main__":
def print_stuff():
# print("line", look(seeline()).lineno, "argv:", argv)
# print("line", look(seeline()).lineno, "current_drive:", current_drive)
# print("line", look(seeline()).lineno, "tree_path:", tree_path)
# print("line", look(seeline()).lineno, "appwide_db_path:", appwide_db_path)
# print("line", look(seeline()).lineno, "default_new_tree:", default_new_tree)
# print("line", look(seeline()).lineno, "directories:", directories)
# print("line", look(seeline()).lineno, "user_tree_titles:", user_tree_titles)
# print("line", look(seeline()).lineno, "filenames:", filenames)
# print("line", look(seeline()).lineno, "full_paths:", full_paths)
pass
import tkinter as tk
from tkinter import ttk
treebard = tk.Tk()
treebard.geometry("800x600+700+200")
treebard.config(bg="#818181")
file_selector = ttk.Combobox(
treebard, values=[i for i in user_tree_titles], width=48)
file_selector.grid()
opener = tk.Button(
treebard, command=lambda selector=file_selector:
open_tree(selector), text="OPEN")
opener.grid()
print_stuff()
treebard.mainloop()