Last active
June 19, 2017 15:47
-
-
Save SergKolo/6530ff89867e899f2b8ba728e28ad45f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Author: Serg Kolo <[email protected]> | |
Date: Nov 29 , 2016 | |
Purpose:Script for shutting down Ubuntu system | |
if AC adapter is removed | |
Written for:http://askubuntu.com/q/844193/295286 | |
""" | |
import dbus | |
import time | |
import subprocess | |
def get_dbus_property(bus_type, obj, path, iface, prop): | |
""" utility:reads properties defined on specific dbus interface""" | |
if bus_type == "session": | |
bus = dbus.SessionBus() | |
if bus_type == "system": | |
bus = dbus.SystemBus() | |
proxy = bus.get_object(obj, path) | |
aux = 'org.freedesktop.DBus.Properties' | |
props_iface = dbus.Interface(proxy, aux) | |
props = props_iface.Get(iface, prop) | |
return props | |
def get_dbus_method(bus_type, obj, path, interface, method, arg): | |
""" utility: executes dbus method on specific interface""" | |
if bus_type == "session": | |
bus = dbus.SessionBus() | |
if bus_type == "system": | |
bus = dbus.SystemBus() | |
proxy = bus.get_object(obj, path) | |
method = proxy.get_dbus_method(method, interface) | |
if arg: | |
return method(arg) | |
else: | |
return method() | |
def on_ac_power(): | |
adapter = get_adapter_path() | |
call = ['system','org.freedesktop.UPower',adapter, | |
'org.freedesktop.UPower.Device','Online' | |
] | |
if get_dbus_property(*call): return True | |
def get_adapter_path(): | |
""" Finds dbus path of the ac adapter device """ | |
call = ['system', 'org.freedesktop.UPower', | |
'/org/freedesktop/UPower','org.freedesktop.UPower', | |
'EnumerateDevices',None | |
] | |
devices = get_dbus_method(*call) | |
for dev in devices: | |
call = ['system','org.freedesktop.UPower',dev, | |
'org.freedesktop.UPower.Device','Type' | |
] | |
if get_dbus_property(*call) == 1: | |
return dev | |
def shutdown_system(): | |
call = ['session', 'com.canonical.Unity', | |
'/com/canonical/Unity/Session', 'com.canonical.Unity.Session', | |
'Shutdown',None | |
] | |
return get_dbus_method(*call) | |
def main(): | |
while not on_ac_power(): | |
time.sleep(1) | |
while on_ac_power(): | |
time.sleep(1) | |
try: | |
shutdown_system() | |
except Exception as e: | |
error_msg = 'Ooops,' + __file__ + 'failed to shutdown your system.' | |
error_msg = error_msg + 'Please show Serg this error so he can fix it:' | |
subprocess.call(['zenity','--error', | |
'--text', error_msg + repr(e) | |
]) | |
if __name__ == "__main__": main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Serg, I have a quick question for you. Would it be possible to modify this file so that it exits a Minecraft server (basically just type "stop" into the console and hit enter) before shutting down the computer?