Skip to content

Commit

Permalink
enforcing command book selection first, separating routines by comman…
Browse files Browse the repository at this point in the history
…d book name
  • Loading branch information
tanjeffreyz committed May 27, 2022
1 parent 4571722 commit 31aaa2d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion resources
24 changes: 21 additions & 3 deletions src/gui/menu/file.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import tkinter as tk
from src.common import config, utils
from src.gui.interfaces import MenuBarItem
from tkinter.filedialog import askopenfilename, asksaveasfilename
Expand All @@ -14,7 +15,15 @@ def __init__(self, parent, **kwargs):
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))
self.add_command(
label='Load Routine',
command=utils.async_callback(self, File._load_routine),
state=tk.DISABLED
)

def enable_routine_state(self):
print(config.bot.module_name)
self.entryconfig('Load Routine', state=tk.NORMAL)

@staticmethod
@utils.run_if_disabled('\n[!] Cannot create a new routine while Auto Maple is enabled')
Expand All @@ -30,7 +39,7 @@ def _new_routine():
@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'),
file_path = asksaveasfilename(initialdir=get_routines_dir(),
title='Save routine',
filetypes=[('*.csv', '*.csv')],
defaultextension='*.csv')
Expand All @@ -46,7 +55,7 @@ def _load_routine():
'Would you like to proceed anyways?',
icon='warning'):
return
file_path = askopenfilename(initialdir=os.path.join(config.RESOURCES_DIR, 'routines'),
file_path = askopenfilename(initialdir=get_routines_dir(),
title='Select a routine',
filetypes=[('*.csv', '*.csv')])
if file_path:
Expand All @@ -66,3 +75,12 @@ def _load_commands():
filetypes=[('*.py', '*.py')])
if file_path:
config.bot.load_commands(file_path)


def get_routines_dir():
target = os.path.join(config.RESOURCES_DIR, 'routines')
if config.bot.module_name is not None:
target = os.path.join(target, config.bot.module_name)
if not os.path.exists(target):
os.makedirs(target)
return target
8 changes: 6 additions & 2 deletions src/modules/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(self):
self.rune_pos = (0, 0)
self.rune_closest_pos = (0, 0) # Location of the Point closest to rune
self.submodules = {}
self.module_name = None
self.buff = components.Buff()

self.command_book = {}
Expand Down Expand Up @@ -166,7 +167,8 @@ def load_commands(self, file):

# Import the desired command book file
module_name = splitext(basename(file))[0]
module = __import__(f'resources.command_books.{module_name}', fromlist=[''])
target = '.'.join(['resources', 'command_books', module_name])
module = __import__(target, fromlist=[''])

# Check if the 'step' function has been implemented
step_found = False
Expand Down Expand Up @@ -197,12 +199,14 @@ def load_commands(self, file):
new_cb[name] = command

if not step_found and not movement_found:
print(f" ! Error: Must either implement both the 'move' and 'adjust' commands, "
print(f" ! Error: Must either implement both 'Move' and 'Adjust' commands, "
f"or the function 'step'.")
if required_found and (step_found or movement_found):
self.module_name = module_name
self.command_book = new_cb
self.buff = new_cb['buff']()
components.step = new_step
config.gui.menu.file.enable_routine_state()
config.gui.view.status.set_cb(basename(file))
config.routine.clear()
print(f"[~] Successfully loaded command book '{module_name}'.")
Expand Down
1 change: 1 addition & 0 deletions src/modules/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self):

self.navigation.pack(expand=True, fill='both')
self.navigation.bind('<<NotebookTabChanged>>', self._resize_window)
self.root.focus()

def set_routine(self, arr):
self.routine_var.set(arr)
Expand Down

0 comments on commit 31aaa2d

Please sign in to comment.