Skip to content

Commit bb58dc9

Browse files
committed
Allow nested commands to be set from the settings only (#4)
1 parent 73ca663 commit bb58dc9

4 files changed

Lines changed: 54 additions & 25 deletions

File tree

main.py

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,43 @@ def resetProgram():
1313

1414
options = Options(resetProgram)
1515

16-
# Convert the command list to a tuple list
17-
updatedCommands = []
18-
for command in options.commands:
19-
icon = command.icon
20-
if (command.icon == "" or command.icon == None):
21-
icon = None
22-
else:
23-
_, extension = splitext(command.icon)
24-
25-
# Ensure that the icon is valid
26-
if (not(exists(command.icon) and extension == ".ico")):
16+
def addCommandsToTuple(commands, tuple):
17+
for command in commands:
18+
icon = command.icon
19+
if (command.icon == "" or command.icon == None):
2720
icon = None
21+
else:
22+
_, extension = splitext(command.icon)
23+
24+
# Ensure that the icon is valid
25+
if (not(exists(command.icon) and extension == ".ico")):
26+
icon = None
27+
28+
# Recursively add items if a command list
29+
if (command.commandList != None):
30+
tuple = tuple + ((command.name, icon, addCommandsToTuple(command.commandList, ())),)
31+
32+
else:
33+
tuple = tuple + (((
34+
command.name,
35+
icon,
36+
command.getActionableCommand())),
37+
)
38+
return tuple
2839

29-
updatedCommands.append((
30-
command.name,
31-
icon,
32-
command.getActionableCommand())
33-
)
40+
# Convert the command list to a tuple list
41+
updatedCommands = ()
42+
updatedCommands = addCommandsToTuple(options.commands, updatedCommands)
3443

35-
# Add the options page
36-
updatedCommands.append(("Options", "icons/settings.ico", options.showOptionsPage))
44+
# Are there nested commands?
45+
nestedCommands = False
46+
for command in options.commands:
47+
if command.commandList != None:
48+
nestedCommands = True
49+
50+
# Add the options page if there are no nested commands as the options page currently doesn't support them
51+
if not nestedCommands:
52+
updatedCommands = updatedCommands + ((("Options", "icons/settings.ico", options.showOptionsPage)),)
3753

38-
sysTrayIcon = SysTrayIcon("icons/main.ico", "Tray Script", tuple(updatedCommands), default_menu_index=len(updatedCommands)-1)
54+
sysTrayIcon = SysTrayIcon("icons/main.ico", "Tray Script", updatedCommands, default_menu_index=len(updatedCommands)-1 if not nestedCommands else 0)
3955
sysTrayIcon.start()

src/Command.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ class Command:
2828
The constructor for the Command datatype
2929
name: The name of the command
3030
command: The command to perform
31+
commandList: The list of commands nested in the command
3132
runCommandInBackground: Whether to run the command in the background
3233
icon: The icon to use
3334
"""
34-
def __init__(self, name, command, runCommandInBackground, icon=None):
35+
def __init__(self, name, command, commandList, runCommandInBackground, icon=None):
3536
self.name = name
3637
self.command = command
38+
self.commandList = commandList
3739
self.runCommandInBackground = runCommandInBackground
3840
self.icon = icon
3941

@@ -44,9 +46,13 @@ def __init__(self, name, command, runCommandInBackground, icon=None):
4446
def __dict__(self):
4547
outDict = {
4648
"name": self.name,
47-
"command": self.command,
4849
"runCommandInBackground": self.runCommandInBackground
4950
}
51+
if (self.commandList != None and len(self.commandList) != 0):
52+
outDict["commandList"] = self.commandList
53+
else:
54+
outDict["command"] = self.command
55+
5056
if (self.icon != None):
5157
outDict["icon"] = self.icon
5258

src/ConfigReader.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
Returns: The commands
88
"""
99
def loadSettings(filePath):
10-
print(os.getcwd())
1110
# Open file
1211
f = open(os.getcwd() + "\\" + filePath, "r")
1312
data = json.loads(f.read())
@@ -19,15 +18,22 @@ def loadSettings(filePath):
1918
return []
2019

2120
# Get the valid commands
21+
return __getCommands(jsonCommands)
22+
23+
def __getCommands(jsonCommands):
2224
commands = []
2325
for jsonCommand in jsonCommands:
2426
name = jsonCommand.get("name", None)
2527
command = jsonCommand.get("command", None)
28+
commandList = jsonCommand.get("commandList", None)
2629
runCommandInBackground = jsonCommand.get("runCommandInBackground", False)
2730
icon = jsonCommand.get("icon", None)
2831

29-
if (name != None and command != None):
30-
commands.append(Command(name, command, runCommandInBackground, icon))
32+
if (name != None and (command != None or commandList != None)):
33+
if (commandList != None and isinstance(commandList, list)):
34+
commands.append(Command(name, None, __getCommands(commandList), runCommandInBackground, icon))
35+
else:
36+
commands.append(Command(name, command, None, runCommandInBackground, icon))
3137

3238
return commands
3339

src/Options.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ def __getCommandFromTextInputs(self):
290290
self.errorText.set("Please ensure that the icon exists and is an ico file")
291291
return
292292

293-
return Command(commandName, command, toRunInBackground, icon)
293+
# Currently set to empty list until the options menu is implemented
294+
return Command(commandName, command, [], toRunInBackground, icon)
294295

295296
"""
296297
A function to update the settings stored in the settings file and updates the options window

0 commit comments

Comments
 (0)