WIP : script to add program to handy menu

parent 1642472e
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import yaml
import argparse
import os
import subprocess
import re
def find_desktop(name):
if not re.search(r'\.desktop$', name):
name = '{}.desktop'.format(name)
if os.path.exists(name):
return name
output = subprocess.run(['locate', name], stdout=subprocess.PIPE).stdout.decode()
return output.replace("\n", "")
def parse_desktop(desktop):
out = {}
with open(desktop) as f:
text = "\n".join(f.readlines())
p = re.compile(r'((Exec|Icon|Name\[fr\]|GenericName\[fr\]).*)')
matches = p.findall(text)
return {k: m.split("=")[1] for m, k in matches}
def add_to_handymenu(user, cmd, category):
path = '/home/{}/.config/handymenu/conf.default.yml'.format(user)
with open(path) as stream:
try:
yml = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
found = False
for app in yml:
lastId = app['id']
if(app['name'] == category):
print("found")
print(app['apps'])
app['apps'] += [cmd]
print(app['apps'])
found = True
if(not found):
yml += {
'apps': [cmd],
'name': category,
'id': lastId+1,
}
print(yml)
with open(path, 'w') as f:
yaml.dump(yml, f, default_flow_style=False)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Add proggrams to handy menu.')
parser.add_argument('app', type=str, nargs=1, help='The app to add (name of a .desktop app or a path to the file)')
parser.add_argument('category', type=str, nargs=1, help='The name of the category in which add the app')
parser.add_argument('users', type=str, nargs='+', help='The users that should see the programm in their handy menu')
args = parser.parse_args()
desktop = find_desktop(args.app[0])
values = parse_desktop(desktop)
cmd = {
'cmd': values['Exec'],
'generic': values['GenericName[fr]'],
'name': values['Name[fr]'],
'icon': values['Icon'],
}
for user in args.users:
add_to_handymenu(user, cmd, args.category[0])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment