给openbox 装上 gnome的程序菜单

思路就是 从/usr/share/applications 里读取全部到启动器。然后找到里面到执行语句。最后再分类写到openbox的menu里

有个遗憾就是纯名命令行到启动器无法执行。比如 /usr/bin/python2.7  这个要在命令行里我就不会写了。改天再说吧。

这是用py写到。所以可以直接 chmod +x 运行。用用还是可以的(*^__^*) 嘻嘻……



#!/usr/bin/python
# -*- coding: UTF-8 -*-   
# Filename: update_ob_menu.py

import os
#------------------------------------------------------
def xml_char(x):

	x=x.replace('<','<')
	x=x.replace('>','>')
	x=x.replace('&','&')
	x=x.replace('"','"')
	x=x.replace("'",''')

	return x
#------------------------------------------------------
def xml_cmd(s):
	x=s.split(' ')
	y=[]
	for i in x:
		if i[0]!='%':
			y.append(i)
	print s
	print ' '.join(y)
	return xml_char(' '.join(y))
#------------------------------------------------------
def get_item(x):

	if x['type']=='Application':
		return '<item label="%s"><action name="Execute"><execute>%s</execute></action></item>'%(xml_char(x['name']),xml_cmd(x['cmd']))
	else:
		return '<item label="%s"><action name="Execute"><execute>x-www-browser %s</execute></action></item>'%(xml_char(x['name']),xml_cmd(x['cmd']))


#------------------------------------------------------
def get_attr(name,f):
	for line in f:
		if line.lower().startswith(name+'='):
			return line[len(name)+1:-1]
	return ""
#-------------------------------------------------------
def load(f):
	df={}
	ft=open(f)
	fo=[]
	while True:
		s = ft.readline()
		if s=="":
			break
		else:
			fo.append( s )
	df['name']=get_attr('name[zh_cn]',fo)
	if df['name']=='':
		df['name']=get_attr('name',fo)
	df['type']=get_attr('type',fo)
	if df['type']=='Application':
		df['cmd']=get_attr('exec',fo)
	else:
		df['cmd']=get_attr('url',fo)
	df['cate']=get_attr('categories',fo)
	df['terminal']=get_attr('terminal',fo).find('ure')>-1
	ft.close
	if len(df['cmd'])>0:
		return df
	else:
		return False;
#------------------------------------------------------
dff=[]
p='/usr/share/applications/'
for f in os.listdir(p):  
	if f.find('.desktop')>-1:
		x=load(p+f)
		if x:
			dff.append(x)
cate={\
'AudioVideo':[],\
'Development':[],\
'Education':[],\
'Game':[],\
'Graphics':[],\
'Network':[],\
'Office':[],\
'Settings':[],\
'System':[],\
'Utility':[]\
}
cate_other=[]

for i in dff:
	is_lost=True
	for k in cate.keys():		
		if i['cate'].find(k) != -1:
			cate[k].append(i)
			is_lost=False
	if is_lost:
		cate_other.append(i)
for k in cate.keys():
	cate[k].sort()
cate_other.sort()
cate['Other']=cate_other
#------------------------------------
cate_name={\
'AudioVideo':'影音',\
'Development':'开发',\
'Education':'教育',\
'Game':'游戏',\
'Graphics':'图形',\
'Network':'网络',\
'Office':'办公',\
'Settings':'设置',\
'System':'系统',\
'Utility':'实用工具',\
'Other':'其他'\
}
menu="<menu id='rmenu-jcy' label='全部程序'>"
for i in cate.keys():
	menu+="<menu id='rmenu-jck-%s' label='%s'>"%(xml_char(i),xml_char(cate_name[i]))
	for d in cate[i]:
		menu+=get_item(d)
	menu+="</menu>"
menu+="</menu>"+os.linesep

#------------------------------------
ft=open(os.environ["HOME"]+'/.config/openbox/menu.xml')
fo=[]
while True:
	s = ft.readline()
	if s=="":
		break
	else:
		fo.append( s )
ft.close()
l1=-1
l2=-1
i=0
for line in fo:
	if line.find("<menu id='rmenu-jcy'")>-1:
		l2=i
	elif line.find('<menu id="root-menu"')>-1:
		l1=i
	i+=1
if l1==-1:
	print 'menu.xml 文件格式不正确;找不到字符串<menu id="root-menu"'
	os.exit
if l2==-1:
	fo[l1+1:l1+1]=[menu]	
else:
	fo[l2]=menu

ft=open(os.environ["HOME"]+'/.config/openbox/menu.xml','w')
for line in fo:
	ft.write(line)
ft.close();


你可能感兴趣的:(xml,cmd,application,NetWork,Terminal,menu)