में BackInTime मैं सभी प्रमुख des पर काम करने के लिए जोड़े को अलग DBus तरीकों का उपयोग कर रहा हूँ। केवल नकारात्मक पक्ष यह है इस के लिए काम नहीं करेगा है root
क्योंकि root
कोई नहीं है dbus.SessionBus
।
#!/usr/bin/env python3
import sys
import dbus
from time import sleep
INHIBIT_LOGGING_OUT = 1
INHIBIT_USER_SWITCHING = 2
INHIBIT_SUSPENDING = 4
INHIBIT_IDLE = 8
INHIBIT_DBUS = (
{'service': 'org.gnome.SessionManager',
'objectPath': '/org/gnome/SessionManager',
'methodSet': 'Inhibit',
'methodUnSet': 'Uninhibit',
'interface': 'org.gnome.SessionManager',
'arguments': (0, 1, 2, 3)
},
{'service': 'org.mate.SessionManager',
'objectPath': '/org/mate/SessionManager',
'methodSet': 'Inhibit',
'methodUnSet': 'Uninhibit',
'interface': 'org.mate.SessionManager',
'arguments': (0, 1, 2, 3)
},
{'service': 'org.freedesktop.PowerManagement',
'objectPath': '/org/freedesktop/PowerManagement/Inhibit',
'methodSet': 'Inhibit',
'methodUnSet': 'UnInhibit',
'interface': 'org.freedesktop.PowerManagement.Inhibit',
'arguments': (0, 2)
})
def inhibitSuspend(app_id = sys.argv[0],
toplevel_xid = None,
reason = 'take snapshot',
flags = INHIBIT_SUSPENDING | INHIBIT_IDLE):
"""
Prevent machine to go to suspend or hibernate.
Returns the inhibit cookie which is used to end the inhibitor.
"""
if not app_id:
app_id = 'backintime'
if not toplevel_xid:
toplevel_xid = 0
for dbus_props in INHIBIT_DBUS:
try:
bus = dbus.SessionBus()
interface = bus.get_object(dbus_props['service'], dbus_props['objectPath'])
proxy = interface.get_dbus_method(dbus_props['methodSet'], dbus_props['interface'])
cookie = proxy(*[(app_id, dbus.UInt32(toplevel_xid), reason, dbus.UInt32(flags))[i] for i in dbus_props['arguments']])
print('Inhibit Suspend started. Reason: %s' % reason)
return (cookie, bus, dbus_props)
except dbus.exceptions.DBusException:
pass
print('Inhibit Suspend failed.')
def unInhibitSuspend(cookie, bus, dbus_props):
"""
Release inhibit.
"""
assert isinstance(cookie, int), 'cookie is not int type: %s' % cookie
assert isinstance(bus, dbus.bus.BusConnection), 'bus is not dbus.bus.BusConnection type: %s' % bus
assert isinstance(dbus_props, dict), 'dbus_props is not dict type: %s' % dbus_props
try:
interface = bus.get_object(dbus_props['service'], dbus_props['objectPath'])
proxy = interface.get_dbus_method(dbus_props['methodUnSet'], dbus_props['interface'])
proxy(cookie)
print('Release inhibit Suspend')
return None
except dbus.exceptions.DBusException:
print('Release inhibit Suspend failed.')
return (cookie, bus, dbus_props)
if __name__ == '__main__':
cookie, bus, dbus_props = inhibitSuspend()
print('do something here')
sleep(10)
unInhibitSuspend(cookie, bus, dbus_props)
sudo
उपयोगकर्ता द्वारा किए जाने पर ब्लॉक करना संभव नहीं है । मैं GUI के लिए पिछले उत्तर को लिंक करूंगा। मुझे पता है कि क्या आप चाहते हैं कि यह आपकी आवश्यकताओं के अनुरूप हो