author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Fri, 08 Jan 2021 13:59:23 +0200 | |
changeset 968 | dc543d247f29 |
parent 657 | e80fe57c9750 |
permissions | -rw-r--r-- |
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 |
641
1ef81c4fd12c
Fix: NameError: name 'codecs' is not defined.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
531
diff
changeset
|
4 |
import codecs |
506
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
5 |
|
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
6 |
import xdg.Menu |
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
7 |
from xdg.DesktopEntry import * |
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
8 |
|
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
9 |
sys.stdout = codecs.getwriter('utf-8')(sys.stdout) |
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
10 |
|
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
11 |
# 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
|
12 |
# sys.argv.append('debian-menu') |
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
13 |
|
518
3dd43161fc0e
Pass full path to menu file. Also pass Fvwm menu entry name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
517
diff
changeset
|
14 |
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
|
15 |
# 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
|
16 |
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
|
17 |
sys_menu_file = sys.argv[2] |
506
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
18 |
else: |
518
3dd43161fc0e
Pass full path to menu file. Also pass Fvwm menu entry name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
517
diff
changeset
|
19 |
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
|
20 |
exit(1) |
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
21 |
|
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
22 |
sys_menu = xdg.Menu.parse(sys_menu_file) |
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
23 |
|
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
24 |
def parse_menu(menu, name): |
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
25 |
''' Parse menu file. ''' |
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
26 |
print 'DestroyMenu recreate %s' % name |
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
27 |
for entry in menu.getEntries(): |
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
28 |
if isinstance(entry, xdg.Menu.Menu): |
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
29 |
icon = entry.getIcon() |
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
30 |
subname = name+"-"+str(entry) |
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
31 |
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
|
32 |
parse_menu(entry, subname) |
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
33 |
elif isinstance(entry, xdg.Menu.MenuEntry): |
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
34 |
desktop = DesktopEntry(entry.DesktopEntry.getFileName()) |
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
35 |
icon = desktop.getIcon() |
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
36 |
cmd = desktop.getExec().rstrip('%FUfu') |
517
de05fae3311f
Use full path to images. Uncomment icon paths.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
506
diff
changeset
|
37 |
if icon == '': |
de05fae3311f
Use full path to images. Uncomment icon paths.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
506
diff
changeset
|
38 |
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
|
39 |
else: |
522
499d9a727fa1
Fix condition for icon without extension.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
521
diff
changeset
|
40 |
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
|
41 |
icon = icon + ".xpm" |
657
e80fe57c9750
Use new syntax for setting SVG icon size to prevent huge icons in menu.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
641
diff
changeset
|
42 |
if icon.endswith('.svg'): |
e80fe57c9750
Use new syntax for setting SVG icon size to prevent huge icons in menu.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
641
diff
changeset
|
43 |
icon = icon + ":32x32" |
517
de05fae3311f
Use full path to images. Uncomment icon paths.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
506
diff
changeset
|
44 |
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
|
45 |
else: |
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
46 |
pass |
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
47 |
|
518
3dd43161fc0e
Pass full path to menu file. Also pass Fvwm menu entry name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
517
diff
changeset
|
48 |
parse_menu(sys_menu, fvwm_menu_name) |
506
0ab510cb567d
Add xdg to Fvwm menus converter.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
49 |