def parse_desktop_file(item): path, dir, prefix = item try: menuentry = MenuEntry(path, dir, prefix) except ParsingError: return return menuentry, dir
def editMenu(self, menu, name=None, genericname=None, comment=None, icon=None, nodisplay=None, hidden=None): # Hack for legacy dirs if isinstance(menu.Directory, MenuEntry) and menu.Directory.Filename == ".directory": xml_menu = self.__getXmlMenu(menu.getPath(True, True)) self.__addXmlTextElement(xml_menu, 'Directory', menu.Name + ".directory") menu.Directory.setAttributes(menu.Name + ".directory") # Hack for New Entries elif not isinstance(menu.Directory, MenuEntry): if not name: name = menu.Name filename = self.__getFileName(name, ".directory").replace("/", "") if not menu.Name: menu.Name = filename.replace(".directory", "") xml_menu = self.__getXmlMenu(menu.getPath(True, True)) self.__addXmlTextElement(xml_menu, 'Directory', filename) menu.Directory = MenuEntry(filename) deskentry = menu.Directory.DesktopEntry if name: if not deskentry.hasKey("Name"): deskentry.set("Name", name) deskentry.set("Name", name, locale=True) if genericname: if not deskentry.hasKey("GenericName"): deskentry.set("GenericName", genericname) deskentry.set("GenericName", genericname, locale=True) if comment: if not deskentry.hasKey("Comment"): deskentry.set("Comment", comment) deskentry.set("Comment", comment, locale=True) if icon: deskentry.set("Icon", icon) if nodisplay is True: deskentry.set("NoDisplay", "true") elif nodisplay is False: deskentry.set("NoDisplay", "false") if hidden is True: deskentry.set("Hidden", "true") elif hidden is False: deskentry.set("Hidden", "false") menu.Directory.updateAttributes() if isinstance(menu.Parent, Menu): self.menu.sort() return menu
def createMenuEntry(self, parent, name, command=None, genericname=None, comment=None, icon=None, terminal=None, after=None, before=None): menuentry = MenuEntry(self.__getFileName(name, ".desktop")) menuentry = self.editMenuEntry(menuentry, name, genericname, comment, command, icon, terminal) self.__addEntry(parent, menuentry, after, before) self.menu.sort() return menuentry
def __addFiles(self, dir, subdir, prefix, legacy): for item in os.listdir(os.path.join(dir, subdir)): if os.path.splitext(item)[1] == ".desktop": try: menuentry = MenuEntry(os.path.join(subdir, item), dir, prefix) except ParsingError: continue self.cacheEntries[dir].append(menuentry) if legacy: self.cacheEntries['legacy'].append(menuentry) elif os.path.isdir(os.path.join(dir, subdir, item)) and not legacy: self.__addFiles(dir, os.path.join(subdir, item), prefix, legacy)
def load_xdg_menu_data(): try: from xdg.Menu import parse, Menu #pylint: disable=import-outside-toplevel except ImportError: log("load_xdg_menu_data()", exc_info=True) if first_time("no-python-xdg"): log.warn("Warning: cannot use application menu data:") log.warn(" no python-xdg module") return None menu = None error = None #see ticket #2340, #invalid values for XDG_CONFIG_DIRS can cause problems, #so try unsetting it if we can't load the menus with it: for cd in (False, True): with OSEnvContext(): if cd: if not os.environ.pop("XDG_CONFIG_DIRS", ""): #was already unset continue #see ticket #2174, #things may break if the prefix is not set, #and it isn't set when logging in via ssh for prefix in (None, "", "gnome-", "kde-"): if prefix is not None: os.environ["XDG_MENU_PREFIX"] = prefix try: log( "parsing xdg menu data for prefix %r with XDG_CONFIG_DIRS=%s and XDG_MENU_PREFIX=%s", prefix, os.environ.get("XDG_CONFIG_DIRS"), os.environ.get("XDG_MENU_PREFIX")) menu = parse() break except Exception as e: log("load_xdg_menu_data()", exc_info=True) error = e menu = None if menu: break if menu is None: if error: log.error("Error parsing xdg menu data:") log.error(" %s", error) log.error(" this is either a bug in python-xdg,") log.error(" or an invalid system menu configuration") return None menu_data = {} entries = tuple(menu.getEntries()) log("%s.getEntries()=%s", menu, entries) if len(entries) == 1 and entries[0].Submenus: entries = entries[0].Submenus log("using submenus %s", entries) for i, submenu in enumerate(entries): if not isinstance(submenu, Menu): log("entry '%s' is not a submenu", submenu) continue name = submenu.getName() log("* %-3i %s", i, name) if not submenu.Visible: log(" submenu '%s' is not visible", name) continue try: md = load_xdg_menu(submenu) if md: menu_data[name] = md else: log(" no menu data for %s", name) except Exception as e: log("load_xdg_menu_data()", exc_info=True) log.error("Error loading submenu '%s':", name) log.error(" %s", e) if LOAD_APPLICATIONS: from xdg.Menu import MenuEntry entries = {} for d in LOAD_APPLICATIONS: for f in os.listdir(d): if not f.endswith(".desktop"): continue try: me = MenuEntry(f, d) except Exception: log("failed to load %s from %s", f, d, exc_info=True) else: ed = load_xdg_entry(me.DesktopEntry) if not ed: continue name = ed.get("Name") #ensure we don't already have it in another submenu: for menu_category in menu_data.values(): if name in menu_category.get("Entries", {}): ed = None break if ed: entries[name] = ed log("entries(%s)=%s", LOAD_APPLICATIONS, remove_icons(entries)) if entries: #add an 'Applications' menu if we don't have one: md = menu_data.get("Applications") if not md: md = { "Name": "Applications", } menu_data["Applications"] = md md.setdefault("Entries", {}).update(entries) return menu_data