Попался мне исходник этой программы EasyShutdown. Если это кому-нибудь интересно.
Напишу пару слов в эту тему, чтобы, как говорится, расставить все точки над i.
#!/usr/bin/env python
#####################################################################################
#Easy Shutdown - shuts the computer down after a specific time
#
#Copyright (C) 2010 Meister0815
#
#This program is free software; you can redistribute it and/or modify it under
#the terms of the GNU General Public License as published by the Free Software
#Foundation; either version 3 of the License, or (at your option) any later version.
#
#This program is distributed in the hope that it will be useful, but WITHOUT
#ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License along with
#this program; if not, see http://www.gnu.org/licenses/.
#####################################################################################
# Parse Command line options
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-u", "--unity_integration", dest="unity_integration", type="int",
help="Force to turn unity-integration on=1 or off=0.")
parser.add_option("-l", "--language", dest="language", type="string",
help="Force to use language en=English, de=German, fr=France,...")
parser.add_option("-s", "--start_countdown", dest="start_countdown", type="int",
help="Start countdown on programm start. Enter time in minutes")
(options, args) = parser.parse_args()
# Check if unity is running
import commands
output = commands.getoutput('pidof unity-panel-service')
if output.isdigit():
unity_running = True
else:
unity_running = False
# Commandline options do override the previous check
if options.unity_integration == 0:
unity_running = False
if options.unity_integration == 1:
unity_running = 1
# For unity interactions, only load when unity running
if unity_running == True:
try:
from gi.repository import Unity, Gio, GObject
except ImportError:
# Exception overrides the unity-check and the Commandline options
print "Error: Can't use Unity"
unity_running = False
# for main program
import pygtk
pygtk.require('2.0')
import gtk
import gobject
import os
# For Notifications
import sys
import pynotify
# For Localization
import gettext
import locale
############# Change before packaging #################
path_to_locale = "/usr/share/locale"
path_to_icon = "/usr/share/pixmaps/easyshutdown.png"
#path_to_locale = "locale"
#path_to_icon = "/home/holger/Programme/workspace/easyshutdown/src/easyshutdown.png"
program_title = "EasyShutdown"
program_version= "0.6"
#######################################################
loc = locale.getdefaultlocale()[0][0:2]
if options.language:
loc = options.language
trans = gettext.translation("easyshutdown", path_to_locale, [loc], fallback=True)
trans.install()
capabilities = {'actions': False,
'body': False,
'body-hyperlinks': False,
'body-images': False,
'body-markup': False,
'icon-multi': False,
'icon-static': False,
'sound': False,
'image/svg+xml': False,
'private-synchronous': False,
'append': False,
'private-icon-only': False}
running = bool(0)
hours = 0
minutes = 0
seconds = 0
startseconds = 0
class EasyShutdown:
def initCaps (self):
caps = pynotify.get_server_caps ()
if caps is None:
print _("Failed to receive server caps.")
sys.exit (1)
for cap in caps:
capabilities[cap] = True
def shutdown(self):
print _("Shutting down...")
os.system("dbus-send --system --print-reply --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Stop")
def on_timer(self):
global seconds
global minutes
global hours
global startseconds
if unity_running == True:
launcher = Unity.LauncherEntry.get_for_desktop_id ("easyshutdown.desktop")
if startseconds == 0.0:
startseconds = (hours * 60 * 60) + (minutes * 60) + seconds
if running == False: # Stop timer and clear launcher
startseconds = 0.0;
if unity_running == True:
launcher.set_property("count_visible", False)
launcher.set_property("progress_visible", False)
return False
else: # Let timer run and set launcher
go_on = True
if seconds > 0:
seconds = seconds - 1
else:
seconds = 59
if minutes > 0:
minutes = minutes - 1
else:
minutes = 59
if hours > 0:
hours = hours - 1
if (hours == 0) and (minutes == 0) and (seconds == 0):
self.shutdown()
go_on = False
spinner1.set_value(hours)
spinner2.set_value(minutes)
spinner3.set_value(seconds)
if unity_running == True:
if hours == 0:
launcher.set_property("count", minutes)
launcher.set_property("count_visible", True)
else:
launcher.set_property("count_visible", False)
if (hours == 0) and (minutes == 0) and (seconds == 59):
if not pynotify.init ("icon-summary-body"):
sys.exit (1)
# call this so we can savely use capabilities dictionary later
self.initCaps ()
# try the icon-summary-body case
n = pynotify.Notification (program_title,
_("Attention, your computer is shutting down in 1 minute!"),
path_to_icon)
n.show ()
if unity_running == True:
launcher.set_property("urgent", True)
else:
if unity_running == True:
launcher.set_property("urgent", False)
remainingseconds = (hours * 60 * 60) + (minutes * 60) + seconds
if unity_running == True:
launcher.set_property("progress", ((remainingseconds + 0.0) / startseconds))
launcher.set_property("progress_visible", True)
return go_on
# if Start/Stop-Button is pressed:
def startstop(self, widget, spin1, spin2, spin3, startbt):
global running
if running == bool(0):
# start countdown
running = bool(1)
spin1.set_sensitive(False)
spin2.set_sensitive(False)
spin3.set_sensitive(False)
global hours
hours = spin1.get_value_as_int()
global minutes
minutes = spin2.get_value_as_int()
global seconds
seconds = spin3.get_value_as_int()
startbt.remove(start_image)
startbt.add(stop_image)
# create timer
gobject.timeout_add(1000, self.on_timer)
else:
# stop countdown
running = bool(0)
spin1.set_sensitive(True)
spin2.set_sensitive(True)
spin3.set_sensitive(True)
startbt.remove(stop_image)
startbt.add(start_image)
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.connect("destroy", lambda w: gtk.main_quit())
window.set_title(program_title + " " + program_version)
window.set_icon_from_file(path_to_icon)
# Create Main HBox
main_hbox = gtk.HBox(False, 5)
main_hbox.set_border_width(3)
window.add(main_hbox)
# Day, month, year spinners
hbox = gtk.HBox(False, 0)
main_hbox.pack_start(hbox, True, True, 5)
# Hours SpinButton
adj = gtk.Adjustment(0, 0, 99, 1, 5, 0)
global spinner1
spinner1 = gtk.SpinButton(adj, 1, 0)
spinner1.set_wrap(False)
spinner1.set_has_tooltip(True)
spinner1.set_tooltip_text(_('Hours'))
main_hbox.pack_start(spinner1, False, True, 0)
# Minutes SpinButton
adj = gtk.Adjustment(30, 0, 59, 1, 5, 0)
global spinner2
spinner2 = gtk.SpinButton(adj, 0, 0)
spinner2.set_wrap(False)
spinner2.set_has_tooltip(True)
spinner2.set_tooltip_text(_('Minutes'))
main_hbox.pack_start(spinner2, False, True, 0)
# Seconds SpinButton
adj = gtk.Adjustment(0, 0, 59, 1, 5, 0)
global spinner3
spinner3 = gtk.SpinButton(adj, 0, 0)
spinner3.set_wrap(False)
spinner3.set_has_tooltip(True)
spinner3.set_tooltip_text(_('Seconds'))
main_hbox.pack_start(spinner3, False, True, 0)
# Load StartStop Images from Stock
global start_image
start_image = gtk.Image()
start_image.set_from_stock('gtk-media-play', gtk.ICON_SIZE_BUTTON)
start_image.show()
global stop_image
stop_image = gtk.Image()
stop_image.set_from_stock('gtk-media-stop', gtk.ICON_SIZE_BUTTON)
stop_image.show()
# a button to contain the startstop-image widget
button = gtk.Button()
button.add(start_image)
button.show()
button.connect("clicked", self.startstop, spinner1, spinner2, spinner3, button)
main_hbox.pack_start(button, True, True, 5)
# Show all
window.show_all()
# On commandline -s start countdown immediately
if options.start_countdown > 0:
spinner1.set_value( options.start_countdown // 60 )
spinner2.set_value( options.start_countdown % 60 )
spinner3.set_value( 0 )
self.startstop(self, spinner1, spinner2, spinner3, button)
def main():
gtk.main()
return 0
if __name__ == "__main__":
EasyShutdown()
main()
ВЕСЬ он нам, я думаю, не особо нужен. НО из него можно выяснить,
тот, что эта программа действительно выключает компьютер с помощью следующей команды.