Browse Source

Replace global variables with a dictionary

It's better organized this way.
pull/30/head 2.2
George Vlahavas 5 months ago
parent
commit
9227cf1605
  1. 157
      src/xdgmenumaker

157
src/xdgmenumaker

@ -19,14 +19,16 @@ pygtkcompat.enable()
pygtkcompat.enable_gtk(version='3.0') pygtkcompat.enable_gtk(version='3.0')
import gtk import gtk
seticon = False xopts = {
iconsize = 16 'seticon': False,
nosvg = False 'iconsize': 16,
desktop = False 'nosvg': False,
submenu = True 'desktop': False,
pekwmdynamic = False 'submenu': True,
twmtitles = False 'pekwmdynamic': False,
max_icon_size = False 'twmtitles': False,
'max_icon_size': False
}
# the following line gets changed by the Makefile. If it is set to # the following line gets changed by the Makefile. If it is set to
# 'not_set' it looks in the currect directory tree for the .directory # 'not_set' it looks in the currect directory tree for the .directory
@ -154,14 +156,7 @@ if not terminal_app:
def main(argv): def main(argv):
global desktop global xopts
global seticon
global iconsize
global nosvg
global submenu
global pekwmdynamic
global twmtitles
global max_icon_size
try: try:
opts, args = getopt.getopt(argv, "hins:f:", ["help", "icons", opts, args = getopt.getopt(argv, "hins:f:", ["help", "icons",
"no-submenu", "no-submenu",
@ -179,19 +174,19 @@ def main(argv):
usage() usage()
sys.exit(0) sys.exit(0)
elif opt in ("-i", "--icons"): elif opt in ("-i", "--icons"):
seticon = True xopts['seticon'] = True
elif opt in ("-s", "--size"): elif opt in ("-s", "--size"):
try: try:
iconsize = int(arg) xopts['iconsize'] = int(arg)
except ValueError: except ValueError:
usage() usage()
sys.exit('ERROR: size must be a number') sys.exit('ERROR: size must be a number')
elif opt in ("-n", "--no-submenu"): elif opt in ("-n", "--no-submenu"):
submenu = False xopts['submenu'] = False
elif opt in ("--pekwm-dynamic",): elif opt in ("--pekwm-dynamic",):
pekwmdynamic = True xopts['pekwmdynamic'] = True
elif opt in ("--twm-titles",): elif opt in ("--twm-titles",):
twmtitles = True xopts['twmtitles'] = True
elif opt in ("--max-icon-size",): elif opt in ("--max-icon-size",):
try: try:
# Pillow is optional and loaded only if we want to restrict the # Pillow is optional and loaded only if we want to restrict the
@ -202,38 +197,38 @@ def main(argv):
# slows down when it is actually needed. # slows down when it is actually needed.
global Image global Image
from PIL import Image from PIL import Image
max_icon_size = True xopts['max_icon_size'] = True
except ImportError: except ImportError:
usage() usage()
sys.exit('ERROR: --max-icon-size requires Pillow') sys.exit('ERROR: --max-icon-size requires Pillow')
elif opt == "--no-svg": elif opt == "--no-svg":
nosvg = True xopts['nosvg'] = True
elif opt in ("-f", "--format"): elif opt in ("-f", "--format"):
desktop = arg xopts['desktop'] = arg
if not desktop: if not xopts['desktop']:
usage() usage()
sys.exit('ERROR: You must specify the output format with -f') sys.exit('ERROR: You must specify the output format with -f')
elif desktop == "blackbox": elif xopts['desktop'] == "blackbox":
blackbox() blackbox()
elif desktop == "fluxbox": elif xopts['desktop'] == "fluxbox":
fluxbox() fluxbox()
elif desktop == "fvwm": elif xopts['desktop'] == "fvwm":
fvwm() fvwm()
elif desktop == "windowmaker": elif xopts['desktop'] == "windowmaker":
seticon = False xopts['seticon'] = False
windowmaker() windowmaker()
elif desktop == "icewm": elif xopts['desktop'] == "icewm":
icewm() icewm()
elif desktop == "pekwm": elif xopts['desktop'] == "pekwm":
pekwmmenu() pekwmmenu()
elif desktop == "jwm": elif xopts['desktop'] == "jwm":
jwm() jwm()
elif desktop == "compizboxmenu": elif xopts['desktop'] == "compizboxmenu":
compizboxmenu() compizboxmenu()
elif desktop == "twm": elif xopts['desktop'] == "twm":
twm() twm()
elif desktop == "amiwm": elif xopts['desktop'] == "amiwm":
seticon = False xopts['seticon'] = False
amiwm() amiwm()
else: else:
usage() usage()
@ -283,7 +278,7 @@ def icon_max_size(icon):
except: except:
# if there is any error reading the icon, just discard it # if there is any error reading the icon, just discard it
return None return None
if img.size[0] <= iconsize: if img.size[0] <= xopts['iconsize']:
return icon return icon
return None return None
@ -296,11 +291,11 @@ def icon_full_path(icon):
if os.path.exists(icon): if os.path.exists(icon):
if ext == ".svg" or ext == ".svgz": if ext == ".svg" or ext == ".svgz":
# only jwm and icewm support svg icons # only jwm and icewm support svg icons
if (desktop == "jwm" or desktop == "icewm") and not nosvg: if (xopts['desktop'] == "jwm" or xopts['desktop'] == "icewm") and not xopts['nosvg']:
return icon return icon
else: else:
# icon is not svg # icon is not svg
if max_icon_size: if xopts['max_icon_size']:
return icon_max_size(icon) return icon_max_size(icon)
else: else:
return icon return icon
@ -309,17 +304,17 @@ def icon_full_path(icon):
icon_theme = gtk.icon_theme_get_default() icon_theme = gtk.icon_theme_get_default()
try: try:
# JWM and IceWM support svg icons # JWM and IceWM support svg icons
if (desktop == "jwm" or desktop == "icewm") and not nosvg: if (xopts['desktop'] == "jwm" or xopts['desktop'] == "icewm") and not xopts['nosvg']:
icon = icon_theme.lookup_icon(icon, iconsize, gtk.ICON_LOOKUP_FORCE_SVG) icon = icon_theme.lookup_icon(icon, xopts['iconsize'], gtk.ICON_LOOKUP_FORCE_SVG)
# but none of the other WMs does # but none of the other WMs does
else: else:
icon = icon_theme.lookup_icon(icon, iconsize, gtk.ICON_LOOKUP_NO_SVG) icon = icon_theme.lookup_icon(icon, xopts['iconsize'], gtk.ICON_LOOKUP_NO_SVG)
except AttributeError: except AttributeError:
sys.exit('ERROR: You need to run xdgmenumaker inside an X session.') sys.exit('ERROR: You need to run xdgmenumaker inside an X session.')
if icon: if icon:
icon = icon.get_filename() icon = icon.get_filename()
# icon size only matters for non-SVG icons # icon size only matters for non-SVG icons
if icon and max_icon_size and (ext != ".svg" or ext != ".svgz"): if icon and xopts['max_icon_size'] and (ext != ".svg" or ext != ".svgz"):
icon = icon_max_size(icon) icon = icon_max_size(icon)
return icon return icon
@ -395,14 +390,14 @@ def get_entry_info(desktopfile, ico_paths=True):
# none of the freedesktop registered environments are supported by # none of the freedesktop registered environments are supported by
# OnlyShowIn but it might be worth using some extra logic here. # OnlyShowIn but it might be worth using some extra logic here.
# http://standards.freedesktop.org/menu-spec/latest/apb.html # http://standards.freedesktop.org/menu-spec/latest/apb.html
if (onlyshowin != [] and not (desktop.lower() in (name.lower() for name in onlyshowin))) \ if (onlyshowin != [] and not (xopts['desktop'].lower() in (name.lower() for name in onlyshowin))) \
or (desktop.lower() in (name.lower() for name in notshowin)) \ or (xopts['desktop'].lower() in (name.lower() for name in notshowin)) \
or hidden or nodisplay: or hidden or nodisplay:
return None return None
name = de.getName().encode('utf-8') name = de.getName().encode('utf-8')
if seticon: if xopts['seticon']:
icon = de.getIcon() icon = de.getIcon()
if ico_paths: if ico_paths:
icon = icon_full_path(icon) icon = icon_full_path(icon)
@ -525,15 +520,15 @@ def category_icon(category):
def blackbox(): def blackbox():
# Blackbox menus are the same as Fluxbox menus. They just don't support # Blackbox menus are the same as Fluxbox menus. They just don't support
# icons. # icons.
global seticon global xopts
seticon = False xopts['seticon'] = False
fluxbox() fluxbox()
def fluxbox(): def fluxbox():
if submenu: if xopts['submenu']:
spacing = ' ' spacing = ' '
if seticon: if xopts['seticon']:
app_icon = icon_full_path(applications_icon) app_icon = icon_full_path(applications_icon)
if app_icon is None: if app_icon is None:
@ -547,7 +542,7 @@ def fluxbox():
for menu_category in menu(): for menu_category in menu():
category = menu_category.category category = menu_category.category
cat_name = category.decode() cat_name = category.decode()
if seticon: if xopts['seticon']:
cat_icon = category_icon(category) cat_icon = category_icon(category)
cat_icon = icon_full_path(cat_icon) cat_icon = icon_full_path(cat_icon)
if cat_icon: if cat_icon:
@ -575,15 +570,15 @@ def fluxbox():
c=command, c=command,
i=icon)) i=icon))
print('{s}[end] # ({c})'.format(s=spacing, c=cat_name)) print('{s}[end] # ({c})'.format(s=spacing, c=cat_name))
if submenu: if xopts['submenu']:
print('[end] # ({})'.format(apps_name)) print('[end] # ({})'.format(apps_name))
def fvwm(): def fvwm():
if submenu: if xopts['submenu']:
print('DestroyMenu "xdgmenu"') print('DestroyMenu "xdgmenu"')
print('AddToMenu "xdgmenu"') print('AddToMenu "xdgmenu"')
if seticon: if xopts['seticon']:
app_icon = icon_full_path(applications_icon) app_icon = icon_full_path(applications_icon)
if app_icon: if app_icon:
print('+ "{a}%{i}% " Title'.format(a=apps_name, i=app_icon)) print('+ "{a}%{i}% " Title'.format(a=apps_name, i=app_icon))
@ -611,7 +606,7 @@ def fvwm():
cat_name = category.decode() cat_name = category.decode()
print('DestroyMenu "{}"'.format(cat_name)) print('DestroyMenu "{}"'.format(cat_name))
print('AddToMenu "{c}"'.format(c=cat_name)) print('AddToMenu "{c}"'.format(c=cat_name))
if seticon: if xopts['seticon']:
cat_icon = category_icon(category) cat_icon = category_icon(category)
cat_icon = icon_full_path(cat_icon) cat_icon = icon_full_path(cat_icon)
if cat_icon: if cat_icon:
@ -651,9 +646,9 @@ def windowmaker():
def icewm(): def icewm():
if submenu: if xopts['submenu']:
spacing = ' ' spacing = ' '
if seticon: if xopts['seticon']:
app_icon = icon_full_path(applications_icon) app_icon = icon_full_path(applications_icon)
if app_icon is None: if app_icon is None:
app_icon = "_none_" app_icon = "_none_"
@ -667,7 +662,7 @@ def icewm():
cat_name = category.decode() cat_name = category.decode()
cat_icon = category_icon(category) cat_icon = category_icon(category)
cat_icon = icon_full_path(cat_icon) cat_icon = icon_full_path(cat_icon)
if seticon and cat_icon is not None: if xopts['seticon'] and cat_icon is not None:
print('{s}menu "{c}" {i} {{'.format(s=spacing, c=cat_name, print('{s}menu "{c}" {i} {{'.format(s=spacing, c=cat_name,
i=cat_icon)) i=cat_icon))
else: else:
@ -676,26 +671,26 @@ def icewm():
name = app.name.decode() name = app.name.decode()
icon = app.icon icon = app.icon
command = app.command command = app.command
if seticon and icon is not None: if xopts['seticon'] and icon is not None:
print('{s} prog "{n}" {i} {c}'.format(s=spacing, n=name, print('{s} prog "{n}" {i} {c}'.format(s=spacing, n=name,
i=icon, c=command)) i=icon, c=command))
else: else:
print('{s} prog "{n}" _none_ {c}'.format(s=spacing, n=name, print('{s} prog "{n}" _none_ {c}'.format(s=spacing, n=name,
c=command)) c=command))
print('{}}}'.format(spacing)) print('{}}}'.format(spacing))
if submenu: if xopts['submenu']:
print('}') print('}')
def pekwmmenu(): def pekwmmenu():
if pekwmdynamic: if xopts['pekwmdynamic']:
print("Dynamic {") print("Dynamic {")
dspacing = ' ' dspacing = ' '
else: else:
dspacing = '' dspacing = ''
if submenu: if xopts['submenu']:
spacing = ' ' spacing = ' '
if seticon: if xopts['seticon']:
app_icon = icon_full_path(applications_icon) app_icon = icon_full_path(applications_icon)
print('{s}Submenu = "{a}" {{ Icon = "{i}"'.format(s=dspacing, print('{s}Submenu = "{a}" {{ Icon = "{i}"'.format(s=dspacing,
a=apps_name, a=apps_name,
@ -709,7 +704,7 @@ def pekwmmenu():
cat_name = category.decode() cat_name = category.decode()
cat_icon = category_icon(category) cat_icon = category_icon(category)
cat_icon = icon_full_path(cat_icon) cat_icon = icon_full_path(cat_icon)
if seticon and cat_icon is not None: if xopts['seticon'] and cat_icon is not None:
print('{d}{s}Submenu = "{c}" {{ Icon = "{i}"'.format(d=dspacing, print('{d}{s}Submenu = "{c}" {{ Icon = "{i}"'.format(d=dspacing,
s=spacing, s=spacing,
c=cat_name, c=cat_name,
@ -731,7 +726,7 @@ def pekwmmenu():
# with "&&" and "||", so we'll launch the command even if the # with "&&" and "||", so we'll launch the command even if the
# path does not exist # path does not exist
command = 'cd {p} && {c} || {c}'.format(p=path, c=command) command = 'cd {p} && {c} || {c}'.format(p=path, c=command)
if seticon and icon is not None: if xopts['seticon'] and icon is not None:
print('{d}{s} Entry = "{n}" {{ Icon = "{i}"; Actions = "Exec {c} &" }}' print('{d}{s} Entry = "{n}" {{ Icon = "{i}"; Actions = "Exec {c} &" }}'
.format(d=dspacing, s=spacing, n=name, i=icon, .format(d=dspacing, s=spacing, n=name, i=icon,
c=command)) c=command))
@ -739,18 +734,18 @@ def pekwmmenu():
print('{d}{s} Entry = "{n}" {{ Actions = "Exec {c} &" }}' print('{d}{s} Entry = "{n}" {{ Actions = "Exec {c} &" }}'
.format(d=dspacing, s=spacing, n=name, c=command)) .format(d=dspacing, s=spacing, n=name, c=command))
print('{d}{s}}}'.format(d=dspacing, s=spacing)) print('{d}{s}}}'.format(d=dspacing, s=spacing))
if submenu: if xopts['submenu']:
print('{}}}'.format(dspacing)) print('{}}}'.format(dspacing))
if pekwmdynamic: if xopts['pekwmdynamic']:
print("}") print("}")
def jwm(): def jwm():
print('<?xml version="1.0"?>') print('<?xml version="1.0"?>')
print('<JWM>') print('<JWM>')
if submenu: if xopts['submenu']:
spacing = ' ' spacing = ' '
if seticon: if xopts['seticon']:
app_icon = icon_full_path(applications_icon) app_icon = icon_full_path(applications_icon)
if app_icon is None: if app_icon is None:
print('<Menu label="{}">'.format(apps_name)) print('<Menu label="{}">'.format(apps_name))
@ -766,7 +761,7 @@ def jwm():
cat_name = category.decode() cat_name = category.decode()
cat_icon = category_icon(category) cat_icon = category_icon(category)
cat_icon = icon_full_path(cat_icon) cat_icon = icon_full_path(cat_icon)
if seticon and cat_icon is not None: if xopts['seticon'] and cat_icon is not None:
print('{s}<Menu icon="{i}" label="{c}">'.format(s=spacing, print('{s}<Menu icon="{i}" label="{c}">'.format(s=spacing,
i=cat_icon, i=cat_icon,
c=cat_name)) c=cat_name))
@ -779,22 +774,22 @@ def jwm():
path = app.path path = app.path
if path is not None: if path is not None:
command = 'cd {p} ; {c}'.format(p=path, c=command) command = 'cd {p} ; {c}'.format(p=path, c=command)
if seticon and icon is not None: if xopts['seticon'] and icon is not None:
print('{s} <Program icon="{i}" label="{n}">{c}</Program>' print('{s} <Program icon="{i}" label="{n}">{c}</Program>'
.format(s=spacing, i=icon, n=name, c=command)) .format(s=spacing, i=icon, n=name, c=command))
else: else:
print('{s} <Program label="{n}">{c}</Program>' print('{s} <Program label="{n}">{c}</Program>'
.format(s=spacing, n=name, c=command)) .format(s=spacing, n=name, c=command))
print('{}</Menu>'.format(spacing)) print('{}</Menu>'.format(spacing))
if submenu: if xopts['submenu']:
print('</Menu>') print('</Menu>')
print('</JWM>') print('</JWM>')
def compizboxmenu(): def compizboxmenu():
if submenu: if xopts['submenu']:
spacing = ' ' spacing = ' '
if seticon: if xopts['seticon']:
app_icon = icon_strip(applications_icon) app_icon = icon_strip(applications_icon)
print('<menu icon="{i}" name="{a}">'.format(i=app_icon, print('<menu icon="{i}" name="{a}">'.format(i=app_icon,
a=apps_name)) a=apps_name))
@ -806,7 +801,7 @@ def compizboxmenu():
category = menu_category.category category = menu_category.category
cat_name = category.decode() cat_name = category.decode()
cat_icon = category_icon(category) cat_icon = category_icon(category)
if seticon and cat_icon is not None: if xopts['seticon'] and cat_icon is not None:
print('{s}<menu icon="{i}" name="{c}">'.format( print('{s}<menu icon="{i}" name="{c}">'.format(
s=spacing, i=cat_icon, c=cat_name)) s=spacing, i=cat_icon, c=cat_name))
else: else:
@ -819,7 +814,7 @@ def compizboxmenu():
if path is not None: if path is not None:
path = path.replace("'", "'\\''") path = path.replace("'", "'\\''")
command = 'sh -c \'cd "{p}" ;{c}\''.format(p=path, c=command) command = 'sh -c \'cd "{p}" ;{c}\''.format(p=path, c=command)
if seticon and icon is not None: if xopts['seticon'] and icon is not None:
print(('{s} <item type="launcher"><name>{n}</name>' print(('{s} <item type="launcher"><name>{n}</name>'
'<icon>{i}</icon>' '<icon>{i}</icon>'
'<command>{c}</command></item>').format(s=spacing, '<command>{c}</command></item>').format(s=spacing,
@ -831,14 +826,14 @@ def compizboxmenu():
n=name, n=name,
c=command)) c=command))
print('{}</menu>'.format(spacing)) print('{}</menu>'.format(spacing))
if submenu: if xopts['submenu']:
print('</menu>') print('</menu>')
def twm(): def twm():
if submenu: if xopts['submenu']:
print('menu "xdgmenu"') print('menu "xdgmenu"')
print("{") print("{")
if twmtitles: if xopts['twmtitles']:
print(' "{a}" f.title'.format(a=apps_name)) print(' "{a}" f.title'.format(a=apps_name))
for menu_category in menu(): for menu_category in menu():
category = menu_category.category category = menu_category.category
@ -850,7 +845,7 @@ def twm():
cat_name = category.decode() cat_name = category.decode()
print('menu "{}"'.format(cat_name)) print('menu "{}"'.format(cat_name))
print("{") print("{")
if twmtitles: if xopts['twmtitles']:
print(' "{c}" f.title'.format(c=cat_name)) print(' "{c}" f.title'.format(c=cat_name))
for app in menu_category.applist: for app in menu_category.applist:
name = app.name.decode() name = app.name.decode()

Loading…
Cancel
Save