-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmenu.py
More file actions
83 lines (68 loc) · 2.31 KB
/
Copy pathmenu.py
File metadata and controls
83 lines (68 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
#
# MENU
#
# Menus are dictionaries where the key is the numeric selction
# identifier for the menu, and the item is a tuple containing the
# text to be displayed and the function to be executed for the
# given selection
#
# sw_menu = {'1':('Han',han_shot_first),'2':('Greedo',greedo_shot_first)}
#
# would display the following menu when called with run_menu(sw_menu)
#
# 1) Han
# 2) Greedo
#
# and execute either han_shot_first() or greedo_shot_first()
#
#
# print_menu orders a menu dict by key then displays it
# receive_menu waits on a user selection and then returns the matching tuple
# run_menu calls print_menu & receive_menu until a valid menu option is chosen
from collections import OrderedDict
class Menu:
def __init__(self, menu_map={}):
self.menu_map = menu_map
def add_item(self, key, text, function, data=None):
# Adds the item to the current menu
# key - menu item for user to hit
# text - text to display in menu
# function - function to call on press
# data - any extraneous data to associate with this menu item (Track, Artist, etc)
self.menu_map[key] = (text, function, data)
def get_item(self, key):
try:
return(self.menu_map[key])
except:
return None
def get_item_text(self, key):
try:
return(self.menu_map[key][0])
except:
return None
def get_item_function(self, key):
try:
return(self.menu_map[key][1])
except:
return None
def get_item_data(self, key):
try:
return(self.menu_map[key][2])
except:
return None
def print(self, end='\r\n'):
# Sort the menu and store in an collections.OrderedDict
# to ensure that items are displayed 1-9 or a-z
ordered_menu = OrderedDict(sorted(self.menu_map.items()))
print('\n', end=end)
for item in ordered_menu:
print('\t%s)\t%s' % (item, ordered_menu[item][0]), end=end)
print('\n', end=end)
def run_item(self, key):
function = self.get_item_function(key)
data = self.get_item_data(key)
if data:
function(data)
else:
function()