-
Notifications
You must be signed in to change notification settings - Fork 9
/
awesome-appmenu
executable file
·607 lines (510 loc) · 21.6 KB
/
awesome-appmenu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#!/usr/bin/env python
# awesome-appmenu is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# sbomgr is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with sbomgr. If not, see <http://www.gnu.org/licenses/>.
# Copyright (C) 2016 Daniel Prosser
import os, sys
from copy import copy
# Try to import configuration file
try:
sys.path.insert(0, os.environ["HOME"] + "/.config/awesome-appmenu")
import menurc
config = True
except:
config = False
################################################################################
# Launcher: finds and stores information for a .desktop file
class Launcher():
# Exec field flags
field_codes = ["%f", "%F", "%u", "%U", "%d", "%D", "%n", "%N", "%i", "%c", \
"%k", "%v", "%m"]
def __init__(self, launcherfile, use_icon=True,
icon_search_paths=["/usr/share/icons/hicolor"], verbose=False):
self.Name = None
self.FileName = None
self.FilePath = None
self.Exec = None
self.IconGenericName = None
self.Icon = None
self.NoDisplay = False
self.OnlyShowIn = None
self.Terminal = False
self.Categories = []
self.verbose = verbose
self.getInfo(launcherfile, use_icon)
if (use_icon and self.IconGenericName and not self.NoDisplay):
self.findIcon(icon_search_paths)
self.expandFieldCodes()
# Reads info from launcher file
def getInfo(self, launcherfile, use_icon):
self.FileName = launcherfile.split("/")[-1].strip()
self.FilePath = launcherfile
f = open(launcherfile)
for line in f:
splitline = line.split("=")
if splitline[0] == "Name":
# If Name is being defined again, just go on (google chrome and
# Libreoffice do this)
if self.Name:
if self.verbose:
print("Redefinition of Name in " + launcherfile + ".")
break
else:
self.Name = splitline[1].strip().replace("'", "\\'")
if self.verbose:
print("Name key for " + launcherfile + ":" + self.Name + ".")
elif splitline[0] == "Exec":
# Sometimes Exec lines have '=' if they set environment variables
self.Exec = splitline[1].strip()
for i in range(2, len(splitline)):
self.Exec += "=" + splitline[i].strip()
if self.verbose:
print("Exec key for " + launcherfile + ":" + self.Exec + ".")
elif splitline[0] == "Icon":
if use_icon:
# Full path specified
if splitline[1][0] == "/": self.Icon = splitline[1].strip()
# Name with or without extension
else:
if ( (splitline[1].strip()[-4:] == ".png") or
(splitline[1].strip()[-4:] == ".svg") or
(splitline[1].strip()[-4:] == ".jpg") ):
self.IconGenericName = splitline[1].strip()[:-4]
else:
self.IconGenericName = splitline[1].strip()
if self.verbose:
if self.IconGenericName:
print("Icon key for " + launcherfile + ":" +
self.IconGenericName + ".")
else:
print("Icon key for " + launcherfile + ":" +
self.Icon + ".")
elif splitline[0] == "Categories":
self.Categories = splitline[1].split(";")
if self.verbose:
print("Categories key for " + launcherfile + ":" + splitline[1] + ".")
for i in range(len(self.Categories)):
self.Categories[i] = self.Categories[i].strip()
elif splitline[0] == "NoDisplay":
if (splitline[1].strip() == "true"):
self.NoDisplay = True
if self.verbose:
print(launcherfile + " has NoDisplay set: skipping.")
elif splitline[0] == "OnlyShowIn":
self.OnlyShowIn = splitline[1].strip()
if self.verbose:
print("OnlyShowIn key for " + launcherfile + ": " +
self.OnlyShowIn + ".")
elif splitline[0] == "Terminal":
if splitline[1].strip() == "true":
if self.verbose:
print(launcherfile + " launches in a terminal.")
self.Terminal = True
f.close()
# Expands field codes in Exec key. See the freedesktop spec:
# https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s06.html
def expandFieldCodes(self):
if not self.Exec: return
for code in self.field_codes:
if self.Exec.find(code) != -1:
if code == "%c":
# Replace any variants of %c, quoted or not, with name in quotes
if self.Name: replacename = '"' + self.Name + '"'
else: replacename = ""
if self.Exec.find('"%c"') != -1:
self.Exec = self.Exec.replace('"%c"', replacename)
if self.Exec.find("'%c'") != -1:
self.Exec = self.Exec.replace("'%c'", replacename)
if self.Exec.find(code) != -1:
self.Exec = self.Exec.replace(code, replacename)
elif code == "%k":
if self.FilePath: self.Exec = self.Exec.replace(code, self.FilePath)
else: self.Exec = self.Exec.replace(code, "")
elif code == "%i":
if (self.Icon):
self.Exec = self.Exec.replace(code, "--icon " + self.Icon)
else: self.Exec = self.Exec.replace(code, "")
# Other field codes are not relevant for launching from appmenu
else: self.Exec = self.Exec.replace(code, "")
# Finally, remove extra spaces that may have been caused by removals
splitexec = self.Exec.split()
self.Exec = splitexec[0]
for i in range(1, len(splitexec)):
self.Exec += " " + splitexec[i]
# Replace ' with \\' to prevent syntax errors on config load
if self.Exec.find("'") != -1:
self.Exec = self.Exec.replace("'", "\\'")
if self.verbose:
print("Expanded field code for " + self.Name + ": " + self.Exec + ".")
# Finds icon by generic name
def findIcon(self, icon_search_paths):
# Note: this just takes the first matching icon that is encountered. It
# doesn't prefer any particular size if there are multiple matches.
for directory in icon_search_paths:
# Skip any directory that doesn't exist
if not os.path.isdir(directory):
if self.verbose:
print("Icon search path " + directory + " does not exist: skipping.")
continue
# Descend into directory
for root, dirs, files in os.walk(directory):
for name in files:
if name[:-4] == self.IconGenericName:
self.Icon = os.path.join(root, name)
if self.verbose:
print("Found icon for " + self.Name + ": " + self.Icon)
break
if self.Icon: break
if self.Icon: break
################################################################################
# Category: groups and sorts launchers
class Category():
def __init__(self, listed_name, name, use_icon=True, icon_generic_name=None,
icon_search_paths=["/usr/share/icons/hicolor"], verbose=False, subcat_of=""):
self.ListedName = listed_name
self.Name = name
self.UseIcon = use_icon
self.IconGenericName = icon_generic_name
self.Icon = None
self.NumLaunchers = 0
self.Launchers = []
self.verbose = verbose
self.SubCat_of = ""
# Determine if icon_generic_name supplied is actually an icon
if use_icon and icon_generic_name:
if ( (icon_generic_name.strip()[-4:] == ".png") or
(icon_generic_name.strip()[-4:] == ".svg") or
(icon_generic_name.strip()[-4:] == ".jpg") ):
self.Icon = icon_generic_name
self.IconGenericName = None
if self.verbose:
print("Using supplied path for category " + self.Name + " icon.")
if (use_icon and self.IconGenericName): self.findIcon(icon_search_paths)
if subcat_of != "":
self.SubCat_of = subcat_of
# Finds icon by generic name
def findIcon(self, icon_search_paths):
# Note: this just takes the first matching icon that is encountered. It
# doesn't prefer any particular size if there are multiple matches.
for directory in icon_search_paths:
# Skip any directory that doesn't exist
if not os.path.isdir(directory):
if self.verbose:
print("Icon search path " + directory + " does not exist: skipping.")
continue
# Descend into directory
for root, dirs, files in os.walk(directory):
for name in files:
if name[:-4] == self.IconGenericName:
self.Icon = os.path.join(root, name)
if self.verbose:
print("Found icon for " + self.Name + " category: " + self.Icon)
break
if self.Icon: break
if self.Icon: break
# Adds a launcher
def addLauncher(self, launcher):
self.Launchers.append(launcher)
self.NumLaunchers += 1
if self.verbose:
print("Added " + launcher.Name + " to " + self.Name + ".")
# Sorts launchers
def sortLaunchers(self):
if self.verbose:
print("Sorting launchers for " + self.Name + " category.")
# Create a list of launchers by name, using FileName to distinguish
launcherdict = {}
for launcher in self.Launchers:
launcherdict.update({launcher.FileName: launcher.Name})
# Sort the list and create a new self.Launchers list
# http://www.saltycrane.com/blog/2007/09/how-to-sort-python-dictionary-by-keys/
templaunchers = copy(self.Launchers)
self.Launchers = []
for key, value in sorted(launcherdict.items(), key=lambda item: (item[1], item[0])):
for launcher in templaunchers:
if launcher.FileName == key:
self.Launchers.append(launcher)
break
if self.verbose:
print("Finished sorting launchers for " + self.Name + " category.")
################################################################################
# Menu: groups categories
class Menu():
def __init__(self, verbose=False):
self.Categories = []
self.NumCategories = 0
self.miscidx = -1
self.Favorites = []
self.verbose = verbose
# Adds a category
def addCategory(self, category):
self.Categories.append(category)
self.NumCategories += 1
if self.verbose: print("Added category " + category.Name)
# Finds launchers and adds them to appropriate categories
def getLaunchers(self, launcherpaths, use_icon=True, icon_search_paths=
["/usr/share/icons/hicolor"], ignore_OnlyShowIn=False):
# See if there's a Miscellaneous category or create it if needed
for i in range(self.NumCategories):
if self.Categories[i].Name == "Miscellaneous":
self.miscidx = i
break
if self.miscidx == -1:
self.addCategory(Category("Miscellaneous", "Miscellaneous", use_icon,
"applications-other", icon_search_paths,
verbose, ""))
self.miscidx = self.NumCategories - 1
if self.verbose: print("Added Miscellaneous category idx {:d}.".format(
self.miscidx))
# Find and group launchers into categories
for directory in launcherpaths:
# Skip any directory that doesn't exist
if not os.path.isdir(directory): continue
for root, dirs, files in os.walk(directory):
for name in files:
if name[-8:] == ".desktop":
newlauncher = Launcher(os.path.join(root, name), use_icon,
icon_search_paths, verbose)
# Don't register launcher if there is no Exec field, if it has
# NoDisplay=true, or if it has OnlyShowIn != awesome (unless
# ignore_OnlyShowIn is enabled)
if not newlauncher.Exec:
if self.verbose:
print(newlauncher.Name + " has no Exec field: skipping.")
continue
if newlauncher.NoDisplay:
if self.verbose:
print(newlauncher.Name + " has NoDisplay set: skipping.")
continue
if not ignore_OnlyShowIn:
if newlauncher.OnlyShowIn:
if newlauncher.OnlyShowIn != "awesome":
if self.verbose:
print(newlauncher.Name + " is only shown in " +
newlauncher.OnlyShowIn + ": skipping.")
continue
# Assign launcher to appropriate category(ies)
self.assignLauncher(newlauncher)
# Adds launcher to appropriate category(ies)
def assignLauncher(self, launcher):
launcher_grouped = False
for category_name in launcher.Categories:
for category in self.Categories:
if category_name == category.ListedName:
category.addLauncher(launcher)
launcher_grouped = True
if self.verbose:
print("Assigned " + launcher.Name + " to " + category.Name +
" category.")
# Put it in Miscellaneous category if nothing else fits
if not launcher_grouped:
self.Categories[self.miscidx].addLauncher(launcher)
if self.verbose:
print("Assigned " + launcher.Name + " to Miscellaneous category.")
# Sorts launchers in each category
def sortLaunchers(self):
for category in self.Categories: category.sortLaunchers()
# Creates favorites list
def createFavoritesList(self, favorites):
for favorite in favorites:
found = False
for category in self.Categories:
for launcher in category.Launchers:
if launcher.FileName == favorite:
found = True
self.Favorites.append(launcher)
if self.verbose:
print("Adding " + launcher.Name + " to favorites.")
break
if found: break
if not found:
print("Warning: could not find a launcher file named " + favorite + ".")
# Writes menu for awesomeWM
def write(self, terminal="xterm"):
home = os.environ["HOME"]
menufile = home + "/.config/awesome/appmenu.lua"
try:
f = open(menufile, 'w')
except IOError:
os.makedirs(home + "/.config/awesome")
f = open(menufile, 'w')
# Header
f.write("local appmenu = {}\n\n")
# Write categories as long as they are not empty
for category in self.Categories:
if category.NumLaunchers == 0:
if self.verbose:
print(category.Name + " category is empty: it will not be written.")
continue
else:
if self.verbose:
print("Writing category " + category.Name + ".")
f.write("appmenu." + category.Name + " = {\n")
# Write subcategories directly into main categories
for subcategory in self.Categories:
if subcategory.NumLaunchers == 0: continue
if subcategory.SubCat_of == category.Name:
f.write(" { '" + subcategory.Name + "', appmenu." + subcategory.Name)
if category.Icon:
f.write(", '" + subcategory.Icon + "'")
f.write(" },\n")
for launcher in category.Launchers:
if self.verbose:
print("Writing " + launcher.Name + " to " + category.Name + ".")
if launcher.Terminal:
if self.verbose:
print(launcher.Name + " launches in a terminal.")
f.write(" { '" + launcher.Name + "', '" + terminal + " -e " +
launcher.Exec + "'")
else: f.write(" { '" + launcher.Name + "', '" + launcher.Exec + "'")
if launcher.Icon:
f.write(", '" + launcher.Icon + "'")
f.write(" },\n")
f.write("}\n\n")
# Write favorites list if not empty
if len(self.Favorites) != 0:
if self.verbose:
print("Writing favorites list.")
f.write("appmenu.Favorites = {\n")
for favorite in self.Favorites:
if self.verbose:
print("Writing " + favorite.Name + " to favorites.")
if favorite.Terminal:
if self.verbose:
print(favorite.Name + " launches in a terminal.")
f.write(" { '" + favorite.Name + "', '" + terminal + " -e " +
favorite.Exec + "'")
else: f.write(" { '" + favorite.Name + "', '" + favorite.Exec + "'")
if favorite.Icon:
f.write(", '" + favorite.Icon + "'")
f.write(" },\n")
f.write("}\n\n")
else:
if self.verbose: print("Favorites list is empty: it will not be written.")
# Write menu
f.write("appmenu.Appmenu = {\n")
for category in self.Categories:
if category.NumLaunchers == 0: continue
# Don't write subdirectorys to main list
if category.SubCat_of != "": continue
f.write(" { '" + category.Name + "', appmenu." + category.Name)
if category.Icon:
f.write(", '" + category.Icon + "'")
f.write(" },\n")
f.write("}\n\n")
f.write("return appmenu")
# Close file and print notification
f.close()
print("Wrote " + menufile + ".")
################################################################################
# Reads configuration file
def load_config():
global config
# Set defaults
home = os.environ["HOME"]
launcherpaths = ["/usr/share/applications",
home + "/.local/share/applications"]
iconpaths = ["/usr/share/icons/hicolor"]
#Categories ["Category", "Category name","Icon", "SubCategory of"]
categories = [ ["Utility", "Accessories", "applications-utilities", ""],
["Development", "Development", "applications-development", ""],
["Education", "Education", "applications-science", ""],
["Game", "Games", "applications-games", ""],
["Graphics", "Graphics", "applications-graphics", ""],
["Network", "Internet", "applications-internet", ""],
["Office", "Office", "applications-office", ""],
["AudioVideo", "MultiMedia", "applications-multimedia", ""],
["Settings", "Settings", "applications-accessories", ""],
["System", "System", "applications-system", ""],
["Wine", "Wine", "wine", ""] ]
favorites = []
terminal = "xterm"
ignore_OnlyShowIn = False
# Read configuration
if config:
try: launcherpaths = menurc.launcherpaths
except AttributeError:
print("Warning: menurc.py does not have launcherpaths. Using default.")
try: iconpaths = menurc.iconpaths
except AttributeError:
print("Warning: menurc.py does not have iconpaths. Using default.")
try: categories = menurc.categories
except AttributeError:
print("Warning: menurc.py does not have categories. Using default.")
try: favorites = menurc.favorites
except AttributeError: pass
try: terminal = menurc.terminal
except AttributeError: pass
try: ignore_OnlyShowIn = menurc.ignore_OnlyShowIn
except AttributeError: pass
# Warnings about any non-existent directories
for directory in launcherpaths:
if not os.path.isdir(directory):
print("Warning: search path " + directory + " does not exist:")
print("No launchers will be searched for there.")
for directory in iconpaths:
if not os.path.isdir(directory):
print("Warning: search path " + directory + " does not exist:")
print("No icons will be searched for there.")
return launcherpaths, iconpaths, categories, favorites, terminal, \
ignore_OnlyShowIn
################################################################################
# Prints usage info
def print_help():
print("Usage: " + sys.argv[0] + " OPTION")
print("Options:")
print(" --no-icons, -n: generate a menu without icons")
print(" --verbose, -v: print verbose output for debugging")
print(" --help, -h: show this usage information")
################################################################################
# Parses command line arguments
def parse_clos(argv):
use_icon = True
verbose = False
for i in range(1, len(argv)):
arg = argv[i]
if (arg == "--help") or (arg == "-h"):
print_help()
exit(0)
elif (arg == "--no-icons") or (arg == "-n"): use_icon = False
elif (arg == "--verbose") or (arg == "-v"): verbose = True
else:
print("Unrecognized option " + arg + ".")
print_help()
exit(1)
return use_icon, verbose
################################################################################
# Main program: generates an application menu for Awesome
if __name__ == "__main__":
# Get command line arguments
use_icon, verbose = parse_clos(sys.argv)
# Load configuration file
launcherpaths, iconpaths, categories, favorites, terminal, \
ignore_OnlyShowIn = load_config()
# Create a list of categories
if use_icon: print("Finding category icons ...")
appmenu = Menu(verbose)
ncategories = len(categories)
for i in range(ncategories):
appmenu.addCategory(Category(categories[i][0], categories[i][1], use_icon,
categories[i][2], iconpaths, verbose, categories[i][3]))
# Find launchers and add them to appropriate categories
if use_icon: print("Finding launcher files and icons ...")
else: print("Finding launchers ...")
appmenu.getLaunchers(launcherpaths, use_icon, iconpaths, ignore_OnlyShowIn)
# Sort launchers
print("Sorting launchers ...")
appmenu.sortLaunchers()
# Get favorites menu
if len(favorites) > 0:
print("Creating favorites list ...")
appmenu.createFavoritesList(favorites)
# Write menu for awesomeWM
appmenu.write(terminal=terminal)