def fetchMore(self, parent): # print("RecollQuery.fetchMore:") num_to_fetch = min(self.pagelen, self.totres - len(self.searchResults)) titem = [] for count in range(num_to_fetch): try: item = self.query.fetchone() url = str(item['url'][7:]) if url.endswith("desktop"): desktop = DesktopEntry() try: desktop.parse(url) except (ParsingError, DuplicateGroupError, DuplicateKeyError) as e: print(e) if desktop.getNoDisplay(): continue if len(desktop.getOnlyShowIn()) != 0: continue titem.append(item) except: break self.beginInsertRows(QtCore.QModelIndex(), len(self.searchResults), len(self.searchResults) + len(titem) - 1) self.searchResults.extend(titem) self.endInsertRows()
def parse_to_entries(file_name): if file_name.endswith('.menu'): return xdg.Menu.parse(file_name) else: result = DesktopEntry() result.parse(file_name) return result
def launch_desktop(self, request, handler, desktop): entry = DesktopEntry() entry.parse(desktop) exec_ = entry.getExec() self.launch( request, handler, '/bin/bash -c "%s"' % exec_.replace('"', '\\"')) # Is it really necessary to call bash ?
def _extract_desktop_entry(self, app): '''Get DesktopEntry for desktop file and verify it''' d = self.manifest['hooks'][app]['desktop'] fn = os.path.join(self.unpack_dir, d) bn = os.path.basename(fn) if not os.path.exists(fn): error("Could not find '%s'" % bn) fh = open_file_read(fn) contents = "" for line in fh.readlines(): contents += line fh.close() try: de = DesktopEntry(fn) except xdgParsingError as e: error("desktop file unparseable: %s (%s):\n%s" % (bn, str(e), contents)) try: de.parse(fn) except Exception as e: error("desktop file unparseable: %s (%s):\n%s" % (bn, str(e), contents)) return de, fn
def launch_desktop(self, request, handler, desktop): entry = DesktopEntry() entry.parse(desktop) exec_ = entry.getExec() self.launch( request, handler, '/bin/bash -c "%s"' % exec_.replace('"', '\\"') ) # Is it really necessary to call bash ?
def on_drag_data_received(self, widget, context, x, y, selection, target, timestamp, box): droppeduris = selection.get_uris() # url-unquote the list, strip file:// schemes, handle .desktop-s pathlist = [] app = None for uri in droppeduris: scheme, _, path, _, _ = urlsplit(uri) if scheme != "file": pathlist.append(uri) else: filename = url2pathname(path) desktopentry = DesktopEntry() try: desktopentry.parse(filename) except xdg.Exceptions.ParsingError: pathlist.append(filename) continue if desktopentry.getType() == 'Link': pathlist.append(desktopentry.getURL()) if desktopentry.getType() == 'Application': app = desktopentry.getExec() if app and len(droppeduris) == 1: text = app else: text = str.join("", (shell_quote(path) + " " for path in pathlist)) box.terminal.feed_child(text) return True
def xdg_name_and_icon(app): entry = DesktopEntry() for directory in xdg.BaseDirectory.xdg_data_dirs: path = os.path.join(directory, "applications", f"{app}.desktop") if os.path.exists(path): entry.parse(path) return entry.getName(), entry.getIcon() return None, None
def getDesktopFile(url): #TODO nfs?? url = str(url[7:]) desktop = DesktopEntry() try: desktop.parse(url) except (ParsingError, DuplicateGroupError, DuplicateKeyError) as e: print(e) return None return desktop
def TestOneInput(input_bytes): # We need to make the file an absolute path testfile_path = os.path.join(os.getcwd(), "testfile.desktop") with open(testfile_path, "wb") as f: f.write(input_bytes) # Test DesktopEntry implementation de = DesktopEntry() try: de.parse(testfile_path) except ParsingError as e: os.remove(testfile_path) return de.checkKey(fdp.ConsumeString(20), "value", "core") os.remove(testfile_path)
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 _get_icon_from_desktop_file( workdir: str, desktop_file_paths: List[str]) -> Optional[str]: # Icons in the desktop file can be either a full path to the icon file, or a name # to be searched in the standard locations. If the path is specified, use that, # otherwise look for the icon in the hicolor theme (also covers icon type="stock"). # See https://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html # for further information. for path in desktop_file_paths: entry = DesktopEntry() entry.parse(os.path.join(workdir, path)) icon = entry.getIcon() icon_path = (icon if os.path.isabs(icon) else _get_icon_from_theme( workdir, "hicolor", icon)) return icon_path return None
def test_write_file(self): de = DesktopEntry() de.parse(self.test_file) de.removeKey("Name") de.addGroup("Hallo") de.set("key", "value", "Hallo") new_file = os.path.join(self.tmpdir, "test.desktop") de.write(new_file, trusted=True) with io.open(new_file, encoding='utf-8') as f: contents = f.read() assert "[Hallo]" in contents, contents assert re.search("key\s*=\s*value", contents), contents # This is missing the Name key, and has an unknown Hallo group, so it # shouldn't validate. new_entry = DesktopEntry(new_file) self.assertRaises(ValidationError, new_entry.validate)
def main(args): args =args[1:] if len(args )!=1: print("takes one argument") return try: desktop =DesktopEntry() desktop.parse(args[0]) except: print("parse error" ,args[0]) name =desktop.getName() name =''.join(lazy_pinyin(name)) AppName=desktop.getName() NoDisplay=False; if len(AppName)==0: AppName=desktop.getGenericName AppComment=desktop.getComment() AppIcon=desktop.getIcon() if not os.path.exists(AppIcon): ip=getIconPath(AppIcon,theme="deepin") # if ip is None: # ip=getIconPath(AppIcon,theme="hicolor") else: ip=AppIcon if ip is None: #TODO add defauult icon ip="test" pass AppIcon=ip NoDisplay=desktop.getNoDisplay() onlydi=desktop.getOnlyShowIn() if NoDisplay is True or len(onlydi)>0: AppIcon="" AppName="" AppComment="" NoDisplay="true" else: NoDisplay="false" print(''' <html><head> <title>''' +name+ '''</title> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" > <meta name="AppName" content="'''+AppName+'''" /> <meta name="AppComment" content="'''+AppComment+'''" /> <meta name="AppIcon" content="'''+AppIcon+'''" /> <meta name="AppNoDisplay" content="'''+NoDisplay+'''" /> </head> <body> '''+AppName+''' </body> </html> ''')
class Mapping(object): """ An object representation of a wiican mapping. A wiican mapping must be located in a single directory containing a file with the wminput code, a file containing the metadata (name, description, author, version) and an optional icon file. """ # Mandatory filename for the metadata file info_filename = "info.desktop" # Mandatory filename for the wminput config file mapping_filename = "mapping.wminput" def __init__(self, path=None): """ Builds a mapping object. Parameters: path - scans the path for building a mapping object if the needed files where found. If None it builds an empty mapping. If some of the needed files wasn't found it tries to build a mapping with the found info. The Mapping.info_filename and Mapping.mapping_filename class attributes marks the requiered filenames for the metadata file and wminput config file respectively. The Mapping.mapping_filename file must contain wminput config file code The Mapping.info_filename follows XDG DesktopEntry syntax. The Mapping.info_filename contains the source of the optional associated icon. If no icon found or no icon directive it falls back to default icon. There are three posibilities for icon setting: - An absolute path where the icon it's stored - Icon filename if it's stored in the same dir as Mapping.info_filename - Theme-icon-name for auto-getting the icon from the icon theme """ self.__path = path # Getting freedesktop definition file self.__info = DesktopEntry() if path and os.path.exists(os.path.join(path, Mapping.info_filename)): self.__info.parse(os.path.join(path, Mapping.info_filename)) else: self.__info.new(self.info_filename) self.__info.set("Type", "Wiican Mapping") # Getting wminput mapping file if path and os.path.exists(os.path.join(path, Mapping.mapping_filename)): mapping_fp = open(os.path.join(path, Mapping.mapping_filename), "r") self.__mapping = mapping_fp.read() mapping_fp.close() else: self.__mapping = "" # Getting icon file path icon_name = self.__info.getIcon() if path and icon_name in os.listdir(path): # Icon included self.set_icon(os.path.join(path, icon_name)) elif getIconPath(icon_name): # Theme icon self.set_icon(getIconPath(icon_name)) else: # Default icon self.set_icon(ICON_DEFAULT) def get_path(self): """Returns the absolute path where the wiican mapping it's saved. It returns None if the mapping it's not saved yet""" return self.__path def get_name(self): """Gets the name of the mapping""" return self.__info.getName() def set_name(self, name): """Sets the name for the mapping""" self.__info.set("Name", name) self.__info.set("Name", name, locale=True) def get_comment(self): """Gets the descriptional comment""" return self.__info.getComment() def set_comment(self, comment): """Sets the descriptional comment for the mapping""" self.__info.set("Comment", comment) self.__info.set("Comment", comment, locale=True) def get_icon(self): """ Gets the associated icon. If no icon found or no icon directive it falls back to default icon. """ icon_name = self.__info.getIcon() # Icon included if self.__path and icon_name in os.listdir(self.__path): return os.path.join(self.__path, icon_name) # Theme icon elif getIconPath(icon_name): return getIconPath(icon_name) # Default icon else: return ICON_DEFAULT def set_icon(self, icon_path): """ Sets the icon for the mapping. There are three posibilities for icon setting: - An absolute path where the icon it's stored - Icon filename if it's stored in the same dir as Mapping.info_filename - Theme-icon-name for auto-getting the icon from the icon theme """ self.__info.set("Icon", icon_path) def get_authors(self): """Gets the mapping author/s""" return self.__info.get("X-Authors") def set_authors(self, authors): """Sets the author/s for the mapping""" self.__info.set("X-Authors", authors) def get_version(self): """Gets the version of the mapping""" return self.__info.get("X-Version") def set_version(self, version): """Sets the version of the mapping""" self.__info.set("X-Version", version) def get_mapping(self): """Gets the wminput config code""" return self.__mapping def set_mapping(self, mapping): """Sets the wminput config code""" self.__mapping = mapping def write(self, dest_path=None): """ Saves the mapping object by writing the files in the mapping directory. The metadata it's saved in Mapping.info_filename file. The wminput config code it's saved in Mapping.mapping_filename file. The associated icon it's copied to the mapping directory. """ if not dest_path: if not self.__path: raise MappingError, _("No path provided for writing mapping") dest_path = self.__path elif not os.path.exists(dest_path): os.mkdir(dest_path) icon_path = self.get_icon() icon_filename = os.path.basename(icon_path) if not icon_path == os.path.join(dest_path, icon_filename): shutil.copy(icon_path, dest_path) self.set_icon(icon_filename) self.__info.write(os.path.join(dest_path, Mapping.info_filename)) mapping_fp = open(os.path.join(dest_path, Mapping.mapping_filename), "w") mapping_fp.write(self.__mapping) mapping_fp.close() # Clean not useful files for item in [ x for x in os.listdir(dest_path) if not x in [Mapping.info_filename, Mapping.mapping_filename, icon_filename] ]: os.unlink(os.path.join(dest_path, item)) self.__path = dest_path def __repr__(self): return "Mapping <" + self.__info.get("Name", locale=False) + " " + str(self.__info.getVersion()) + ">"