def _add_from_recent_directory(self, catalog): recent_directory = os.path.join( kpu.shell_known_folder_path(FOLDERID.RoamingAppData.value), "Microsoft", "Windows", "Recent" ) try: recent_links = kpu.scan_directory( recent_directory, "*.lnk", kpu.ScanFlags.FILES, max_level=0 ) except OSError as e: self.dbg(str(e)) return for link_name in recent_links: link_path = os.path.join(recent_directory, link_name) item = self.create_item( category=kp.ItemCategory.FILE, label=os.path.splitext(link_name)[0], short_desc="Recent link: " + link_path, target=link_path, args_hint=kp.ItemArgsHint.ACCEPTED, hit_hint=kp.ItemHitHint.KEEPALL ) catalog.append(item)
def _autodetect_startmenu(self, exe_name, name_pattern): known_folders = ( "{625b53c3-ab48-4ec1-ba1f-a1ef4146fc19}", # FOLDERID_StartMenu "{a4115719-d62e-491d-aa7c-e74b8be3b067}") # FOLDERID_CommonStartMenu found_link_files = [] for kf_guid in known_folders: try: known_dir = kpu.shell_known_folder_path(kf_guid) found_link_files += [ os.path.join(known_dir, f) for f in kpu.scan_directory( known_dir, name_pattern, kpu.ScanFlags.FILES, -1)] except Exception as exc: self.dbg(str(exc)) pass for link_file in found_link_files: try: link_props = kpu.read_link(link_file) if (link_props['target'].lower().endswith(exe_name) and os.path.exists(link_props['target'])): return link_props['target'] except Exception as exc: self.dbg(str(exc)) pass return None
def on_catalog(self): if not self.scan_env_path: self.set_catalog([]) return start = time.perf_counter() self.path_str, self.path = self._read_env_path() self.pathext_str, self.pathext = self._read_env_pathext() catalog = [] for path_dir in self.path: try: entries = kpu.scan_directory(path_dir, self.pathext, kpu.ScanFlags.FILES, max_level=0) except OSError as exc: #self.dbg("Exception raised while scanning PATH:", exc) continue if self.should_terminate(): return for entry in entries: entry_path = os.path.normpath(os.path.join(path_dir, entry)) catalog.append( self.create_item(category=kp.ItemCategory.FILE, label=os.path.basename(entry), short_desc="", target=entry_path, args_hint=kp.ItemArgsHint.ACCEPTED, hit_hint=kp.ItemHitHint.KEEPALL)) self.set_catalog(catalog) self._log_catalog_duration(start, len(catalog))
def _autodetect_startmenu(self, exe_name, name_pattern): known_folders = ( "{625b53c3-ab48-4ec1-ba1f-a1ef4146fc19}", # FOLDERID_StartMenu "{a4115719-d62e-491d-aa7c-e74b8be3b067}") # FOLDERID_CommonStartMenu found_link_files = [] for kf_guid in known_folders: try: known_dir = kpu.shell_known_folder_path(kf_guid) found_link_files += [ os.path.join(known_dir, f) for f in kpu.scan_directory( known_dir, name_pattern, kpu.ScanFlags.FILES, -1)] except Exception as e: self.dbg(e) pass for link_file in found_link_files: try: link_props = kpu.read_link(link_file) if (link_props['target'].lower().endswith(exe_name) and os.path.exists(link_props['target'])): return link_props['target'] except Exception as e: self.dbg(e) pass return None
def _load_dir(self, i, config): if config['name'] is None: self.warn("No 'name' provided for config #{}".format(i + 1)) return 0 path_name = config['name'].replace('\\\\', '\\') root_path = os.path.expandvars(path_name) if not os.path.exists(root_path): self.warn("Path '{}' in config #{} does not exist".format( path_name, i + 1)) return 0 paths = [] for glob in config['types'].split(','): if glob.strip() in ['', '@Invalid()']: continue self.should_terminate() files = kpu.scan_directory(root_path, name_patterns=glob.strip(), flags=kpu.ScanFlags.FILES, max_level=config['depth']) paths.extend(files) if config['indexdirs']: self.should_terminate() dirs = kpu.scan_directory(root_path, name_patterns='*', flags=kpu.ScanFlags.DIRS, max_level=config['depth']) paths.extend(dirs) self.merge_catalog([ self.create_item(category=kp.ItemCategory.FILE, label=os.path.basename(path), short_desc="", target=os.path.join(root_path, path), args_hint=kp.ItemArgsHint.ACCEPTED, hit_hint=kp.ItemHitHint.KEEPALL) for path in paths ]) return len(paths)
def list_bookmarks(self): favorites_dirs = self.settings.get_multiline("favorites_dirs", self.config_section) if not favorites_dirs: favorites_dirs = [ kpu.shell_known_folder_path( "{1777f761-68ad-4d8a-87bd-30b759fa33dd}") ] bookmarks = [] for favdir in favorites_dirs: favdir = os.path.normpath(favdir) try: url_files = kpu.scan_directory(favdir, "*.url", kpu.ScanFlags.FILES, -1) except OSError as exc: print(self.__class__.__name__ + ":", exc, file=sys.stderr) continue for url_file in url_files: url_file = os.path.join(favdir, url_file) #bookmarks.append(self.plugin.create_item( # category=kp.ItemCategory.FILE, # label=os.path.splitext(os.path.basename(url_file))[0], # short_desc=self.label, # trick to transport provider's label # target=url_file, # args_hint=kp.ItemArgsHint.FORBIDDEN, # hit_hint=kp.ItemHitHint.NOARGS)) try: bk_url = None with kpu.chardet_open(url_file, mode="rt") as fh: in_url_section = False for line in fh: line = line.strip() if not in_url_section: if line.lower() == "[internetshortcut]": in_url_section = True else: if line.lower().startswith("url="): bk_url = line[len("url="):].strip() break if bk_url: bk_label = os.path.splitext( os.path.basename(url_file))[0] bookmarks.append(Bookmark(self.label, bk_label, bk_url)) except Exception as exc: self.plugin.warn( "Failed to read URL file: {}. Error: {}".format( url_file, exc)) return bookmarks
def _catalog_knownfolder(self, kf_guid, kf_label, kf_desc, recursive_scan): try: kf_path = kpu.shell_known_folder_path(kf_guid) except OSError: self.warn("Failed to get path of known folder {}".format(kf_label)) return [] if self.should_terminate(): return [] max_scan_level = -1 if recursive_scan else 0 try: files = kpu.scan_directory(kf_path, ('*'), flags=kpu.ScanFlags.FILES, max_level=max_scan_level) except IOError: return [] if self.should_terminate(): return [] catalog = [] for relative in files: f = os.path.normpath(os.path.join(kf_path, relative)) label = os.path.splitext(os.path.basename(f))[0] desc = os.path.dirname(relative) if not len(desc): desc = os.path.basename(kf_path) if not len(desc): desc = label desc = kf_desc + ": " + desc catalog.append( self.create_item(category=kp.ItemCategory.FILE, label=label, short_desc=desc, target=f, args_hint=kp.ItemArgsHint.ACCEPTED, hit_hint=kp.ItemHitHint.KEEPALL)) return catalog
def list_bookmarks(self): bookmarks = [] bookmarks_files = self.settings.get_multiline("bookmarks_files", self.config_section) if bookmarks_files: for f in bookmarks_files: bookmarks += self._read_bookmarks(os.path.normpath(f)) else: srcdir_candidates = self.profile_dir_candidates() if not srcdir_candidates: return [] for chrome_dir in srcdir_candidates: try: profile_dirs = kpu.scan_directory(chrome_dir, flags=kpu.ScanFlags.DIRS, max_level=0) except OSError: profile_dirs = [] for profile_dir in profile_dirs: bookmarks += self._read_bookmarks( os.path.join(chrome_dir, profile_dir, "Bookmarks")) return bookmarks
def on_catalog(self): start = time.perf_counter() self.pathext_str, self.pathext = self._read_env_pathext() catalog = [] for user_extra_path in self.extra_paths: user_extra_path = user_extra_path.replace("/", os.sep) has_trailing_sep = user_extra_path.endswith(os.sep) if user_extra_path.startswith('::') and len(user_extra_path) >= 38: (guid, tail) = (user_extra_path[2:].split(os.sep, maxsplit=1) + [None] * 2)[:2] try: kf_path = kpu.shell_known_folder_path(guid) if tail is not None: user_extra_path = os.path.normpath( os.path.join(kf_path, tail)) else: user_extra_path = kf_path except OSError: self.warn( "Failed to get path of known folder from setting \"{}\"" .format(user_extra_path)) continue else: user_extra_path = os.path.normpath(user_extra_path) #user_extra_path = os.path.expandvars(user_extra_path) # not needed if has_trailing_sep: user_extra_path += os.sep recursive_glob = "**" in user_extra_path for globbed_path in glob.iglob(user_extra_path, recursive=recursive_glob): if self.should_terminate(): return files = [] if os.path.isdir(globbed_path): try: files = kpu.scan_directory(globbed_path, self.pathext, kpu.ScanFlags.FILES, max_level=0) except IOError as exc: self.warn(exc) continue files = [os.path.join(globbed_path, f) for f in files] elif os.path.isfile(globbed_path): files = [globbed_path] else: continue # duh?! if self.should_terminate(): return for f in files: catalog.append( self.create_item(category=kp.ItemCategory.FILE, label=os.path.basename(f), short_desc=f, target=f, args_hint=kp.ItemArgsHint.ACCEPTED, hit_hint=kp.ItemHitHint.KEEPALL)) if len(catalog) % 100 == 0 and self.should_terminate(): return self.set_catalog(catalog) if len(self.extra_paths): self._log_catalog_duration(start, len(catalog))