def add_desktop(self, filename): try: d = DesktopEntry(filename) if d.getHidden() or d.getNoDisplay() or d.getTerminal( ) or d.getType() != 'Application': return app = Application(name=d.getName(), command_line=get_command(d), mtime=datetime.fromtimestamp( os.path.getmtime(filename)), is_desktop=True) if d.getPath(): app.path = d.getPath() self.add_app(app) except (xdg.Exceptions.ParsingError, xdg.Exceptions.DuplicateGroupError, xdg.Exceptions.DuplicateKeyError): pass
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)