Skip to content

Instantly share code, notes, and snippets.

@bharadwaj-raju
Created January 9, 2017 16:47
Show Gist options
  • Save bharadwaj-raju/bf332240d30a2a0f8e59855bac2386de to your computer and use it in GitHub Desktop.
Save bharadwaj-raju/bf332240d30a2a0f8e59855bac2386de to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
import os
def parse_xdg_desktop(desktop_file_or_string):
'''Parse a .desktop file.
Parse an XDG .desktop file or a string with its contents into an easy-to-use dict, with standard values present even if not defined in file.
Args:
desktop_file_or_string (str): Either the path to a .desktop file or a string with a .desktop file as its contents.
Returns:
dict: A dictionary of the parsed file.'''
if os.path.isfile(desktop_file_or_string):
with open(desktop_file_or_string) as f:
desktop_file = f.read()
else:
desktop_file = desktop_file_or_string
result = {}
for line in desktop_file.split('\n'):
if '=' in line:
result[line.split('=')[0]] = line.split('=')[1]
for key, value in result.items():
if value == 'false':
result[key] = False
elif value == 'true':
result[key] = True
if not 'Terminal' in result:
result['Terminal'] = False
if not 'Hidden' in result:
result['Hidden'] = False
return result
def convert(desktop):
desktop_parsed = parse_xdg_desktop(desktop)
debian_menu = '?package({name})\\\n'.format(name=desktop_parsed['Name'])
debian_menu += 'needs="{env}"\\\n'.format(env='text' if desktop_parsed['Terminal'] else 'X11')
debian_menu += 'title="{name}"\\\n'.format(name=desktop_parsed['Name'])
debian_menu += 'longtitle="{comment}"\\\n'.format(comment=desktop_parsed['Name'] + ' - ' + desktop_parsed['Comment'])
debian_menu += 'command="{command}"\\\n'.format(command=desktop_parsed['Exec'])
# TODO: debian_menu += 'section="{}"'
return debian_menu
def main():
print(convert(sys.argv[1]))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment