def on_execute(self, item, action): if item.category() != kp.ItemCategory.REFERENCE: return try: item_target = kpu.kwargs_decode(item.target()) site_name = item_target['site'] except Exception as exc: self.dbg(str(exc)) return if site_name not in self.sites: self.warn( 'Could not execute item "{}". Site "{}" not found.'.format( item.label(), site_name)) return site = self.sites[site_name] if len(item.raw_args()) > 0: for url in site['urls']: final_url = self._url_build(url, item.raw_args(), site['quoting']) kpu.web_browser_command(private_mode=site['incognito'], new_window=site['new_window'], url=final_url, execute=True) else: for home_url in site['home_urls']: kpu.web_browser_command(private_mode=site['incognito'], new_window=site['new_window'], url=home_url, execute=True)
def _parse_selected_target(self, selected_chain): try: selected_target = kpu.kwargs_decode(selected_chain.target()) except Exception: selected_target = selected_chain.target() return selected_target
def on_suggest(self, user_input, items_chain): if not items_chain or items_chain[0].category() != kp.ItemCategory.FILE: return suggestions = [] data_bag = kpu.kwargs_decode(items_chain[0].data_bag()) current_distro = self._distros[data_bag['distro_name']] current_distro_path = os.path.dirname(os.path.abspath(current_distro['exe_file'])) sessions = current_distro['sessions'] for session_name in sessions: displayed_session_name = session_name.replace(os.path.join(current_distro_path, 'Sessions') + os.sep, '') if not user_input or kpu.fuzzy_score(user_input, displayed_session_name) > 0: suggestions.append(self.create_item( category=kp.ItemCategory.REFERENCE, label="{}".format(displayed_session_name), short_desc='Launch "{}" session'.format(displayed_session_name), target=kpu.kwargs_encode( dist=data_bag['distro_name'], session=session_name), args_hint=kp.ItemArgsHint.FORBIDDEN, hit_hint=kp.ItemHitHint.IGNORE)) if user_input: suggestions.append(self.create_item( category=kp.ItemCategory.REFERENCE, label="{}".format(user_input), short_desc='Launch "{}" directly'.format(user_input), target=kpu.kwargs_encode( dist=data_bag['distro_name'], host_name=user_input), args_hint=kp.ItemArgsHint.FORBIDDEN, hit_hint=kp.ItemHitHint.IGNORE)) self.set_suggestions(suggestions, kp.Match.ANY, kp.Sort.NONE)
def on_suggest(self, user_input, items_chain): if not items_chain or items_chain[0].category( ) != kp.ItemCategory.KEYWORD: return suggestions = [] data_bag = kpu.kwargs_decode(items_chain[0].data_bag()) sessions = self._distros[data_bag['distro_name']]['sessions'] for session in sessions: session_name = str(session).replace('\\', '/') if not user_input or kpu.fuzzy_score(user_input, session_name) > 0: suggestions.append( self.create_item( category=kp.ItemCategory.REFERENCE, label=session_name, short_desc='Open "{}" with HeidiSQL'.format( session_name), target=kpu.kwargs_encode( distro_name=data_bag['distro_name'], session=session), args_hint=kp.ItemArgsHint.FORBIDDEN, hit_hint=kp.ItemHitHint.IGNORE)) self.set_suggestions(suggestions, kp.Match.ANY, kp.Sort.NONE)
def on_suggest(self, user_input, items_chain): if not items_chain or items_chain[-1].category( ) != self.ITEMCAT_PROFILE: return user_input = user_input.strip() current_item = items_chain[-1] target_props = kpu.kwargs_decode(current_item.target()) profile_name = target_props['profile'] try: profile = self.profiles[profile_name] except KeyError: self.warn( 'Item definition not found in current config: "{}"'.format( profile_name)) return suggestions = [current_item.clone()] # default item suggestions[0].set_args(user_input) if not user_input: suggestions[0].set_short_desc("Open the search engine home page") self.set_suggestions(suggestions) return # avoid doing unnecessary network requests in case user is still typing if len(user_input) < 2 or self.should_terminate(self.idle_time): return provider_suggestions = [] try: provider_suggestions = profile['provider'].query(self, user_input) if self.should_terminate(): return except urllib.error.HTTPError as exc: suggestions.append( self.create_error_item(label=user_input, short_desc=str(exc))) except Exception as exc: suggestions.append( self.create_error_item(label=user_input, short_desc="Error: " + str(exc))) traceback.print_exc() for provider_suggestion in provider_suggestions: item = current_item.clone() item.set_args(provider_suggestion) #item.set_data_bag(user_input) suggestions.append(item) if not provider_suggestions: # change default item suggestions[0].set_short_desc( "No suggestions found (default action: {})".format( profile['default_action'])) self.set_suggestions(suggestions, kp.Match.ANY, kp.Sort.NONE)
def on_execute(self, item, action): if not item and item.category() != self.ITEMCAT_RESULT: return if not item.data_bag(): return data_bag = kpu.kwargs_decode(item.data_bag()) if not action or action.name() == self.ACTION_DEFAULT: kpu.set_clipboard(data_bag['translation'])
def on_execute(self, item, action): target_props = kpu.kwargs_decode(item.target()) profile_name = target_props['profile'] args = item.raw_args() try: profile = self.profiles[profile_name] except KeyError: self.warn( 'Item definition not found in current config: "{}"'.format( profile_name)) return if not args: # open the search engine home page base = profile['provider'].browse_base try: parts = urllib.parse.urlsplit(base) url = '{}://{}'.format(parts.scheme, parts.netloc) except ValueError: url = base kpu.web_browser_command(url=url, execute=True) return # choose action action_name = action.name() if action else None if not action_name: action_name = profile['default_action'] if action_name and action_name not in self.actions_names: self.warn( 'Unknown action "{}". Falling back to default: {}'.format( action_name, self.DEFAULT_ACTION)) action_name = self.DEFAULT_ACTION if not action_name: action_name = self.DEFAULT_ACTION # browse or copy url if action_name in (self.ACTION_BROWSE, self.ACTION_BROWSE_PRIVATE, self.ACTION_COPY_URL): url = profile['provider'].build_browse_url(args) # copy url if action_name == self.ACTION_COPY_URL: kpu.set_clipboard(url) # launch browser else: private_mode = True if action_name == self.ACTION_BROWSE_PRIVATE else None kpu.web_browser_command(private_mode=private_mode, url=url, execute=True) # default action: copy result (ACTION_COPY_RESULT) else: kpu.set_clipboard(args)
def on_execute(self, item, action): if item.category() == kp.ItemCategory.FILE: kpu.execute_default_action(self, item, action) return if item.category() != kp.ItemCategory.REFERENCE: return # extract info from item's target property try: item_target = kpu.kwargs_decode(item.target()) distro_name = item_target['dist'] if 'session' in item_target: session_name = item_target['session'] else: session_name = None if 'host_name' in item_target: host_name = item_target['host_name'] else: host_name = None except Exception as e: self.dbg(e) return # check if the desired distro is available and enabled if distro_name not in self._distros: self.warn( 'Could not execute item "{}". Distro "{}" not found.'.format( item.label(), distro_name)) return distro = self._distros[distro_name] if not distro['enabled']: self.warn( 'Could not execute item "{}". Distro "{}" is disabled.'.format( item.label(), distro_name)) return if host_name: kpu.shell_execute(distro['exe_file'], args=host_name) return # check if the desired session still exists if session_name not in distro['sessions']: self.warn( 'Could not execute item "{}". Session "{}" not found in distro "{}".' .format(item.label(), session_name, distro_name)) return # find the placeholder of the session name in the args list and execute sidx = distro['cmd_args'].index('%1') kpu.shell_execute(distro['exe_file'], args=distro['cmd_args'][0:sidx] + [session_name] + distro['cmd_args'][sidx + 1:])
def on_execute(self, item, action): if not item and item.category() != kp.ItemCategory.EXPRESSION: return if not item.data_bag(): return data_bag = kpu.kwargs_decode(item.data_bag()) if not action or action.name() == self.ACTION_DEFAULT: kpu.set_clipboard(data_bag['translation_result']) elif action.name() == self.ACTION_OPEN_LEO: kpu.web_browser_command( url=self.URL_LEO.format(**data_bag), execute=True )
def on_suggest(self, user_input: str, items_chain: Sequence): self.dbg('on_suggest') if not items_chain: return [] initial_item = items_chain[0] current_items = items_chain[1:len(items_chain)] current_suggestion_type: SuggestionMode = self.get_current_suggestionmode( items_chain) suggestions = [] actual_user_input = self.filter_prefix if len(items_chain) == 1 else "" previous_effective_value = "" if (len(current_items) > 0): current_item = current_items[-1] previous_effective_value = kpu.kwargs_decode( current_item.data_bag())['effective_value'] actual_user_input += previous_effective_value actual_user_input += user_input self.print(actual_user_input=actual_user_input, user_input=user_input) self.print(is_filter=str(current_suggestion_type)) if current_suggestion_type == SuggestionMode.Filter: self.add_filter_suggestions(actual_user_input, suggestions) else: self.add_issues_matching_filter(actual_user_input, suggestions) suggestions.append( self.plugin.create_item( category=self.plugin.ITEMCAT_SWITCH, label="Switch ⇆", short_desc="Switch between filter suggestions and issue list", target="switch", args_hint=kp.ItemArgsHint.ACCEPTED, hit_hint=kp.ItemHitHint.IGNORE, icon_handle=self.plugin._icons[self.filter_icon], loop_on_suggest=True, data_bag=kpu.kwargs_encode( url=self.api.create_issues_url(previous_effective_value), effective_value=previous_effective_value))) # avoid flooding YouTrack with too many unnecessary queries in # case user is still typing her search if self.plugin.should_terminate(self.plugin.idle_time): return [] if initial_item.category() == self.plugin.ITEMCAT_SWITCH: return [] return suggestions
def on_suggest(self, user_input: str, items_chain: List): if not items_chain or items_chain[0].category() not in [ self.ITEMCAT_FILTER, self.ITEMCAT_ISSUES, self.ITEMCAT_SWITCH ]: return current_item = items_chain[0] target_props = kpu.kwargs_decode(current_item.target()) server_name = target_props['server'] try: server = self.servers[server_name] except KeyError: self.warn( 'Item definition not found in current config: "{}"'.format( server_name)) return suggestions = [current_item.clone()] # default item suggestions[0].set_args(user_input) # avoid doing unnecessary network requests in case user is still typing if self.should_terminate(self.idle_time): return server_suggestions = [] try: server_suggestions = server.on_suggest(user_input, items_chain) self.dbg("len=" + str(len(server_suggestions))) if self.should_terminate(): return except urllib.error.HTTPError as exc: server_suggestions.append( self.create_error_item(label=user_input, short_desc=str(exc))) except Exception as exc: server_suggestions.append( self.create_error_item(label=user_input, short_desc="Error: " + str(exc))) traceback.print_exc() if not server_suggestions: # change default item server_suggestions[0].set_short_desc( "No suggestions found (default action: open browser)") self.set_suggestions(server_suggestions, kp.Match.ANY, kp.Sort.NONE)
def on_execute(self, item, action): if item.category() == kp.ItemCategory.FILE: kpu.execute_default_action(self, item, action) return if item.category() != kp.ItemCategory.REFERENCE: return # extract info from item's target property try: item_target = kpu.kwargs_decode(item.target()) distro_name = item_target['dist'] if 'session' in item_target: session_name = item_target['session'] else: session_name = None if 'host_name' in item_target: host_name = item_target['host_name'] else: host_name = None except Exception as e: self.dbg(e) return # check if the desired distro is available and enabled if distro_name not in self._distros: self.warn('Could not execute item "{}". Distro "{}" not found.'.format(item.label(), distro_name)) return distro = self._distros[distro_name] if not distro['enabled']: self.warn('Could not execute item "{}". Distro "{}" is disabled.'.format(item.label(), distro_name)) return if host_name: kpu.shell_execute(distro['exe_file'], args=host_name) return # check if the desired session still exists if session_name not in distro['sessions']: self.warn('Could not execute item "{}". Session "{}" not found in distro "{}".'.format(item.label(), session_name, distro_name)) return # find the placeholder of the session name in the args list and execute sidx = distro['cmd_args'].index('%1') kpu.shell_execute( distro['exe_file'], args=distro['cmd_args'][0:sidx] + [session_name.replace(' ', '%20')] + distro['cmd_args'][sidx+1:])
def on_execute(self, item, action): self.dbg('on_execute') if not item or not item.data_bag(): return data_bag = kpu.kwargs_decode(item.data_bag()) if not data_bag: return # ACTION_KEY_DEFAULT if not action or action.name() == self.ACTION_BROWSE: webbrowser.open(data_bag['url']) elif action.name() == self.ACTION_COPY_URL: kpu.set_clipboard(data_bag['url']) elif action.name() == self.ACTION_COPY_RESULT and item.category( ) != self.ITEMCAT_ISSUES: kpu.set_clipboard(data_bag['effective_value'])
def on_suggest(self, user_input, items_chain): if not items_chain: return current_item = items_chain[-1] params = kpu.kwargs_decode( current_item.data_bag()) if current_item.data_bag() else None if current_item.category() == self.ITEMCAT_CONTACT: self.suggest_actions(current_item, params) elif current_item.category( ) == kp.ItemCategory.REFERENCE and user_input: self.suggest_contacts(current_item, params, user_input) elif current_item.category() == self.ITEMCAT_ACTION: self.suggest_copy(current_item, params) else: if self._debug: self.dbg(f"on_suggest ignored")
def on_execute(self, item, action): target_props = kpu.kwargs_decode(item.target()) profile_name = target_props['profile'] try: profile = self.profiles[profile_name] except KeyError: self.warn('Item definition not found in current config: "{}"'.format(profile_name)) return # choose action action_name = action.name() if action else None if not action_name: action_name = profile['default_action'] if action_name and action_name not in self.actions_names: self.warn( 'Unknown action "{}". Falling back to default: {}'.format( action_name, self.DEFAULT_ACTION)) action_name = self.DEFAULT_ACTION if not action_name: action_name = self.DEFAULT_ACTION # browse or copy url if action_name in (self.ACTION_BROWSE, self.ACTION_BROWSE_PRIVATE, self.ACTION_COPY_URL): url = profile['provider'].build_browse_url(item.raw_args()) # copy url if action_name == self.ACTION_COPY_URL: kpu.set_clipboard(url) # launch browser else: if action_name == self.ACTION_BROWSE_PRIVATE: private_mode = True else: private_mode = None kpu.web_browser_command(private_mode=private_mode, url=url, execute=True) # default action: copy result (ACTION_COPY_RESULT) else: kpu.set_clipboard(item.raw_args())
def on_suggest(self, user_input, items_chain): if not items_chain or items_chain[0].category( ) != kp.ItemCategory.FILE: return suggestions = [] data_bag = kpu.kwargs_decode(items_chain[0].data_bag()) current_distro = self._distros[data_bag['distro_name']] current_distro_path = os.path.dirname( os.path.abspath(current_distro['exe_file'])) sessions = current_distro['sessions'] for session_name in sessions: displayed_session_name = session_name.replace( os.path.join(current_distro_path, 'Sessions') + os.sep, '') if not user_input or kpu.fuzzy_score(user_input, displayed_session_name) > 0: suggestions.append( self.create_item(category=kp.ItemCategory.REFERENCE, label="{}".format(displayed_session_name), short_desc='Launch "{}" session'.format( displayed_session_name), target=kpu.kwargs_encode( dist=data_bag['distro_name'], session=session_name), args_hint=kp.ItemArgsHint.FORBIDDEN, hit_hint=kp.ItemHitHint.IGNORE)) if user_input: suggestions.append( self.create_item( category=kp.ItemCategory.REFERENCE, label="{}".format(user_input), short_desc='Launch "{}" directly'.format(user_input), target=kpu.kwargs_encode(dist=data_bag['distro_name'], host_name=user_input), args_hint=kp.ItemArgsHint.FORBIDDEN, hit_hint=kp.ItemHitHint.IGNORE)) self.set_suggestions(suggestions, kp.Match.ANY, kp.Sort.NONE)
def on_suggest(self, user_input, items_chain): if not items_chain or items_chain[0].category() != kp.ItemCategory.FILE: return suggestions = [] data_bag = kpu.kwargs_decode(items_chain[0].data_bag()) sessions = self._distros[data_bag['distro_name']]['sessions'] for session_name in sessions: if not user_input or kpu.fuzzy_score(user_input, session_name) > 0: suggestions.append(self.create_item( category=kp.ItemCategory.REFERENCE, label="{}".format(session_name), short_desc='Launch "{}" session'.format(session_name), target=kpu.kwargs_encode( dist=data_bag['distro_name'], session=session_name), args_hint=kp.ItemArgsHint.FORBIDDEN, hit_hint=kp.ItemHitHint.IGNORE)) self.set_suggestions(suggestions, kp.Match.ANY, kp.Sort.NONE)
def on_execute(self, item, action): """Perform an action related with the selected item/action item.""" item_category = item.category() item_label = item.label() try: item_target = kpu.kwargs_decode(item.target()) except Exception: item_target = item.target() # Open in browser if there it is an URL in the target if 'url' in item_target: url = item_target['url'] # If it is a moviedb url to be opened, append the language query if constants.URL_MOVIEDB_BASE in url: url += '?language={}'.format(self.settings['language']) kpu.web_browser_command(url=url, execute=True) # Copy text if item_category == TextSuggestion.ITEM_CAT: kpu.set_clipboard(item_label)
def on_suggest(self, user_input, items_chain): if not items_chain or items_chain[0].category() != kp.ItemCategory.KEYWORD: return suggestions = [] data_bag = kpu.kwargs_decode(items_chain[0].data_bag()) sessions = self._distros[data_bag['distro_name']]['sessions'] for session in sessions: session_name = str(session).rpartition('\\')[2] if not user_input or kpu.fuzzy_score(user_input, session_name) > 0: suggestions.append(self.create_item( category=kp.ItemCategory.REFERENCE, label=session_name, short_desc='Open "{}" with HeidiSQL'.format(session_name), target=kpu.kwargs_encode( distro_name=data_bag['distro_name'], session=session ), args_hint=kp.ItemArgsHint.FORBIDDEN, hit_hint=kp.ItemHitHint.IGNORE )) self.set_suggestions(suggestions, kp.Match.ANY, kp.Sort.NONE)
def on_execute(self, item, action): if (not item): return selection = item.target() params = kpu.kwargs_decode(item.data_bag()) verb_name = params['verb_name'] contact_no = int(params['contact_no']) contact = self.contacts[contact_no] verb = self.VERBS[verb_name] if self._debug: self.dbg( f"Executing {verb_name}: {selection}, defaultAction: {item.category() == self.ITEMCAT_CONTACT}\n" ) if verb.action == self.ACTION_CALL: self.do_call_action(contact, selection, self.cell_protocol) elif verb.action == self.ACTION_MAIL: self.do_mail_action(contact, verb, self.mail_protocol) elif verb.action == self.ACTION_CARD: self.do_card_action(contact) else: kpu.set_clipboard(selection)