def parse(desktop_file, debug_enabled): """ Returns a dict if the .desktop entry should be included in the dmenu, or none if not applicable """ application = DesktopEntry(desktop_file) if debug_enabled: eprint('PARSED: ' + desktop_file) if application.getHidden(): if debug_enabled: eprint('HIDDEN: ' + desktop_file) return None if application.getNoDisplay(): if debug_enabled: eprint('NODISPLAY: ' + desktop_file) return None executable = application.getTryExec() if not executable: executable = application.getExec() if not executable: if debug_enabled: eprint('NO EXECUTABLE: ' + desktop_file) return None return { application.getName(): [part for part in executable.split(' ') if not part.startswith('%')] }
def main(argv): args = parse_args(argv[1:]) if args.list_dirs: for directory in xdg_data_dirs: print(os.path.join(directory, "applications")) elif args.list_files: for directory in xdg_data_dirs: path = os.path.join(directory, "applications") try: for entry in os.listdir(path): if entry.endswith(".desktop"): if args.verbose: filename = os.path.join(path, entry) desktop = DesktopEntry(filename) print("{:70} {:40} {:40}".format( filename, desktop.getName(), desktop.getExec())) else: print(os.path.join(path, entry)) except FileNotFoundError: pass else: filename = get_desktop_file(args.DESKTOP) print(filename) desktop = DesktopEntry(filename) print("Name: {}".format(desktop.getName())) print("Exec: {}".format(desktop.getExec())) print("TryExec: {}".format(desktop.getTryExec())) print("Mime-Types: {}".format(desktop.getMimeTypes()))
def addDesktopEntry(file): entry=DesktopEntry() try: entry.parse(file) except: return if entry.getNoDisplay(): return tryexec=entry.getTryExec() if tryexec and not os.path.exists(tryexec): return entry.setLocale("C") display_name=entry.getName() catalog.insertEntry(file, display_name, run)
def listAndUpdateDesktopFiles(self, path, menuObjs): for f in listdir(path): fPath = path + f if isfile(fPath) and f.endswith(".desktop"): xdgObj = DesktopEntry(fPath) title = xdgObj.getName() groups = xdgObj.getCategories() comment = xdgObj.getComment() icon = xdgObj.getIcon() mainExec = xdgObj.getExec() tryExec = xdgObj.getTryExec() group = "" if "Accessories" in groups or "Utility" in groups: group = "Accessories" elif "Multimedia" in groups or "Video" in groups or "Audio" in groups: group = "Multimedia" elif "Development" in groups: group = "Development" elif "Game" in groups: group = "Game" elif "Internet" in groups or "Network" in groups: group = "Internet" elif "Graphics" in groups: group = "Graphics" elif "Office" in groups: group = "Office" elif "System" in groups: group = "System" elif "Settings" in groups: group = "Settings" elif "Wine" in groups: group = "Wine" else: group = "Other" menuObjs[group].append( {"title": title, "groups": groups, "comment": comment, "exec": mainExec, "tryExec": tryExec, "fileName": f, "filePath": fPath, "icon": icon})
def parseMenu(menu, menuJSON): for submenu in menu.Entries: if isinstance(submenu, Menu): newmenu=parseMenu(submenu, []); #print ("Submenu icon: "+submenu.getIcon() + " is "+theme.getIconPath(submenu.getIcon())) iconpath=theme.getIconPath(submenu.getIcon(), None, 'Numix-Circle') #iconpath=theme.getIconPath(submenu.getIcon(), 128, 'future-green') #iconpath=theme.getIconPath(submenu.getIcon(), 128, 'Numix-Circle') iconpath=give_me_png(iconpath) if iconpath==None: iconpath='' print "cat icon: "+submenu.getIcon()+" is "+iconpath menuJSON.append({"id": unicode(submenu).replace(" ", "_"), "name":submenu.getName(), "icon":iconpath, "children":newmenu}); elif isinstance(submenu, MenuEntry): # Description at # http://pyxdg.readthedocs.org/en/latest/_modules/xdg/DesktopEntry.html newitem={} filename="/usr/share/applications/"+unicode(submenu); item=False if (os.path.isfile(filename)): item=DesktopEntry(filename) else: # is kde4- prexifed? if (unicode(submenu)[0:5]=="kde4-"): filename="/usr/share/applications/kde4/"+unicode(submenu)[5:]; item=DesktopEntry(filename) #print "!!"+item; if item is not False: newitem["id"]=unicode(submenu).replace(" ", "_") newitem["name"]=item.getName() #newitem["icon"]=item.getIcon() newitem["comment"]=item.getComment() newitem["tryexec"]=item.getTryExec() newitem["exec"]=item.getExec() newitem["path"]=item.getPath() iconpath=theme.getIconPath(item.getIcon(), 128, 'Numix-Circle') #iconpath=theme.getIconPath(item.getIcon(), 128, 'future-green') #iconname=item.getIcon(); # Convert png if not exists... #iconpath=give_me_png(iconname); iconpath=give_me_png(iconpath); if iconpath==None: iconpath='' #print "icon: "+item.getIcon()+" is "+iconpath #print ("Icon: "+item.getIcon() + " is "+iconpath) newitem["icon"]=iconpath #newitem["name"]=a.getName(); #newitem["id"]=unicode(submenu) # Only Append if it's executable #if (item.findTryExec()): # menuJSON.append(newitem); #if newitem["name"]!="": menuJSON.append(newitem); return menuJSON
class AutostartFile: def __init__(self, path): self.path = path self.filename = os.path.basename(path) self.dirname = os.path.dirname(path) self.de = DesktopEntry(path) def __eq__(self, other): return self.filename == other.filename def __str__(self): return self.path + " : " + self.de.getName() def _isexecfile(self, path): return os.access(path, os.X_OK) def _findFile(self, path, search, match_func): # check empty path if not path: return None # check absolute path if path[0] == "/": if match_func(path): return path else: return None else: # check relative path for dirname in search.split(os.pathsep): if dirname != "": candidate = os.path.join(dirname, path) if match_func(candidate): return candidate def _alert(self, mstr, info=False): if info: print("\t " + mstr) else: print("\t*" + mstr) def _showInEnvironment(self, envs, verbose=False): default = not self.de.getOnlyShowIn() noshow = False force = False for i in self.de.getOnlyShowIn(): if i in envs: force = True for i in self.de.getNotShowIn(): if i in envs: noshow = True if verbose: if not default and not force: s = "" for i in self.de.getOnlyShowIn(): if s: s += ", " s += i self._alert("Excluded by: OnlyShowIn (" + s + ")") if default and noshow and not force: s = "" for i in self.de.getNotShowIn(): if s: s += ", " s += i self._alert("Excluded by: NotShowIn (" + s + ")") return (default and not noshow) or force def _shouldRun(self, envs, verbose=False): if not self.de.getExec(): if verbose: self._alert("Excluded by: Missing Exec field") return False if self.de.getHidden(): if verbose: self._alert("Excluded by: Hidden") return False if self.de.getTryExec(): if not self._findFile(self.de.getTryExec(), os.getenv("PATH"), self._isexecfile): if verbose: self._alert("Excluded by: TryExec (" + self.de.getTryExec() + ")") return False if not self._showInEnvironment(envs, verbose): return False return True def display(self, envs): if self._shouldRun(envs): print("[*] " + self.de.getName()) else: print("[ ] " + self.de.getName()) self._alert("File: " + self.path, info=True) if self.de.getExec(): self._alert("Executes: " + self.de.getExec(), info=True) self._shouldRun(envs, True) print def run(self, envs): here = os.getcwd() if self.de.getPath(): os.chdir(self.de.getPath()) if self._shouldRun(envs): args = ["/bin/sh", "-c", "exec " + self.de.getExec()] os.spawnv(os.P_NOWAIT, args[0], args) os.chdir(here)