wakepy icon indicating copy to clipboard operation
wakepy copied to clipboard

Add support for passing down arguments for different Methods

Open fohrloop opened this issue 10 months ago • 1 comments

Motivation

It would be nice if it would be possible to pass down keyword arguments for different Methods. Say, you have some method like this:

class FreedesktopScreenSaverInhibit(Method):
    ...

    def enter_mode(self) -> None:
        call = DBusMethodCall(
            method=self.method_inhibit,
            args=dict(
                application_name="wakepy",               # could give as an argument?
                reason_for_inhibit="wakelock active",    # could give as an argument?
            ),
        )

       # do something

There are probably many examples when a Method could work in various ways, and it would be helpful if that was somehow configurable by the user.

Implementation sketch

Usage could look something like this


mode = keep.running(
    on_fail = "warn",
    dbus_adapter=SomeAdapter,
    method_kwargs={
        "org.freedesktop.ScreenSaver":  dict(reason_for_inhibit='some reason')
    }

)

with mode:
    # do something

but since the dbus_adapter is something that is passed to the Method subclasses, the incoming kwargs could be utilized there. So instead of the current Method.__init__ method implementation:

def __init__(self, dbus_adapter: Optional[DBusAdapter] = None):
    self._dbus_adapter = dbus_adapter

it could look like this:

class Method:
    def __init__(self, **kwargs: object):
        self.dbus_adapter: DbusAdapter | None = kwargs.pop('dbus_adapter', None)
        self.method_kwargs = kwargs

common and method-specific kwargs

One way would be to use an additional common dictionary which will be passed to all the used Methods in addition to the method-specific kwargs:


mode = keep.running(
    on_fail = "warn",
    dbus_adapter=SomeAdapter,
    common_method_kwargs={
        'foo': 123,
        'bar': True,
    }
    method_kwargs={
        "org.freedesktop.ScreenSaver":  dict(reason_for_inhibit='some reason'),
    }

)

with mode:
    # do something

In this case, the Method(s) with name "org.freedesktop.ScreenSaver" would be set following method_kwargs:

dict(foo=123, bar=True, reason_for_inhibit='some reason')

and all other Methods:

dict(foo=123, bar=True)

fohrloop avatar Apr 15 '24 21:04 fohrloop