示例#1
0
 def toggle(self):
     if self.activated:
         execute("xautolock -disable")
     else:
         execute("xautolock -enable")
     self.activated = not self.activated
     self.update_output()
示例#2
0
    def __button_callback_handler(self, button, cb):
        if not cb:
            self.__log_button_event(button, None, None,
                                    "No callback attached")
            return False

        if isinstance(cb, list):
            cb, args = (cb[0], cb[1:])
        else:
            args = []

        if callable(cb):
            self.__log_button_event(button, cb, args, "Python callback")
            cb(self, *args)
        elif hasattr(self, cb):
            if cb is not "run":
                self.__log_button_event(button, cb, args, "Member callback")
                getattr(self, cb)(*args)
        else:
            self.__log_event(button, cb, args, "External command")
            execute(cb, detach=True)

        # Notify status handler
        try:
            self.__status_handler.io.async_refresh()
        except:
            pass
示例#3
0
    def run(self):
        if self.state is not TimerState.stopped:
            diff = self.compare - time.time()

            if diff < 0 and self.state is TimerState.running:
                self.state = TimerState.overflow
                if self.on_overflow:
                    if callable(self.on_overflow):
                        self.on_overflow()
                    else:
                        execute(self.on_overflow)

            fmt = self.format
            color = self.color
            for rule in self.format_custom:
                if diff < rule[0]:
                    fmt = rule[1]
                    color = rule[2]
                    break
            urgent = self.overflow_urgent and self.state is TimerState.overflow

            self.output = {
                "full_text": format(TimeWrapper(abs(diff), fmt)),
                "color": color,
                "urgent": urgent,
            }
        else:
            self.output = {
                "full_text": self.format_stopped,
                "color": self.color_stopped,
            }
示例#4
0
 def switch_mute(self):
     if self.has_amixer:
         command = "amixer -q -D pulse sset Master "
         if self.currently_muted:
             command += 'unmute'
         else:
             command += 'mute'
         execute(command)
示例#5
0
 def reset(self):
     """
     Stop timer and execute ``on_reset`` if overflow occured.
     """
     if self.state is not TimerState.stopped:
         if self.on_reset and self.state is TimerState.overflow:
             if callable(self.on_reset):
                 self.on_reset()
             else:
                 execute(self.on_reset)
         self.state = TimerState.stopped
示例#6
0
    def __button_callback_handler(self, button, cb):
        if not cb:
            self.__log_button_event(button, None, None,
                                    "No callback attached")
            return False

        if isinstance(cb, list):
            cb, args = (cb[0], cb[1:])
        else:
            args = []

        try:
            our_method = is_method_of(cb, self)
            if callable(cb) and not our_method:
                self.__log_button_event(button, cb, args, "Python callback")
                cb(*args)
            elif our_method:
                cb(self, *args)
            elif hasattr(self, cb):
                if cb is not "run":
                    # CommandEndpoint already calls run() after every
                    # callback to instantly update any changed state due
                    # to the callback's actions.
                    self.__log_button_event(button, cb, args, "Member callback")
                    getattr(self, cb)(*args)
            else:
                self.__log_button_event(button, cb, args, "External command")
                if hasattr(self, "data"):
                    args = [arg.format(**self.data) for arg in args]
                    cb = cb.format(**self.data)
                execute(cb + " " + " ".join(args), detach=True)
        except Exception as e:
            self.logger.critical("Exception while processing button callback: {!r}".format(e))

        # Notify status handler
        try:
            self.__status_handler.io.async_refresh()
        except:
            pass
示例#7
0
    def on_click(self, button):
        """
        Maps a click event with its associated callback.

        Currently implemented events are:

        ===========  ================  =========
        Event        Callback setting  Button ID
        ===========  ================  =========
        Left click   on_leftclick      1
        Right click  on_rightclick     3
        Scroll up    on_upscroll       4
        Scroll down  on_downscroll     5
        ===========  ================  =========

        The action is determined by the nature (type and value) of the callback
        setting in the following order:

        1. If null callback (``None``), no action is taken.
        2. If it's a `python function`, call it and pass any additional
           arguments.
        3. If it's name of a `member method` of current module (string), call it
           and pass any additional arguments.
        4. If the name does not match with `member method` name execute program
           with such name.

        .. seealso:: :ref:`callbacks` for more information about
         callback settings and examples.

        :param button: The ID of button event received from i3bar.
        :type button: int
        :return: Returns ``True`` if a valid callback action was executed.
         ``False`` otherwise.
        :rtype: bool

        """

        def log_event(name, button, cb, args, action):
            msg = "{}: button={}, cb='{}', args={}, type='{}'".format(
                  name, button, cb, args, action)
            self.logger.debug(msg)

        def split_callback_and_args(cb):
            if isinstance(cb, list):
                return cb[0], cb[1:]
            else:
                return cb, []

        cb = None
        if button == 1:  # Left mouse button
            cb = self.on_leftclick
        elif button == 3:  # Right mouse button
            cb = self.on_rightclick
        elif button == 4:  # mouse wheel up
            cb = self.on_upscroll
        elif button == 5:  # mouse wheel down
            cb = self.on_downscroll
        else:
            log_event(self.__name__, button, None, None, "Unhandled button")
            return False

        if not cb:
            log_event(self.__name__, button, None, None, "No callback attached")
            return False
        else:
            cb, args = split_callback_and_args(cb)

        if callable(cb):
            log_event(self.__name__, button, cb, args, "Python callback")
            cb(self, *args)
        elif hasattr(self, cb):
            if cb is not "run":
                log_event(self.__name__, button, cb, args, "Member callback")
                getattr(self, cb)(*args)
        else:
            log_event(self.__name__, button, cb, args, "External command")
            execute(cb, detach=True)
        return True
示例#8
0
    def __button_callback_handler(self, button, cb, **kwargs):

        def call_callback(cb, *args, **kwargs):
            # Recover the function if wrapped (with get_module for example)
            wrapped_cb = getattr(cb, "__wrapped__", None)
            if wrapped_cb:
                locals()["self"] = self  # Add self to the local stack frame
                tmp_cb = wrapped_cb
            else:
                tmp_cb = cb

            try:
                args_spec = inspect.getargspec(tmp_cb)
            except Exception:
                args_spec = inspect.ArgSpec([], None, None, None)

            # Remove all variables present in kwargs that are not used in the
            # callback, except if there is a keyword argument.
            if not args_spec.keywords:
                kwargs = {k: v for k, v in kwargs.items()
                          if k in args_spec.args}
            cb(*args, **kwargs)

        if not cb:
            self.__log_button_event(button, None, None,
                                    "No callback attached", **kwargs)
            return False

        if isinstance(cb, list):
            cb, args = (cb[0], cb[1:])
        else:
            args = []

        try:
            our_method = is_method_of(cb, self)
            if callable(cb) and not our_method:
                self.__log_button_event(button, cb, args,
                                        "Python callback", **kwargs)
                call_callback(cb, *args, **kwargs)
            elif our_method:
                self.__log_button_event(button, cb, args,
                                        "Method callback", **kwargs)
                call_callback(cb, self, *args, **kwargs)
            elif hasattr(self, cb):
                if cb is not "run":
                    # CommandEndpoint already calls run() after every
                    # callback to instantly update any changed state due
                    # to the callback's actions.
                    self.__log_button_event(button, cb, args,
                                            "Member callback", **kwargs)
                    call_callback(getattr(self, cb), *args, **kwargs)
            else:
                self.__log_button_event(button, cb, args,
                                        "External command", **kwargs)

                if hasattr(self, "data"):
                    kwargs.update(self.data)

                args = [str(arg).format(**kwargs) for arg in args]
                cb = cb.format(**kwargs)
                execute(cb + " " + " ".join(args), detach=True)
        except Exception as e:
            self.logger.critical("Exception while processing button "
                                 "callback: {!r}".format(e))
            self.logger.critical(traceback.format_exc())

        # Notify status handler
        try:
            self.__status_handler.io.async_refresh()
        except:
            pass
示例#9
0
    def __button_callback_handler(self, button, cb, **kwargs):

        def call_callback(cb, *args, **kwargs):
            # Recover the function if wrapped (with get_module for example)
            wrapped_cb = getattr(cb, "__wrapped__", None)
            if wrapped_cb:
                locals()["self"] = self  # Add self to the local stack frame
                tmp_cb = wrapped_cb
            else:
                tmp_cb = cb

            try:
                args_spec = inspect.getargspec(tmp_cb)
            except Exception:
                args_spec = inspect.ArgSpec([], None, None, None)

            # Remove all variables present in kwargs that are not used in the
            # callback, except if there is a keyword argument.
            if not args_spec.keywords:
                kwargs = {k: v for k, v in kwargs.items()
                          if k in args_spec.args}
            cb(*args, **kwargs)

        if not cb:
            self.__log_button_event(button, None, None,
                                    "No callback attached", **kwargs)
            return False

        if isinstance(cb, list):
            cb, args = (cb[0], cb[1:])
        else:
            args = []

        try:
            our_method = is_method_of(cb, self)
            if callable(cb) and not our_method:
                self.__log_button_event(button, cb, args,
                                        "Python callback", **kwargs)
                call_callback(cb, *args, **kwargs)
            elif our_method:
                self.__log_button_event(button, cb, args,
                                        "Method callback", **kwargs)
                call_callback(cb, self, *args, **kwargs)
            elif hasattr(self, cb):
                if cb is not "run":
                    # CommandEndpoint already calls run() after every
                    # callback to instantly update any changed state due
                    # to the callback's actions.
                    self.__log_button_event(button, cb, args,
                                            "Member callback", **kwargs)
                    call_callback(getattr(self, cb), *args, **kwargs)
            else:
                self.__log_button_event(button, cb, args,
                                        "External command", **kwargs)

                if hasattr(self, "data"):
                    kwargs.update(self.data)

                args = [str(arg).format(**kwargs) for arg in args]
                cb = cb.format(**kwargs)
                execute(cb + " " + " ".join(args), detach=True)
        except Exception as e:
            self.logger.critical("Exception while processing button "
                                 "callback: {!r}".format(e))
            self.logger.critical(traceback.format_exc())

        # Notify status handler
        try:
            self.__status_handler.io.async_refresh()
        except:
            pass
示例#10
0
 def decrease_volume(self):
     if self.has_amixer:
         command = "amixer -q -D pulse sset Master %s%%-" % self.step
         execute(command)
示例#11
0
    def on_click(self, button):
        """
        Maps a click event with its associated callback.

        Currently implemented events are:

        ===========  ================  =========
        Event        Callback setting  Button ID
        ===========  ================  =========
        Left click   on_leftclick      1
        Right click  on_rightclick     3
        Scroll up    on_upscroll       4
        Scroll down  on_downscroll     5
        ===========  ================  =========

        The action is determined by the nature (type and value) of the callback
        setting in the following order:

        1. If null callback (``None``), no action is taken.
        2. If it's a `python function`, call it and pass any additional
           arguments.
        3. If it's name of a `member method` of current module (string), call it
           and pass any additional arguments.
        4. If the name does not match with `member method` name execute program
           with such name.

        .. seealso:: :ref:`callbacks` for more information about
         callback settings and examples.

        :param button: The ID of button event received from i3bar.
        :type button: int
        :return: Returns ``True`` if a valid callback action was executed.
         ``False`` otherwise.
        :rtype: bool

        """
        def log_event(name, button, cb, args, action):
            msg = "{}: button={}, cb='{}', args={}, type='{}'".format(
                name, button, cb, args, action)
            self.logger.debug(msg)

        def split_callback_and_args(cb):
            if isinstance(cb, list):
                return cb[0], cb[1:]
            else:
                return cb, []

        cb = None
        if button == 1:  # Left mouse button
            cb = self.on_leftclick
        elif button == 3:  # Right mouse button
            cb = self.on_rightclick
        elif button == 4:  # mouse wheel up
            cb = self.on_upscroll
        elif button == 5:  # mouse wheel down
            cb = self.on_downscroll
        else:
            log_event(self.__name__, button, None, None, "Unhandled button")
            return False

        if not cb:
            log_event(self.__name__, button, None, None,
                      "No callback attached")
            return False
        else:
            cb, args = split_callback_and_args(cb)

        if callable(cb):
            log_event(self.__name__, button, cb, args, "Python callback")
            cb(self, *args)
        elif hasattr(self, cb):
            if cb is not "run":
                log_event(self.__name__, button, cb, args, "Member callback")
                getattr(self, cb)(*args)
        else:
            log_event(self.__name__, button, cb, args, "External command")
            execute(cb, detach=True)
        return True