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
Пример #2
0
    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
Пример #3
0
 def on_suggest(self, user_input, items_chain):
     if items_chain and items_chain[-1].category() == kp.ItemCategory.FILE:
         current_item = items_chain[-1]
         path = current_item.target()
         if os.path.isdir(path):
             # File is a directory
             suggestions, match_method, sort_method = self._browse_dir(path)
             self.set_suggestions(suggestions, match_method, sort_method)
         elif os.path.splitext(path)[1].lower() == ".lnk":
             # File is a link
             try:
                 link_props = kpu.read_link(path)
                 if os.path.isdir(link_props['target']):
                     # Link points to a directory
                     dir_target = link_props['target']
                     suggestions, match_method, sort_method = self._browse_dir(
                                         dir_target, check_base_dir=False)
                     self.set_suggestions(suggestions, match_method, sort_method)
             except:
                 pass
         else:
             clone = items_chain[-1].clone()
             clone.set_args(user_input)
             self.set_suggestions([clone])
Пример #4
0
    def on_suggest(self, user_input, items_chain):
        orig_user_input = user_input
        if len(user_input) > 0:
            user_input = os.path.normpath(user_input)

        # initial search, home dir(s)
        if not items_chain and len(
                self.home_trigger) > 0 and orig_user_input.lower().startswith(
                    self.home_trigger.lower()):
            suggestions, match_method, sort_method = self._home_suggestions(
                orig_user_input[len(self.home_trigger):])
            self.set_suggestions(suggestions, match_method, sort_method)

        # initial search, "\" or "/"
        elif not items_chain and user_input == os.sep:
            suggestions = self._drives_suggestions()
            self.set_suggestions(suggestions, kp.Match.ANY, kp.Sort.LABEL_ASC)

        # initial search, user_input format is like "X:"
        elif not items_chain and (len(user_input) == 2 and user_input[1] == ":"
                                  and user_input[0].upper()
                                  in string.ascii_uppercase):
            suggestions, match_method, sort_method = self._browse_dir(
                user_input + os.sep)
            self.set_suggestions(suggestions, match_method, sort_method)

        # initial search, user_input is an absolute path
        elif not items_chain and os.path.isabs(user_input):
            if user_input.endswith(os.sep):
                # path is expected to be a directory
                suggestions, match_method, sort_method = self._browse_dir(
                    user_input)
                self.set_suggestions(suggestions, match_method, sort_method)
            elif os.path.isdir(user_input):
                # path is a directory
                suggestions, match_method, sort_method = self._browse_dir(
                    user_input, check_base_dir=False)
                self.set_suggestions(suggestions, match_method, sort_method)
            elif os.path.exists(user_input):
                # user_input is an item of the filesystem
                self.set_suggestions([
                    self.create_item(category=kp.ItemCategory.FILE,
                                     label=os.path.basename(user_input),
                                     short_desc="",
                                     target=user_input,
                                     args_hint=kp.ItemArgsHint.ACCEPTED,
                                     hit_hint=kp.ItemHitHint.KEEPALL,
                                     loop_on_suggest=True)
                ], kp.Match.ANY, kp.Sort.NONE)
            else:
                # path is expected to be a directory suffixed by search terms
                base_dir, search_terms = os.path.split(user_input)
                suggestions, match_method, sort_method = self._browse_dir(
                    base_dir, search_terms=search_terms, store_score=True)
                if len(search_terms) > 0:
                    # Because of the self.home_trigger prefix, the user_input cannot
                    # be matched against files names. So we have to sort the
                    # suggestions by ourselves.
                    self._sort_matched_suggestions(suggestions)
                    match_method = kp.Match.ANY
                    sort_method = kp.Sort.NONE
                self.set_suggestions(suggestions, match_method, sort_method)

        # current item is a FILE
        elif items_chain and items_chain[-1].category(
        ) == kp.ItemCategory.FILE:
            current_item = items_chain[-1]

            # check file's attributes
            if os.path.isdir(current_item.target()):
                dir_target = current_item.target()
                exists = True
            elif os.path.exists(current_item.target()):
                dir_target = None
                exists = True
                # check if file is a link pointing to a directory, in which case
                # we want to browse it
                if self.follow_shell_links and os.path.splitext(
                        current_item.target())[1].lower() == ".lnk":
                    try:
                        link_props = kpu.read_link(current_item.target())
                        if os.path.isdir(link_props['target']):
                            dir_target = link_props['target']
                    except:
                        pass
            else:
                dir_target = None
                exists = False

            if dir_target is not None:
                suggestions, match_method, sort_method = self._browse_dir(
                    dir_target, check_base_dir=False, search_terms=user_input)
                self.set_suggestions(suggestions, match_method, sort_method)
            elif exists:
                clone = current_item.clone()
                clone.set_args(orig_user_input)
                clone.set_loop_on_suggest(False)
                self.set_suggestions([clone], kp.Match.ANY, kp.Sort.NONE)
            else:
                self.set_suggestions([
                    self.create_error_item(label=orig_user_input,
                                           short_desc="File/Dir not found: " +
                                           current_item.target())
                ])