def __init__(self): setup_logging() self.extension_id = os.path.basename(os.path.dirname(sys.argv[0])) self.logger = logging.getLogger(self.extension_id) self._listeners = defaultdict(list) self._client = Client(self) self.preferences = {} try: self.preferences = json.loads( os.environ.get("EXTENSION_PREFERENCES", "{}")) except Exception: pass if not self.preferences: self.logger.error("Could not load user preferences") # subscribe with methods if user has added their own if self.__class__.on_query_change is not Extension.on_query_change: self.subscribe(KeywordQueryEvent, 'on_query_change') if self.__class__.on_item_enter is not Extension.on_item_enter: self.subscribe(ItemEnterEvent, 'on_item_enter') if self.__class__.on_unload is not Extension.on_unload: self.subscribe(UnloadEvent, 'on_unload') if self.__class__.on_preferences_update is not Extension.on_preferences_update: self.subscribe(PreferencesUpdateEvent, 'on_preferences_update')
class Extension: """ Manages extension runtime """ def __init__(self): self._listeners = defaultdict(list) self._client = Client(self) self.preferences = {} self.logger = logging.getLogger(__name__) setup_logging() def subscribe(self, event_type, event_listener): """ Example: extension.subscribe(PreferencesEvent, PreferencesEventListener()) :param type event_type: :param ~ulauncher.api.client.EventListener.EventListener event_listener: """ self._listeners[event_type].append(event_listener) def get_listeners_for_event(self, event): """ :param ~ulauncher.api.shared.event.BaseEvent event: :rtype: ~ulauncher.api.client.EventListener.EventListener """ return self._listeners[type(event)] def trigger_event(self, event): """ :param ~ulauncher.api.shared.event.BaseEvent event: """ listeners = self.get_listeners_for_event(event) if not listeners: self.logger.debug('No listeners for event %s', type(event).__name__) return for listener in listeners: action = listener.on_event(event, self) if action: assert isinstance( action, BaseAction ), "on_event return value is not an instance of BaseAction" self._client.send(Response(event, action)) def run(self): """ Subscribes to events and connects to Ulauncher WS server """ self.subscribe(PreferencesEvent, PreferencesEventListener()) self.subscribe(PreferencesUpdateEvent, PreferencesUpdateEventListener()) self._client.connect()
def __init__(self): self.extension_id = get_extension_name() self._listeners = defaultdict(list) self._client = Client(self) self.preferences = {} self.logger = logging.getLogger(__name__) setup_logging() # subscribe with methods if user has added their own if self.__class__.on_query_change is not Extension.on_query_change: self.subscribe(KeywordQueryEvent, self, 'on_query_change') if self.__class__.on_item_enter is not Extension.on_item_enter: self.subscribe(ItemEnterEvent, self, 'on_item_enter') if self.__class__.on_system_exit is not Extension.on_system_exit: self.subscribe(SystemExitEvent, self, 'on_system_exit') if self.__class__.on_preferences is not Extension.on_preferences: self.subscribe(PreferencesEvent, self, 'on_preferences') if self.__class__.on_preferences_update is not Extension.on_preferences_update: self.subscribe(PreferencesUpdateEvent, self, 'on_preferences_update')
class Extension: """ Manages extension runtime """ def __init__(self): setup_logging() self.extension_id = os.path.basename(os.path.dirname(sys.argv[0])) self.logger = logging.getLogger(self.extension_id) self._listeners = defaultdict(list) self._client = Client(self) self.preferences = {} try: self.preferences = json.loads( os.environ.get("EXTENSION_PREFERENCES", "{}")) except Exception: pass if not self.preferences: self.logger.error("Could not load user preferences") # subscribe with methods if user has added their own if self.__class__.on_query_change is not Extension.on_query_change: self.subscribe(KeywordQueryEvent, 'on_query_change') if self.__class__.on_item_enter is not Extension.on_item_enter: self.subscribe(ItemEnterEvent, 'on_item_enter') if self.__class__.on_unload is not Extension.on_unload: self.subscribe(UnloadEvent, 'on_unload') if self.__class__.on_preferences_update is not Extension.on_preferences_update: self.subscribe(PreferencesUpdateEvent, 'on_preferences_update') def subscribe(self, event_type: Type[BaseEvent], listener: Union[str, object]): """ Example: extension.subscribe(KeywordQueryEvent, "on_query_change") """ method_name = None if isinstance(listener, str): method_name = listener listener = self self._listeners[event_type].append((listener, method_name)) def trigger_event(self, event: BaseEvent): event_type = type(event) listeners = self._listeners[event_type] if not listeners: self.logger.debug('No listeners for event %s', event_type.__name__) return for listener, method_name in listeners: method = getattr(listener, method_name or "on_event") # We can use method_name to determine if listener was added the old way or the new class method way # Pass the event args if method_name isn't None, otherwise event and self for backwards compatibility action = method( *event.args) if method_name else method(event, self) if action: assert isinstance( action, (list, BaseAction )), "on_event must return list of Results or a BaseAction" self._client.send(Response(event, action)) def run(self): """ Subscribes to events and connects to Ulauncher socket server """ self.subscribe(PreferencesUpdateEvent, PreferencesUpdateEventListener()) self._client.connect() def on_query_change(self, query): pass def on_item_enter(self, data): pass def on_preferences_update(self, id, value, previous_value): pass def on_unload(self): pass
def __init__(self): self._listeners = defaultdict(list) self._client = Client(self) self.preferences = {} self.logger = logging.getLogger(__name__) setup_logging()
def client(self, extension, framer, sock_client): client = Client(extension) client.framer = framer client.client = sock_client return client
def client(self, extension): return Client(extension, ws_api_url="ws://localhost:5000/test_extension")
class Extension: """ Manages extension runtime """ def __init__(self): self.extension_id = get_extension_name() self._listeners = defaultdict(list) self._client = Client(self) self.preferences = {} self.logger = logging.getLogger(__name__) setup_logging() # subscribe with methods if user has added their own if self.__class__.on_query_change is not Extension.on_query_change: self.subscribe(KeywordQueryEvent, self, 'on_query_change') if self.__class__.on_item_enter is not Extension.on_item_enter: self.subscribe(ItemEnterEvent, self, 'on_item_enter') if self.__class__.on_system_exit is not Extension.on_system_exit: self.subscribe(SystemExitEvent, self, 'on_system_exit') if self.__class__.on_preferences is not Extension.on_preferences: self.subscribe(PreferencesEvent, self, 'on_preferences') if self.__class__.on_preferences_update is not Extension.on_preferences_update: self.subscribe(PreferencesUpdateEvent, self, 'on_preferences_update') def subscribe(self, event_type, event_listener, method='on_event'): """ Example: extension.subscribe(PreferencesEvent, PreferencesEventListener()) :param type event_type: :param ~ulauncher.api.client.EventListener.EventListener event_listener: :param str method: """ self._listeners[event_type].append((event_listener, method)) def get_listeners_for_event(self, event): """ :param ~ulauncher.api.shared.event.BaseEvent event: :rtype: ~ulauncher.api.client.EventListener.EventListener """ return self._listeners[type(event)] def trigger_event(self, event): """ :param ~ulauncher.api.shared.event.BaseEvent event: """ listeners = self.get_listeners_for_event(event) if not listeners: self.logger.debug('No listeners for event %s', type(event).__name__) return for listener, method in listeners: _method = getattr(listener, method) param_count = len(signature(_method).parameters) # method can and likely will be a member on self for new extensions, in which case # it can access self. But for backward compatibility we need to pass self action = _method(event) if param_count == 1 else _method( event, self) if action: assert isinstance( action, (list, BaseAction )), "on_event must return list of Results or a BaseAction" self._client.send(Response(event, action)) def run(self): """ Subscribes to events and connects to Ulauncher WS server """ self.subscribe(PreferencesEvent, PreferencesEventListener()) self.subscribe(PreferencesUpdateEvent, PreferencesUpdateEventListener()) self._client.connect() def on_query_change(self, event): pass def on_item_enter(self, event): pass def on_system_exit(self, event): pass def on_preferences(self, event): pass def on_preferences_update(self, event): pass