-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from tanjeffreyz/dev
Added GUI for updating resources: - during startup, Auto Maple automatically pulls if no conflicts - "Update > Resources" in file menu for viewing local changes and force-updating resources
- Loading branch information
Showing
5 changed files
with
168 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import os | ||
from src.common import config, utils | ||
from src.gui.interfaces import MenuBarItem | ||
from tkinter.filedialog import askopenfilename, asksaveasfilename | ||
from tkinter.messagebox import askyesno | ||
|
||
|
||
class File(MenuBarItem): | ||
def __init__(self, parent, **kwargs): | ||
super().__init__(parent, 'File', **kwargs) | ||
# parent.add_cascade(label='File', menu=self) | ||
|
||
self.add_command(label='New Routine', command=utils.async_callback(self, File._new_routine)) | ||
self.add_command(label='Save Routine', command=utils.async_callback(self, File._save_routine)) | ||
self.add_separator() | ||
self.add_command(label='Load Command Book', command=utils.async_callback(self, File._load_commands)) | ||
self.add_command(label='Load Routine', command=utils.async_callback(self, File._load_routine)) | ||
|
||
@staticmethod | ||
@utils.run_if_disabled('\n[!] Cannot create a new routine while Auto Maple is enabled') | ||
def _new_routine(): | ||
if config.routine.dirty: | ||
if not askyesno(title='New Routine', | ||
message='The current routine has unsaved changes. ' | ||
'Would you like to proceed anyways?', | ||
icon='warning'): | ||
return | ||
config.routine.clear() | ||
|
||
@staticmethod | ||
@utils.run_if_disabled('\n[!] Cannot save routines while Auto Maple is enabled') | ||
def _save_routine(): | ||
file_path = asksaveasfilename(initialdir=os.path.join(config.RESOURCES_DIR, 'routines'), | ||
title='Save routine', | ||
filetypes=[('*.csv', '*.csv')], | ||
defaultextension='*.csv') | ||
if file_path: | ||
config.routine.save(file_path) | ||
|
||
@staticmethod | ||
@utils.run_if_disabled('\n[!] Cannot load routines while Auto Maple is enabled') | ||
def _load_routine(): | ||
if config.routine.dirty: | ||
if not askyesno(title='Load Routine', | ||
message='The current routine has unsaved changes. ' | ||
'Would you like to proceed anyways?', | ||
icon='warning'): | ||
return | ||
file_path = askopenfilename(initialdir=os.path.join(config.RESOURCES_DIR, 'routines'), | ||
title='Select a routine', | ||
filetypes=[('*.csv', '*.csv')]) | ||
if file_path: | ||
config.routine.load(file_path) | ||
|
||
@staticmethod | ||
@utils.run_if_disabled('\n[!] Cannot load command books while Auto Maple is enabled') | ||
def _load_commands(): | ||
if config.routine.dirty: | ||
if not askyesno(title='Load Command Book', | ||
message='Loading a new command book will discard the current routine, ' | ||
'which has unsaved changes. Would you like to proceed anyways?', | ||
icon='warning'): | ||
return | ||
file_path = askopenfilename(initialdir=os.path.join(config.RESOURCES_DIR, 'command_books'), | ||
title='Select a command book', | ||
filetypes=[('*.py', '*.py')]) | ||
if file_path: | ||
config.bot.load_commands(file_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,13 @@ | ||
"""A menu for loading routines and command books.""" | ||
|
||
import os | ||
import queue | ||
import tkinter as tk | ||
from src.common import config, utils | ||
from tkinter.filedialog import askopenfilename, asksaveasfilename | ||
from tkinter.messagebox import askyesno | ||
from src.gui.menu.file import File | ||
from src.gui.menu.update import Update | ||
|
||
|
||
class Menu(tk.Menu): | ||
def __init__(self, parent, **kwargs): | ||
super().__init__(parent, **kwargs) | ||
|
||
self.file = tk.Menu(self, tearoff=0) | ||
self.queue = queue.Queue() | ||
|
||
self.file.add_command(label='New Routine', command=utils.async_callback(self, Menu._new_routine)) | ||
self.file.add_command(label='Save Routine', command=utils.async_callback(self, Menu._save_routine)) | ||
self.file.add_separator() | ||
|
||
self.file.add_command(label='Load Command Book', command=utils.async_callback(self, Menu._load_commands)) | ||
self.file.add_command(label='Load Routine', command=utils.async_callback(self, Menu._load_routine)) | ||
|
||
self.add_cascade(label='File', menu=self.file) | ||
|
||
@staticmethod | ||
@utils.run_if_disabled('\n[!] Cannot create a new routine while Auto Maple is enabled') | ||
def _new_routine(): | ||
if config.routine.dirty: | ||
if not askyesno(title='New Routine', | ||
message='The current routine has unsaved changes. ' | ||
'Would you like to proceed anyways?', | ||
icon='warning'): | ||
return | ||
|
||
config.routine.clear() | ||
|
||
@staticmethod | ||
@utils.run_if_disabled('\n[!] Cannot save routines while Auto Maple is enabled') | ||
def _save_routine(): | ||
file_path = asksaveasfilename(initialdir=os.path.join(config.RESOURCES_DIR, 'routines'), | ||
title='Save routine', | ||
filetypes=[('*.csv', '*.csv')], | ||
defaultextension='*.csv') | ||
if file_path: | ||
config.routine.save(file_path) | ||
|
||
@staticmethod | ||
@utils.run_if_disabled('\n[!] Cannot load routines while Auto Maple is enabled') | ||
def _load_routine(): | ||
if config.routine.dirty: | ||
if not askyesno(title='Load Routine', | ||
message='The current routine has unsaved changes. ' | ||
'Would you like to proceed anyways?', | ||
icon='warning'): | ||
return | ||
|
||
file_path = askopenfilename(initialdir=os.path.join(config.RESOURCES_DIR, 'routines'), | ||
title='Select a routine', | ||
filetypes=[('*.csv', '*.csv')]) | ||
if file_path: | ||
config.routine.load(file_path) | ||
|
||
@staticmethod | ||
@utils.run_if_disabled('\n[!] Cannot load command books while Auto Maple is enabled') | ||
def _load_commands(): | ||
if config.routine.dirty: | ||
if not askyesno(title='Load Command Book', | ||
message='Loading a new command book will discard the current routine, ' | ||
'which has unsaved changes. Would you like to proceed anyways?', | ||
icon='warning'): | ||
return | ||
|
||
file_path = askopenfilename(initialdir=os.path.join(config.RESOURCES_DIR, 'command_books'), | ||
title='Select a command book', | ||
filetypes=[('*.py', '*.py')]) | ||
if file_path: | ||
config.bot.load_commands(file_path) | ||
self.file = File(self, tearoff=0) | ||
self.update = Update(self, tearoff=0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import git | ||
import tkinter as tk | ||
from src.common import config | ||
from src.gui.interfaces import MenuBarItem, LabelFrame, Frame | ||
from tkinter.messagebox import askyesno | ||
|
||
|
||
class Update(MenuBarItem): | ||
def __init__(self, parent, **kwargs): | ||
super().__init__(parent, 'Update', **kwargs) | ||
|
||
self.add_command(label='Resources', command=self._resources) | ||
|
||
def _resources(self): | ||
ResourcesPrompt(self) | ||
|
||
|
||
class ResourcesPrompt(tk.Toplevel): | ||
RESOLUTION = '500x400' | ||
|
||
def __init__(self, parent, **kwargs): | ||
super().__init__(parent, **kwargs) | ||
|
||
self.grab_set() | ||
self.title('Update Resources') | ||
icon = tk.PhotoImage(file='assets/icon.png') | ||
self.iconphoto(False, icon) | ||
self.geometry(ResourcesPrompt.RESOLUTION) | ||
self.resizable(False, False) | ||
|
||
self.columnconfigure(0, weight=1) | ||
self.columnconfigure(3, weight=1) | ||
self.list_var = tk.StringVar(value=[]) | ||
self.dirty = False | ||
|
||
# Display local changes | ||
display_frame = LabelFrame(self, 'Local Changes') | ||
display_frame.grid(row=0, column=1, sticky=tk.NSEW, padx=(10, 0), pady=10) | ||
self.scroll = tk.Scrollbar(display_frame) | ||
self.scroll.pack(side=tk.RIGHT, fill='both', pady=5) | ||
self.listbox = tk.Listbox(display_frame, | ||
width=40, | ||
height=15, | ||
listvariable=self.list_var, | ||
exportselection=False, | ||
activestyle='none', | ||
yscrollcommand=self.scroll.set) | ||
self.listbox.pack(side=tk.LEFT, expand=True, fill='both', padx=(5, 0), pady=5) | ||
|
||
# Controls | ||
controls_frame = Frame(self) | ||
controls_frame.grid(row=0, column=2, sticky=tk.NSEW, padx=10, pady=10) | ||
self.refresh = tk.Button(controls_frame, text='Refresh', command=self._refresh_display) | ||
self.refresh.pack(side=tk.TOP, pady=(10, 5)) | ||
self.submit = tk.Button(controls_frame, text='Update', command=self._update) | ||
self.submit.pack(side=tk.BOTTOM) | ||
|
||
self.listbox.bindtags((self.listbox, config.gui.root, "all")) # Unbind all events | ||
self.bind('<FocusIn>', lambda *_: self._refresh_display()) | ||
self.focus() | ||
|
||
def _update(self): | ||
if self.dirty: | ||
if not askyesno(title='Overwrite Local Changes', | ||
message='Updating resources will overwrite local changes. ' | ||
'Do you wish to proceed?', | ||
icon='warning'): | ||
return | ||
config.bot.update_submodules(force=True) | ||
self._close() | ||
|
||
def _refresh_display(self): | ||
self.list_var.set(['Searching for local changes...']) | ||
self.update() | ||
repo = git.Repo('resources') | ||
diffs = [] | ||
for item in repo.index.diff(None) + repo.index.diff('HEAD'): | ||
diffs.append(f'{item.change_type} - {item.a_path}') | ||
self.dirty = len(diffs) > 0 | ||
if len(diffs) == 0: | ||
diffs.append('No local changes found, safe to update') | ||
self.list_var.set(diffs) | ||
|
||
def _close(self): | ||
self.destroy() | ||
self.update() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters