Skip to content

Commit

Permalink
Merge pull request #83 from tanjeffreyz/dev
Browse files Browse the repository at this point in the history
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
tanjeffreyz authored May 27, 2022
2 parents 34eb83d + 4571722 commit b70fee0
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 73 deletions.
6 changes: 6 additions & 0 deletions src/gui/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ class Tab(Frame):
def __init__(self, parent, name, **kwargs):
super().__init__(parent, **kwargs)
parent.add(self, text=name)


class MenuBarItem(tk.Menu):
def __init__(self, parent, label, **kwargs):
super().__init__(parent, **kwargs)
parent.add_cascade(label=label, menu=self)
68 changes: 68 additions & 0 deletions src/gui/menu/file.py
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)
74 changes: 4 additions & 70 deletions src/gui/menu/main.py
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)
86 changes: 86 additions & 0 deletions src/gui/menu/update.py
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()
7 changes: 4 additions & 3 deletions src/modules/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(self):
self.rune_active = False
self.rune_pos = (0, 0)
self.rune_closest_pos = (0, 0) # Location of the Point closest to rune
self.submodules = {}
self.buff = components.Buff()

self.command_book = {}
Expand All @@ -55,7 +56,7 @@ def start(self):
:return: None
"""

Bot._update_submodules()
self.update_submodules()
print('\n[~] Started main bot loop')
self.thread.start()

Expand Down Expand Up @@ -210,8 +211,7 @@ def load_commands(self, file):
print(f"[!] Command book '{module_name}' was not loaded.")
return False

@staticmethod
def _update_submodules(force=False):
def update_submodules(self, force=False):
print('\n[~] Retrieving latest submodules:')
repo = git.Repo.init()
changed = False
Expand All @@ -222,6 +222,7 @@ def _update_submodules(force=False):
if lines[i].startswith('[') and i < len(lines) - 2:
path = lines[i + 1].split('=')[1].strip()
url = lines[i + 2].split('=')[1].strip()
self.submodules[path] = url
try: # First time loading submodule
repo.git.clone(url, path)
changed = True
Expand Down

0 comments on commit b70fee0

Please sign in to comment.