def _find_pdf_viewers(self): pdf_viewers = {} if platform.system() == "Linux": # Find all .desktop files for search_path in [ "/usr/share/applications", "/usr/local/share/applications", os.path.expanduser("~/.local/share/applications"), ]: try: for filename in os.listdir(search_path): full_filename = os.path.join(search_path, filename) if os.path.splitext(filename)[1] == ".desktop": # See which ones can open PDFs desktop_entry = DesktopEntry(full_filename) if ("application/pdf" in desktop_entry.getMimeTypes() and desktop_entry.getName() != "dangerzone"): pdf_viewers[desktop_entry.getName( )] = desktop_entry.getExec() except FileNotFoundError: pass return pdf_viewers
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 _find_pdf_viewers(self): pdf_viewers = {} if platform.system() == "Darwin": # Get all installed apps that can open PDFs bundle_identifiers = LaunchServices.LSCopyAllRoleHandlersForContentType( "com.adobe.pdf", CoreServices.kLSRolesAll ) for bundle_identifier in bundle_identifiers: # Get the filesystem path of the app res = LaunchServices.LSCopyApplicationURLsForBundleIdentifier( bundle_identifier, None ) if res[0] is None: continue app_url = res[0][0] app_path = str(app_url.path()) # Load its plist file plist_path = os.path.join(app_path, "Contents/Info.plist") # Skip if there's not an Info.plist if not os.path.exists(plist_path): continue with open(plist_path, "rb") as f: plist_data = f.read() plist_dict = plistlib.loads(plist_data) if plist_dict.get("CFBundleName") and plist_dict["CFBundleName"] != "Dangerzone": pdf_viewers[plist_dict["CFBundleName"]] = bundle_identifier elif platform.system() == "Linux": # Find all .desktop files for search_path in [ "/usr/share/applications", "/usr/local/share/applications", os.path.expanduser("~/.local/share/applications"), ]: try: for filename in os.listdir(search_path): full_filename = os.path.join(search_path, filename) if os.path.splitext(filename)[1] == ".desktop": # See which ones can open PDFs desktop_entry = DesktopEntry(full_filename) if ( "application/pdf" in desktop_entry.getMimeTypes() and desktop_entry.getName() != "dangerzone" ): pdf_viewers[ desktop_entry.getName() ] = desktop_entry.getExec() except FileNotFoundError: pass return pdf_viewers
def test_values(self): entry = DesktopEntry(self.test_file) self.assertEqual(entry.getName(), 'gedit') self.assertEqual(entry.getGenericName(), 'Text Editor') self.assertEqual(entry.getNoDisplay(), False) self.assertEqual(entry.getComment(), 'Edit text files') self.assertEqual(entry.getIcon(), 'accessories-text-editor') self.assertEqual(entry.getHidden(), False) self.assertEqual(entry.getOnlyShowIn(), []) self.assertEqual(entry.getExec(), 'gedit %U') self.assertEqual(entry.getTerminal(), False) self.assertEqual(entry.getMimeTypes(), ['text/plain']) self.assertEqual(entry.getCategories(), ['GNOME', 'GTK', 'Utility', 'TextEditor']) self.assertEqual(entry.getTerminal(), False)