Browse Source

Use str.format() for string formatting

Replace string concatenation with '+' with str.format()
pull/8/head
George Vlahavas 9 years ago
parent
commit
66eb271433
  1. 162
      src/xdgmenumaker

162
src/xdgmenumaker

@ -28,10 +28,10 @@ prefix = 'not_set'
if prefix == 'not_set': if prefix == 'not_set':
desktop_dir = '../desktop-directories/' desktop_dir = '../desktop-directories/'
else: else:
desktop_dir = prefix + '/share/desktop-directories/' desktop_dir = '{}/share/desktop-directories/'.format(prefix)
if not os.path.isdir(desktop_dir): if not os.path.isdir(desktop_dir):
sys.exit('ERROR: Could not find ' + desktop_dir) sys.exit('ERROR: Could not find {}'.format(desktop_dir))
class App: class App:
@ -277,7 +277,7 @@ def get_entry_info(desktopfile, ico_paths=True):
terminal = de.getTerminal() terminal = de.getTerminal()
if terminal: if terminal:
command = terminal_app + ' -e ' + command command = '{term} -e {cmd}'.format(term=terminal_app, cmd=command)
path = de.getPath() path = de.getPath()
if not path: if not path:
@ -344,7 +344,7 @@ def desktopfilelist():
filelist = [] filelist = []
df_temp = [] df_temp = []
for d in dirs: for d in dirs:
xdgdir = d + '/applications' xdgdir = '{}/applications'.format(d)
if os.path.isdir(xdgdir): if os.path.isdir(xdgdir):
for i in os.listdir(xdgdir): for i in os.listdir(xdgdir):
if i.endswith('.desktop'): if i.endswith('.desktop'):
@ -355,7 +355,7 @@ def desktopfilelist():
# /usr/share/applications # /usr/share/applications
if i not in df_temp: if i not in df_temp:
df_temp.append(i) df_temp.append(i)
filelist.append(xdgdir + '/' + i) filelist.append('{}/{}'.format(xdgdir, i))
return filelist return filelist
@ -422,11 +422,11 @@ def fluxbox():
if seticon: if 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('[submenu] (' + applications + ')') print('[submenu] ({})'.format(applications))
else: else:
print('[submenu] (' + applications + ') <' + app_icon + '>') print('[submenu] ({}) <{}>'.format(applications, app_icon))
else: else:
print('[submenu] (' + applications + ')') print('[submenu] ({})'.format(applications))
else: else:
spacing = '' spacing = ''
for menu_category in menu(): for menu_category in menu():
@ -435,12 +435,12 @@ def fluxbox():
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:
print(spacing + '[submenu] (' + print('{s}[submenu] ({c}) <{i}>'.format(s=spacing, c=category,
category + ') <' + cat_icon + '>') i=cat_icon))
else: else:
print(spacing + '[submenu] (' + category + ')') print('{s}[submenu] ({c})'.format(s=spacing, c=category))
else: else:
print(spacing + '[submenu] (' + category + ')') print('{s}[submenu] ({c})'.format(s=spacing, c=category))
for app in menu_category.applist: for app in menu_category.applist:
# closing parentheses need to be escaped, otherwise they are # closing parentheses need to be escaped, otherwise they are
# cropped out, along with everything that comes after them # cropped out, along with everything that comes after them
@ -449,28 +449,31 @@ def fluxbox():
command = app.command command = app.command
path = app.path path = app.path
if path is not None: if path is not None:
command = 'cd ' + path + ' ; ' + command command = 'cd {p} ; {c}'.format(p=path, c=command)
if icon is None: if icon is None:
print(spacing + ' [exec] (' + name + ') {' + command + '}') print('{s} [exec] ({n}) {{{c}}}'.format(s=spacing, n=name,
else: c=command))
print(spacing + ' [exec] (' + name + else:
') {' + command + '} <' + icon + '>') print('{s} [exec] ({n}) {{{c}}} <{i}>'.format(s=spacing,
print(spacing + '[end] # (' + category + ')') n=name,
c=command,
i=icon))
print('{s}[end] # ({c})'.format(s=spacing, c=category))
if submenu: if submenu:
print('[end] # (' + applications + ')') print('[end] # ({})'.format(applications))
def windowmaker(): def windowmaker():
print('"' + applications + '" MENU') print('"{}" MENU'.format(applications))
for menu_category in menu(): for menu_category in menu():
category = menu_category.category category = menu_category.category
print(' "' + category + '" MENU') print(' "{}" MENU'.format(category))
for app in menu_category.applist: for app in menu_category.applist:
name = app.name name = app.name
command = app.command command = app.command
print(' "' + name + '" EXEC ' + command) print(' "{n}" EXEC {c}'.format(n=name, c=command))
print(' "' + category + '" END') print(' "{}" END'.format(category))
print('"' + applications + '" END') print('"{}" END'.format(applications))
def icewm(): def icewm():
@ -480,9 +483,9 @@ def icewm():
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_"
print('menu "' + applications + '" ' + app_icon + ' {') print('menu "{a}" {i} {{'.format(a=applications, i=app_icon))
else: else:
print('menu "' + applications + '" _none_ {') print('menu "{}" _none_ {{'.format(applications))
else: else:
spacing = '' spacing = ''
for menu_category in menu(): for menu_category in menu():
@ -490,19 +493,21 @@ def icewm():
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 seticon and cat_icon is not None:
print(spacing + 'menu "' + category + '" ' + cat_icon + ' {') print('{s}menu "{c}" {i} {{'.format(s=spacing, c=category,
i=cat_icon))
else: else:
print(spacing + 'menu "' + category + '" _none_ {') print('{s}menu "{c}" _none_ {{'.format(s=spacing, c=category))
for app in menu_category.applist: for app in menu_category.applist:
name = app.name name = app.name
icon = app.icon icon = app.icon
command = app.command command = app.command
if seticon and icon is not None: if seticon and icon is not None:
print(spacing + ' prog "' + name + print('{s} prog "{n}" {i} {c}'.format(s=spacing, n=name,
'" ' + icon + ' ' + command) i=icon, c=command))
else: else:
print(spacing + ' prog "' + name + '" _none_ ' + command) print('{s} prog "{n}" _none_ {c}'.format(s=spacing, n=name,
print(spacing + '}') c=command))
print('{}}}'.format(spacing))
if submenu: if submenu:
print('}') print('}')
@ -517,10 +522,11 @@ def pekwmmenu():
spacing = ' ' spacing = ' '
if seticon: if seticon:
app_icon = icon_full_path(applications_icon) app_icon = icon_full_path(applications_icon)
print(dspacing + 'Submenu = "' + applications + print('{s}Submenu = "{a}" {{ Icon = "{i}"'.format(s=dspacing,
'" { Icon = "' + app_icon + '"') a=applications,
i=app_icon))
else: else:
print(dspacing + 'Submenu = "' + applications + '" {') print('{s}Submenu = "{a}" {{'.format(s=dspacing, a=applications))
else: else:
spacing = '' spacing = ''
for menu_category in menu(): for menu_category in menu():
@ -528,10 +534,13 @@ def pekwmmenu():
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 seticon and cat_icon is not None:
print(dspacing + spacing + 'Submenu = "' + print('{d}{s}Submenu = "{c}" {{ Icon = "{i}"'.format(d=dspacing,
category + '" { Icon = "' + cat_icon + '"') s=spacing,
c=category,
i=cat_icon))
else: else:
print(dspacing + spacing + 'Submenu = "' + category + '" {') print('{d}{s}Submenu = "{c}" {{'.format(d=dspacing, s=spacing,
c=category))
for app in menu_category.applist: for app in menu_category.applist:
name = app.name name = app.name
icon = app.icon icon = app.icon
@ -545,17 +554,17 @@ def pekwmmenu():
# pekwm doesn't like "cd path ; command", but it works # pekwm doesn't like "cd path ; command", but it works
# 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 ' + path + ' && ' + command + ' || ' + command command = 'cd {p} && {c} || {c}'.format(p=path, c=command)
if seticon and icon is not None: if seticon and icon is not None:
print(dspacing + spacing + ' Entry = "' + name + print('{d}{s} Entry = "{n}" {{ Icon = "{i}"; Actions = "Exec {c} &" }}'
'" { Icon = "' + icon + '"; Actions = "Exec ' + .format(d=dspacing, s=spacing, n=name, i=icon,
command + ' &" }') c=command))
else: else:
print(dspacing + spacing + ' Entry = "' + name + print('{d}{s} Entry = "{n}" {{ Actions = "Exec {c} &" }}'
'" { Actions = "Exec ' + command + ' &" }') .format(d=dspacing, s=spacing, n=name, c=command))
print(dspacing + spacing + '}') print('{d}{s}}}'.format(d=dspacing, s=spacing))
if submenu: if submenu:
print(dspacing + '}') print('{}}}'.format(dspacing))
if pekwmdynamic: if pekwmdynamic:
print("}") print("}")
@ -568,12 +577,12 @@ def jwm():
if seticon: if 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="' + applications + '">') print('<Menu label="{}">'.format(applications))
else: else:
print('<Menu icon="' + app_icon + print('<Menu icon="{i}" label="{a}">'.format(i=app_icon,
'" label="' + applications + '">') a=applications))
else: else:
print('<Menu label="' + applications + '">') print('<Menu label="{}">'.format(applications))
else: else:
spacing = '' spacing = ''
for menu_category in menu(): for menu_category in menu():
@ -581,24 +590,25 @@ def jwm():
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 seticon and cat_icon is not None:
print(spacing + '<Menu icon="' + cat_icon + print('{s}<Menu icon="{i}" label="{c}">'.format(s=spacing,
'" label="' + category + '">') i=cat_icon,
c=category))
else: else:
print(spacing + '<Menu label="' + category + '">') print('{s}<Menu label="{c}">'.format(s=spacing, c=category))
for app in menu_category.applist: for app in menu_category.applist:
name = app.name name = app.name
icon = app.icon icon = app.icon
command = app.command command = app.command
path = app.path path = app.path
if path is not None: if path is not None:
command = 'cd ' + path + ' ; ' + command command = 'cd {p} ; {c}'.format(p=path, c=command)
if seticon and icon is not None: if seticon and icon is not None:
print(spacing + ' <Program icon="' + icon + print('{s} <Program icon="{i}" label="{n}">{c}</Program>'
'" label="' + name + '">' + command + '</Program>') .format(s=spacing, i=icon, n=name, c=command))
else: else:
print(spacing + ' <Program label="' + print('{s} <Program label="{n}">{c}</Program>'
name + '">' + command + '</Program>') .format(s=spacing, n=name, c=command))
print(spacing + '</Menu>') print('{}</Menu>'.format(spacing))
if submenu: if submenu:
print('</Menu>') print('</Menu>')
print('</JWM>') print('</JWM>')
@ -608,37 +618,37 @@ def compizboxmenu():
spacing = ' ' spacing = ' '
if seticon: if seticon:
app_icon = icon_strip(applications_icon) app_icon = icon_strip(applications_icon)
print('<menu icon="' + app_icon + print('<menu icon="{i}" name="{a}">'.format(i=app_icon,
'" name="' + applications + '">') a=applications))
else: else:
print('<menu name="' + applications + '">') print('<menu name="{}">'.format(applications))
else: else:
spacing = '' spacing = ''
for menu_category in menu(ico_paths=False): for menu_category in menu(ico_paths=False):
category = menu_category.category category = menu_category.category
cat_icon = category_icon(category) cat_icon = category_icon(category)
if seticon and cat_icon is not None: if seticon and cat_icon is not None:
print(spacing + '<menu icon="' + cat_icon + print('{s}<menu icon="{i}" name="{c}">'.format(s=spacing, i=cat_icon, c=category))
'" name="' + category + '">')
else: else:
print(spacing + '<menu name="' + category + '">') print('{s}<menu name="{c}">'.format(s=spacing, c=category))
for app in menu_category.applist: for app in menu_category.applist:
name = app.name.replace('&', '&amp;') name = app.name.replace('&', '&amp;')
icon = app.icon icon = app.icon
command = app.command command = app.command.replace("'", "'\\''").replace('&', '&amp;')
path = app.path path = app.path
if path is not None: if path is not None:
command = 'sh -c \'cd "' + path.replace("'", "'\\''") + '" ; ' + command.replace("'", "'\\''") + '\'' path = path.replace("'", "'\\''")
command = 'sh -c \'cd "{p}" ;{c}\''.format(p=path, c=command)
if seticon and icon is not None: if seticon and icon is not None:
print(('{} <item type="launcher"><name>{}</name>' print(('{s} <item type="launcher"><name>{n}</name>'
'<icon>{}</icon>' '<icon>{i}</icon>'
'<command>{}</command></item>').format(spacing, '<command>{c}</command></item>').format(s=spacing,
name, icon, command.replace('&', '&amp;'))) n=name, i=icon, c=command))
else: else:
print(('{} <item type="launcher"><name>{}</name>' print(('{s} <item type="launcher"><name>{n}</name>'
'<command>{}</command></item>').format(spacing, '<command>{c}</command></item>').format(s=spacing,
name, command.replace('&', '&amp;'))) n=name, c=command))
print(spacing + '</menu>') print('{}</menu>'.format(spacing))
if submenu: if submenu:
print('</menu>') print('</menu>')

Loading…
Cancel
Save