You are not logged in.

#1 2018-07-12 09:27:56

leethaxor
Member
Registered: 2018-07-09
Posts: 31

(SOLVED) Python check if computer just woke up from suspend

Is there a way to run a loop that checks for a signal from arch that says it just woke up? I've tried:

#!/usr/bin/python
# This is some example code on howto use dbus to detect when the system returns
# from hibernation or suspend.

import dbus      # for dbus communication (obviously)
import gobject   # main loop
from dbus.mainloop.glib import DBusGMainLoop  # integration into the main loop


def handle_resume_callback():
    print("System just resumed from hibernate or suspend")


DBusGMainLoop(set_as_default=True) # integrate into main loop
bus = dbus.SystemBus()             # connect to dbus system wide
bus.add_signal_receiver(           # defince the signal to listen to
    handle_resume_callback,            # name of callback function
    'Resuming',                        # singal name
    'org.freedesktop.UPower',          # interface
    'org.freedesktop.UPower'           # bus name
)

loop = gobject.MainLoop()         # define mainloop
loop.run()                         # run main loop

but I get this error saying MainLoop() is not a function of gobject. I am using python 3.6.6 .

Last edited by leethaxor (2018-07-15 08:50:21)

Offline

#2 2018-07-12 12:44:26

Steef435
Member
Registered: 2013-08-29
Posts: 577
Website

Re: (SOLVED) Python check if computer just woke up from suspend

You're on the right track, but UPower doesn't seem to emit these signals anymore on systemd systems, systemd emits its own signal using logind: see here and the documentation.

So you want to listen to the PrepareForSleep signal on the system bus emitted by org.freedesktop.login1 (which gets emitted with boolean parameter true on sleep, and false on wake, see docs above). I'm not really a D-Bus or Python person but it looks like your code is using the old D-Bus API by GLib which should be well deprecated by now.

There's not really a ton of Python examples for the new method (read: I found none) but I managed to get this running (I think, you'll have to test and tweak yourself) with the API reference and the serverfault answer linked above. Hope this helps!

#!/usr/bin/env python

from gi.repository import GLib
from gi.repository import Gio

def onPrepareForSleep(conn, sender, obj, interface, signal, parameters, data):
    if not parameters[0]: # parameters[0] is True just before sleep, False just after wake
        print("Just woke up!")

system_bus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)
system_bus.signal_subscribe('org.freedesktop.login1',
                            'org.freedesktop.login1.Manager',
                            'PrepareForSleep',
                            '/org/freedesktop/login1',
                            None,
                            Gio.DBusSignalFlags.NONE,
                            onPrepareForSleep,
                            None)

GLib.MainLoop().run()

Offline

#3 2018-07-12 17:46:00

leethaxor
Member
Registered: 2018-07-09
Posts: 31

Re: (SOLVED) Python check if computer just woke up from suspend

I've installed python-gobject and gi via AUR because I had no gi.repository, but now I have. What I don't got is GLib or Gio inside gi.repositry, so how do I install them? Via pip or AUR??

Offline

#4 2018-07-12 22:42:27

Steef435
Member
Registered: 2013-08-29
Posts: 577
Website

Re: (SOLVED) Python check if computer just woke up from suspend

One could say GLib (of which Gio is a part) are like base libraries for GTK+, though they can be used without it. They all use GObject internally. GObject Introspection (GI) is used to create language bindings for GObject-based libraries, to Python for example.

I think that the packages glib2 (providing GLib, Gio) and python-gobject (which provides gi.repository so you get the Python bindings) from the main repositories should be sufficient, definitely no need for the gi from the AUR.

(Seeing that you don't have GLib installed already: it's probably also possible to use Qt for the D-Bus part, but I've never used Qt myself)

Last edited by Steef435 (2018-07-12 22:43:33)

Offline

#5 2018-07-13 05:30:32

leethaxor
Member
Registered: 2018-07-09
Posts: 31

Re: (SOLVED) Python check if computer just woke up from suspend

I have glib2, python-gobject, python-dbus and gtk3 installed already but it still can't find GLib nor Gio from gi.repository. I am using Python3 if that makes a difference and if I hadn't made that clear! smile

Offline

#6 2018-07-13 05:49:18

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: (SOLVED) Python check if computer just woke up from suspend

The common gobject bindings, including for both GLib and DBus, are installed by the repository package gobject-introspection-runtime

python-gobject is a repository package, too -- due to the fact that large parts of an entire programming language, plus multiple desktop environments (have you ever heard of gnome? Right, they're the ones who invented gobject, and they use it major time) depend on it.

I have no idea how screwed up your installation might be if you installed some mysterious AUR package instead. It's entirely plausible that you're missing core requirements like the base runtime libraries.


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#7 2018-07-13 07:39:39

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: (SOLVED) Python check if computer just woke up from suspend

https://bbs.archlinux.org/viewtopic.php?id=57855

But maybe all you need is to understand that these are dynamic bindings/imports. (Follow Steef435's example and don't try something like dir(gi.repository).)

Offline

#8 2018-07-13 16:49:55

leethaxor
Member
Registered: 2018-07-09
Posts: 31

Re: (SOLVED) Python check if computer just woke up from suspend

The only library that can find GLib and Gio is gi.overrides:

from gi.overrides import GLib
from gi.overrides import Gio

But these imports give me errors

Traceback (most recent call last):
  File "/home/archuser/py.py", line 7, in <module>
    from gi.overrides import GLib
  File "/usr/lib/python3.6/site-packages/gi/overrides/GLib.py", line 501, in <module>
    MainLoop = override(MainLoop)
  File "/usr/lib/python3.6/site-packages/gi/overrides/__init__.py", line 207, in override
    module = sys.modules["gi.repository." + namespace]
KeyError: 'gi.repository.GLib'

Last edited by leethaxor (2018-07-13 20:00:19)

Offline

#9 2018-07-13 18:40:15

Steef435
Member
Registered: 2013-08-29
Posts: 577
Website

Re: (SOLVED) Python check if computer just woke up from suspend

What's the situation package-wise right now? What relevant packages have you installed, and from which sources? Did you install gobject-introspection-runtime as Eschwartz suggested?

Offline

#10 2018-07-13 20:00:08

leethaxor
Member
Registered: 2018-07-09
Posts: 31

Re: (SOLVED) Python check if computer just woke up from suspend

I've installed these

gobject-introspection-runtime 1.56.1-1
pygobject-devel 3.28.3-1
pygobject2-devel 2.28.7-1
python-gobject 3.28.3-1
python2-gobject2 2.28.7-1
gi 0.1.2-1
gtk2 2.24.32-1
gtk3 3.22.30-1
pygtk 2.24.0-8
glib2 2.56.1-1
dbus 1.12.8-1
dbus-glib 0.110-1
python-dbus 1.2.8-1
python-dbus-common 1.2.8-1

And I still can't import GLib or Gio from anything other than overrides but overrides give me the error I mentioned. I've installed most of the stuff through regular Pacman and some from AUR. gobject-introspection-runtime was already installed when I made the post.

Offline

#11 2018-07-13 20:06:12

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: (SOLVED) Python check if computer just woke up from suspend

According to the AUR page for "gi", it is "Git wrapper to perform Mercurial-like short unique abbreviation searching for commands".

If you've got something python/glib/gobject-related for that package, then what?


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#12 2018-07-13 22:50:12

Steef435
Member
Registered: 2013-08-29
Posts: 577
Website

Re: (SOLVED) Python check if computer just woke up from suspend

Hmm, you might need the package gobject-introspection itself as well (from the repositories), although I wouldn't think so normally (shouldn't that just be the scanner etc.?).

I'm not in the position to check right now but I would think that then you have everything you could need, way more than enough in fact. Make sure that you don't have any of the packages installed by PIP or whatever as well, that might lead to conflicts. Everything you need is definitely in the official repositories, so get rid of everything from the AUR , PIP, and other sources.

If that also doesn't work, maybe you'd have to search in the direction of gi-repository's internals, maybe there's some weird caching involved. But I can't imagine it to be a problem there, it Just Worked (TM) on my machine and I've never done Python development.

Tomorrow morning I'm leaving for a computer-free vacation (aka a scouting camp) so I'm afraid I won't be able to help you further any time soon. Hopefully the others remain vigilant smile

Offline

#13 2018-07-14 06:11:39

leethaxor
Member
Registered: 2018-07-09
Posts: 31

Re: (SOLVED) Python check if computer just woke up from suspend

reinstalled EVERY gobject package and removed all of them in pip and still doesn't work. gobject-introspection doesn't do anything for me either...

Offline

#14 2018-07-14 10:19:43

Morn
Member
Registered: 2012-09-02
Posts: 886

Re: (SOLVED) Python check if computer just woke up from suspend

How about a more low tech solution? Run a loop that checks the system time (time.time()) every 10 seconds and looks at the difference to the previously saved value. If the difference is larger than, say, 20 seconds, the system has obviously just come out of suspend or hibernation.

Offline

#15 2018-07-15 08:37:27

leethaxor
Member
Registered: 2018-07-09
Posts: 31

Re: (SOLVED) Python check if computer just woke up from suspend

from time import gmtime, strftime, sleep


def main():
    lastS = int(strftime("%S", gmtime()))
    lastM = int(strftime("%M", gmtime()))
    lastH = int(strftime("%H", gmtime()))
    while True:
        difS = lastS - int(strftime("%S", gmtime()))
        difM = lastM - int(strftime("%M", gmtime()))
        difH = lastH - int(strftime("%H", gmtime()))

        if difS < -1:
            print('suspended')
        if difM < 0 and difS != 59:
            print('susp')

        lastS = int(strftime("%S", gmtime()))
        lastM = int(strftime("%M", gmtime()))
        lastH = int(strftime("%H", gmtime()))
        sleep(1)

It's a quick fix and not a very neat one but this works to check if computer was suspended or not! Thanks for the idea Morn smile

Offline

#16 2018-07-16 00:55:34

bulletmark
Member
From: Brisbane, Australia
Registered: 2013-10-22
Posts: 649

Re: (SOLVED) Python check if computer just woke up from suspend

I'm sure Morn's idea was something much less convoluted and more efficient, like:

#!/usr/bin/python3
import time

WAITTIME = 1
BUMPTIME = WAITTIME + 10

last = time.time()
while True:
    time.sleep(WAITTIME)
    now = time.time()

    if (now - last) > BUMPTIME:
        print('We suspended')
    else:
        print('We did not suspend')

    last = now

Last edited by bulletmark (2018-07-16 00:57:33)

Offline

#17 2018-07-16 06:28:43

leethaxor
Member
Registered: 2018-07-09
Posts: 31

Re: (SOLVED) Python check if computer just woke up from suspend

bulletmarks solution worked perfectly! Thanks! smile

Offline

#18 2018-07-16 17:13:11

Morn
Member
Registered: 2012-09-02
Posts: 886

Re: (SOLVED) Python check if computer just woke up from suspend

@bulletmark: Yes, that's what I meant...

Offline

Board footer

Powered by FluxBB