.fvwm/xdg2fvwm.py
author Oleksandr Gavenko <gavenkoa@gmail.com>
Thu, 30 Aug 2012 15:40:51 +0300
changeset 531 89812bfac67c
parent 522 499d9a727fa1
child 641 1ef81c4fd12c
permissions -rw-r--r--
Use XPM files instead of PNG to prevent loading large icons.


import sys
import os

import xdg.Menu
from xdg.DesktopEntry import *

sys.stdout = codecs.getwriter('utf-8')(sys.stdout)

# For testing purpose. Uncomment to run this file without args...
# sys.argv.append('debian-menu')

if len(sys.argv) >= 3:
    # xdg_config_dirs hold possible paths. We use Debian one.
    fvwm_menu_name = sys.argv[1]
    sys_menu_file = sys.argv[2]
else:
    print("You forget pass NAME which is like /etc/xdg/NAME.menu")
    exit(1)

sys_menu = xdg.Menu.parse(sys_menu_file)

def parse_menu(menu, name):
    ''' Parse menu file. '''
    print 'DestroyMenu recreate %s' % name
    for entry in menu.getEntries():
        if isinstance(entry, xdg.Menu.Menu):
            icon = entry.getIcon()
            subname = name+"-"+str(entry)
            print u'AddToMenu "%s" "%s%%folder.png%%" Popup "%s"' % (name, entry.getName(), subname)
            parse_menu(entry, subname)
        elif isinstance(entry, xdg.Menu.MenuEntry):
            desktop = DesktopEntry(entry.DesktopEntry.getFileName())
            icon = desktop.getIcon()
            cmd = desktop.getExec().rstrip('%FUfu')
            if icon == '':
                print u'AddToMenu "%s" "%s" Exec exec %s' % (name, desktop.getName(), cmd)
            else:
                if icon.rfind('/') == -1 and icon.rfind('.') == -1:
                    icon = icon + ".xpm"
                print u'AddToMenu "%s" "%s%%%s%%" Exec exec %s' % (name, desktop.getName(), icon, cmd)
        else:
            pass

parse_menu(sys_menu, fvwm_menu_name)