.fvwm/xdg2fvwm.py
author Oleksandr Gavenko <gavenkoa@gmail.com>
Fri, 07 Apr 2017 18:48:40 +0300
changeset 851 bfeb801ace0e
parent 657 e80fe57c9750
permissions -rw-r--r--
Fix moving window down.


import sys
import os
import codecs

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"
                if icon.endswith('.svg'):
                    icon = icon + ":32x32"
                print u'AddToMenu "%s" "%s%%%s%%" Exec exec %s' % (name, desktop.getName(), icon, cmd)
        else:
            pass

parse_menu(sys_menu, fvwm_menu_name)