Instantly share code, notes, and snippets.
Created
July 22, 2012 06:44
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save alick/3158721 to your computer and use it in GitHub Desktop.
fvwm-xdg-menu with some fixes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Author: Piotr Zielinski (http://www.cl.cam.ac.uk/~pz215/) | |
# Licence: GPL 2 | |
# Date: 03.12.2005 | |
# This script takes names of menu files conforming to the XDG Desktop | |
# Menu Specification, and outputs their FVWM equivalents to the | |
# standard output. | |
# | |
# http://standards.freedesktop.org/menu-spec/latest/ | |
# Syntax: | |
# | |
# fvwm-xdg-menu.py menufile1 menufile2 menufile3 ... | |
# | |
# Each menufile is an XDG menu description file. Example: | |
# | |
# fvwm-xdg-menu.py /etc/xdg/menus/gnome-applications.menu | |
# This script requires the python-xdg module, which in Debian can be | |
# installed by typing | |
# | |
# apt-get install python-xdg | |
import sys | |
import xdg.Menu | |
import xdg.IconTheme | |
import xdg.Locale | |
import optparse | |
import os.path | |
import os | |
from xdg.DesktopEntry import * | |
import logging | |
logging.basicConfig( | |
filename='debug.log', | |
filemode='w', | |
level=logging.INFO, # may changed to DEBUG etc | |
) | |
usage = """ | |
%prog [options] file1 file2 ... | |
This script takes names of menu files conforming to the XDG Desktop | |
Menu Specification, and outputs their FVWM equivalents to the standard | |
output. | |
http://standards.freedesktop.org/menu-spec/latest/ | |
examples: | |
%prog /etc/xdg/menus/gnome-applications.menu | |
%prog /etc/xdg/menus/kde-applications.menu | |
%prog /etc/xdg/menus/debian-menu.menu""" | |
parser = optparse.OptionParser(usage=usage) | |
parser.add_option("-e", "--exec", dest="exec_command", type="string", | |
default="Exec exec", | |
help="FVWM command used to execute programs [Exec exec]") | |
parser.add_option("-s", "--size", dest="icon_size", type="int", | |
default=24, help="Default icon size [24]") | |
parser.add_option("-f", "--force", action="store_true", dest="force", | |
default=False, | |
help="Force icon size (requires imagemagick and writes \ | |
into ICON_DIR)") | |
parser.add_option("-i", "--icon-dir", dest="icon_dir", type="string", | |
default="~/.fvwm/icons", | |
help="Directory for converted icons [~/.fvwm/icons]") | |
parser.add_option("-t", "--theme", dest="theme", type="string", | |
default="gnome", | |
help="Icon theme [gnome]") | |
parser.add_option("-m", "--top-menu", dest="top", type="string", | |
default="", | |
help="Top menu name") | |
options, args = parser.parse_args() | |
def printtext(text): | |
print text.encode("utf-8") | |
def geticonfile(icon, size=options.icon_size, theme=options.theme): | |
iconpath = xdg.IconTheme.getIconPath(icon, size, theme, ["png", "xpm"]) | |
if not iconpath: | |
return None | |
logging.debug('iconpath:'+iconpath) | |
# the iconpath maybe just does not exist! | |
if not os.path.exists(iconpath): | |
logging.warning('iconpath %s does not exist!' % iconpath) | |
return None | |
# The fallback hicolor will ignore type argument, | |
# so svg format might as well be returned. | |
if os.path.splitext(iconpath)[1][1:] == 'svg': | |
return '%s:%sx%s' % (iconpath, size, size) | |
if not options.force: | |
return iconpath | |
if iconpath.find("%ix%i" % (size, size)) >= 0: # ugly hack!!! | |
return iconpath | |
iconfile = os.path.join(os.path.expanduser(options.icon_dir), | |
"%ix%i-" % (size, size) + | |
os.path.basename(iconpath)) | |
os.system("if test \\( ! -f '%s' \\) -o \\( '%s' -nt '%s' \\) ; then convert '%s' -resize %i '%s' ; fi"% | |
(iconfile, iconpath, iconfile, iconpath, size, iconfile)) | |
logging.info('convert %s to %s' % (iconpath, iconfile)) | |
return iconfile | |
def getdefaulticonfile(command): | |
if command.startswith("Popup"): | |
return geticonfile("gnome-fs-directory") | |
else: | |
return geticonfile("gnome-applications") | |
def printmenu(name, icon, command): | |
if not isinstance(name, unicode): | |
name = unicode(name, 'utf-8') | |
if isinstance(icon, unicode): | |
icon = icon.encode('utf-8') | |
if not isinstance(command, unicode): | |
command = unicode(command, 'utf-8') | |
iconfile = geticonfile(icon) or getdefaulticonfile(command) or icon | |
printtext(u'+ "%s%%%s%%" %s' % (name, iconfile, command)) | |
def parsemenu(menu, name=""): | |
if not name: | |
name = menu.getPath() | |
# print 'DestroyMenu "%s"' % name | |
printtext('AddToMenu "%s"' % name) | |
for entry in menu.getEntries(): | |
if isinstance(entry, xdg.Menu.Menu): | |
printmenu(entry.getName(), entry.getIcon(), | |
'Popup "%s"' % entry.getPath()) | |
elif isinstance(entry, xdg.Menu.MenuEntry): | |
desktop = DesktopEntry(entry.DesktopEntry.getFileName()) | |
printmenu(desktop.getName(), desktop.getIcon(), | |
options.exec_command + " " + desktop.getExec()) | |
else: | |
printtext('# not supported: ' + str(entry)) | |
for entry in menu.getEntries(): | |
if isinstance(entry, xdg.Menu.Menu): | |
parsemenu(entry) | |
for arg in args: | |
print '#', ' '.join(sys.argv) | |
print '# %s' % arg | |
parsemenu(xdg.Menu.parse(arg), options.top) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment