.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.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
506
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     1
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     2
import sys
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     3
import os
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     4
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     5
import xdg.Menu
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     6
from xdg.DesktopEntry import *
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     7
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     8
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     9
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    10
# For testing purpose. Uncomment to run this file without args...
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    11
# sys.argv.append('debian-menu')
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    12
518
3dd43161fc0e Pass full path to menu file. Also pass Fvwm menu entry name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 517
diff changeset
    13
if len(sys.argv) >= 3:
3dd43161fc0e Pass full path to menu file. Also pass Fvwm menu entry name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 517
diff changeset
    14
    # xdg_config_dirs hold possible paths. We use Debian one.
3dd43161fc0e Pass full path to menu file. Also pass Fvwm menu entry name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 517
diff changeset
    15
    fvwm_menu_name = sys.argv[1]
3dd43161fc0e Pass full path to menu file. Also pass Fvwm menu entry name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 517
diff changeset
    16
    sys_menu_file = sys.argv[2]
506
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    17
else:
518
3dd43161fc0e Pass full path to menu file. Also pass Fvwm menu entry name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 517
diff changeset
    18
    print("You forget pass NAME which is like /etc/xdg/NAME.menu")
506
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    19
    exit(1)
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    20
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    21
sys_menu = xdg.Menu.parse(sys_menu_file)
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    22
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    23
def parse_menu(menu, name):
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    24
    ''' Parse menu file. '''
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    25
    print 'DestroyMenu recreate %s' % name
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    26
    for entry in menu.getEntries():
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    27
        if isinstance(entry, xdg.Menu.Menu):
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    28
            icon = entry.getIcon()
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    29
            subname = name+"-"+str(entry)
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    30
            print u'AddToMenu "%s" "%s%%folder.png%%" Popup "%s"' % (name, entry.getName(), subname)
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    31
            parse_menu(entry, subname)
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    32
        elif isinstance(entry, xdg.Menu.MenuEntry):
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    33
            desktop = DesktopEntry(entry.DesktopEntry.getFileName())
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    34
            icon = desktop.getIcon()
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    35
            cmd = desktop.getExec().rstrip('%FUfu')
517
de05fae3311f Use full path to images. Uncomment icon paths.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 506
diff changeset
    36
            if icon == '':
de05fae3311f Use full path to images. Uncomment icon paths.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 506
diff changeset
    37
                print u'AddToMenu "%s" "%s" Exec exec %s' % (name, desktop.getName(), cmd)
de05fae3311f Use full path to images. Uncomment icon paths.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 506
diff changeset
    38
            else:
522
499d9a727fa1 Fix condition for icon without extension.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 521
diff changeset
    39
                if icon.rfind('/') == -1 and icon.rfind('.') == -1:
531
89812bfac67c Use XPM files instead of PNG to prevent loading large icons.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 522
diff changeset
    40
                    icon = icon + ".xpm"
517
de05fae3311f Use full path to images. Uncomment icon paths.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 506
diff changeset
    41
                print u'AddToMenu "%s" "%s%%%s%%" Exec exec %s' % (name, desktop.getName(), icon, cmd)
506
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    42
        else:
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    43
            pass
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    44
518
3dd43161fc0e Pass full path to menu file. Also pass Fvwm menu entry name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 517
diff changeset
    45
parse_menu(sys_menu, fvwm_menu_name)
506
0ab510cb567d Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    46