37,720
社区成员
发帖
与我相关
我的任务
分享
# -*- coding:utf-8 -*-
class Menu(object):
def __init__(self,label):
self.label=label
def click(self):
pass
class callbackMenu(Menu):
def __init__(self,label,callback,father=None):
self.label=label
self.rcallback=callback
self.rfather=father
def click(self,*args,**kwds):
print "currentClick",self.label
print
if callable(self.rcallback):
self.rcallback(*args,**kwds)
else:
return None
class subMenu(Menu):
def __init__(self,label,submenu,father=None):
self.label=label
self.rsubmenu=submenu
self.rfather=father
def show(self):
for i,sub in enumerate(self.rsubmenu):
print "[%d] %s"%(i,sub.label)
def click(self):
print "currentMenu",self.label
self.show()
def showfather(p):
if(hasattr(p,"rfather") and p.rfather==None):
print p.label
return
print p.label,'<-',
showfather(p.rfather)
def showfather2(p,symbol='->'):
if p.rfather==None:
print p.label,
return
showfather2(p.rfather,symbol)
print symbol,p.label,
def handle(p):
while True:
print '-'*40
showfather2(p)
print
p.click()
try:
get=raw_input("Input:")
if get in ['q','Q']:
break
get=int(get)
if get==100:
if hasattr(p,"rfather") and p.rfather!=None:
p=p.rfather
continue
if isinstance(p.rsubmenu[get],callbackMenu):
p.rsubmenu[get].click()
elif isinstance(p.rsubmenu[get],subMenu):
fp=p
p=p.rsubmenu[get]
p.rfather=fp
except:
print "Wrong Input"
if __name__=='__main__':
sub=subMenu("sub",[callbackMenu("cb1",None),
callbackMenu("cb2",None)])
sub2=subMenu("sub2",[callbackMenu("cb2_1",None),
callbackMenu("cb2_2",None)])
sub3=subMenu(
"sub3",
[subMenu("subsub1",
[callbackMenu("subcb1",None),
callbackMenu("subcb2",None)]),
subMenu("subsub2",
[callbackMenu("sub2cb1",None),
callbackMenu("sub2cb2",None)])])
main=subMenu("Main",[sub,sub2,sub3])
handle(main)
#!/usr/bin/env python
import menu_util
def func_b(menu_path_list):
'''
'''
print 'print in func_b()'
return 0
root_menu_dict = {
1 : ('1st_a', 'sub_a.py'),
2 : ('1st_b', func_b),
}
if __name__ == '__main__':
menu_util.menu_manage(['root'], root_menu_dict)
#!/usr/bin/env python
import menu_util
def sub_sub_sub_a(menu_path_list):
print 'in sub_sub_sub_a()'
return 0
def sub_sub_sub_b(menu_path_list):
print 'in sub_sub_sub_b()'
return 0
def sub_sub_menu_a(menu_path_list):
'''
'''
sub_sub_menu_a_dict = {
1 : ('3rd_a', sub_sub_sub_a),
2 : ('3rd_b', sub_sub_sub_b),
}
return menu_util.menu_manage(menu_path_list, sub_sub_menu_a_dict)
sub_a_menu_dict = {
1 : ('2nd_a', sub_sub_menu_a),
2 : ('2nd_b', 'sub_sub_b.py'),
}
def main(menu_path_list):
'''
'''
return menu_util.menu_manage(menu_path_list, sub_a_menu_dict)
#!/usr/bin/env python
def main(menu_path_list):
'''
'''
print 'in sub_sub_b.main()'
return 6
[root@louis menu]# ./main_menu.py
root:
1) 1st_a
2) 1st_b
Select option number ( b>ack or l>ist ) (b): 1
root -> 1st_a:
1) 2nd_a
2) 2nd_b
Select option number ( b>ack or l>ist ) (b): 1
root -> 1st_a -> 2nd_a:
1) 3rd_a
2) 3rd_b
Select option number ( b>ack or l>ist ) (b): 1
in sub_sub_sub_a()
Select option number ( b>ack or l>ist ) (b): l
root -> 1st_a -> 2nd_a:
1) 3rd_a
2) 3rd_b
Select option number ( b>ack or l>ist ) (b): 2
in sub_sub_sub_b()
Select option number ( b>ack or l>ist ) (b): l
root -> 1st_a -> 2nd_a:
1) 3rd_a
2) 3rd_b
Select option number ( b>ack or l>ist ) (b): b
root -> 1st_a:
1) 2nd_a
2) 2nd_b
Select option number ( b>ack or l>ist ) (b): 2
in sub_sub_b.main()
root -> 1st_a:
1) 2nd_a
2) 2nd_b
Select option number ( b>ack or l>ist ) (b): b
root:
1) 1st_a
2) 1st_b
Select option number ( b>ack or l>ist ) (b): 2
print in func_b()
Select option number ( b>ack or l>ist ) (b):
class CCommand//抽象类,根据需要派生不同的Command,在不同的菜单中调用
{
public:
do();
redo();
};
class CMenuItem
{
list<CMenuItem *> m_children;
CMenuItem * m_parent;
Command * m_cmd; //所要执行的任务对象
public:
printChildrenMenu();//当选择该菜单时,显示下级子菜单,即打印所以子菜单
AddCommand(Command & cmd);//一般来说,只有叶子节点菜单才会有,
//但也不排除非叶子节点也执行功能,增强菜单灵活性
};
class CTextMenu
{
CMenuItem & m_cursor=m_root;
CMenuItem m_root;
public:
CmenuItem & addMenuItem(CMenuItem & parent,CMenuItem & added);//return added
};
def sf():
'''
just a test function
'''
pass
import os
import grub_util
import menu_util
def query_system_boot_info(menu_path_list):
'''
'''
return grub_util.ShowBootInfo()
def switch_system(menu_path_list):
'''
'''
grub_util.ShowBootInfo()
answer = raw_input('Please enter new default boot item: ')
if answer.isdigit():
boot_index = int(answer)
return grub_util.SetDefaultItem(boot_index)
else:
print 'Input is invalid!'
return 0
system_boot_menu_dict = {
1 : ('Query boot info', query_system_boot_info),
2 : ('Switch to other system', switch_system),
}
def main(menu_path_list):
return menu_util.menu_manage(menu_path_list, system_boot_menu_dict)
if __name__ == '__main__':
pass
#!/usr/bin/env python
'''
Description: This is a menu manage module, it take a dictory data struct as a input and output a text menu
Author: Joshua.guo <shijian.guo@bigbandnet.com>
Date: 28/12/2010
'''
def sf():
'''
just a test function
'''
pass
def menu_manage(menu_path_list, menu_dict):
'''
menu_path_list likes below:
menu_path_list = ['1st', '2nd', '3rd']
menu_dict likes below:
menu_dict = {
1 : ('system', 'test.py'),
2 : ('software', 'test.py'),
3 : ('user', function),
4 : ('network', function),
5 : ('1+1 redundancy', 'test.py'),
6 : ('snmp', function),
7 : ('logout', 'test.py'),
}
The output menu likes below:
1st -> 2nd -> 3rd:
1) system
2) software
3) user
4) network
5) 1+1 redundancy
6) snmp
7) logout
Select option number ( b>ack or l>ist ) (b) :
'''
#output menu
output_menu(menu_path_list, menu_dict)
ret = 0
while True:
#output input prompt
print ''
input_str = raw_input('Select option number ( b>ack or l>ist ) (b): ')
if input_str in ['', 'b']:
if len(menu_path_list) == 1:
#it is the root menu, just output this menu again
output_menu(menu_path_list, menu_dict)
continue
else:
#back to top menu
return 6
elif input_str in ['l']:
print ''
output_menu(menu_path_list, menu_dict)
continue
try:
option_number = int(input_str)
except ValueError:
print 'Error, input should be a number!'
continue
if option_number not in menu_dict:
print 'Error, input option is invalid!'
continue
menu_node = menu_dict[option_number][0]
if type(menu_dict[option_number][1]) == type(sf):
#it is a function
sub_menu_path_list = menu_path_list[:]
sub_menu_path_list.append(menu_node)
func = menu_dict[option_number][1]
ret = func(sub_menu_path_list)
else:
#it is a script
script_name = menu_dict[option_number][1]
module_name = script_name[:-3]
exec('import ' + module_name)
sub_menu_path_list = menu_path_list[:]
sub_menu_path_list.append(menu_node)
exec "ret = %s.main(%s)" % (module_name, sub_menu_path_list)
if ret == 6:
#exit from child menu, output parent menu again
output_menu(menu_path_list, menu_dict)
else:
#do nothing
pass
return 0
def output_menu(menu_path_list, menu_dict):
'''
menu_path_list likes below:
menu_path_list = ['1st', '2nd', '3rd']
menu_dict likes below:
menu_dict = {
1 : ('system', 'test.py'),
2 : ('software', 'test.py'),
3 : ('user', function),
4 : ('network', function),
5 : ('1+1 redundancy', 'test.py'),
6 : ('snmp', function),
7 : ('logout', 'test.py'),
}
The output menu likes below:
1st -> 2nd -> 3rd:
1) system
2) software
3) user
4) network
5) 1+1 redundancy
6) snmp
7) logout
'''
#output menu path
menu_path = ' -> '.join(menu_path_list)
print menu_path + ':'
#output menu body
for menu_key in sorted(menu_dict.keys()):
print '\t%d) %s' % (menu_key, menu_dict[menu_key][0])
return 0