From 03a36673c7796a2df8c0752e1ae68dcf6d4d0ffd Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Tue, 17 Mar 2009 21:11:39 +0100 Subject: moved python scripts --- openbox-shutdown.conf | 41 ++++++++++ openbox-shutdown.py | 190 +++++++++++++++++++++++++++++++++++++++++++ python/openbox-shutdown.conf | 41 ---------- python/openbox-shutdown.py | 190 ------------------------------------------- python/shutdown.py | 97 ---------------------- shutdown.py | 97 ++++++++++++++++++++++ 6 files changed, 328 insertions(+), 328 deletions(-) create mode 100644 openbox-shutdown.conf create mode 100755 openbox-shutdown.py delete mode 100644 python/openbox-shutdown.conf delete mode 100755 python/openbox-shutdown.py delete mode 100755 python/shutdown.py create mode 100755 shutdown.py diff --git a/openbox-shutdown.conf b/openbox-shutdown.conf new file mode 100644 index 0000000..eb4350c --- /dev/null +++ b/openbox-shutdown.conf @@ -0,0 +1,41 @@ +# configuration for openbox-shutdown +# +# buttons are shown in the order they are listed in this file +# +# each section contains the following values: +# command - command to execute when button is pressed +# can be used several times to execute more commands +# commands are executed in the order they are listed in this +# file +# image - location of the image for the button +# absolute path is recommended +# set to None if no image should be shown +# info_text - small text to show in the footer +# set to None if not requested +# label - label for the button +# uses mnemics +# set to None to disable + +[reboot] +#command = openbox --exit +command = sudo shutdown -r now +image = /usr/share/icons/hydroxygen/72x72/apps/gnome-session-reboot.png +info_text = Reboot +#label = _Neu starten +label = None + +[shutdown] +#command = openbox --exit +command = sudo shutdown -h now +image = /usr/share/icons/hydroxygen/72x72/apps/gnome-session-halt.png +info_text = Halt +#label = _Herunterfahren +label = None + +[suspend] +command = screen-locker.sh +command = sudo pm-suspend +image = /usr/share/icons/hydroxygen/72x72/apps/gnome-session-suspend.png +info_text = Suspend2Ram +#label = _Suspend +label = None diff --git a/openbox-shutdown.py b/openbox-shutdown.py new file mode 100755 index 0000000..cfdd67b --- /dev/null +++ b/openbox-shutdown.py @@ -0,0 +1,190 @@ +#!/usr/bin/env python +""" + +shutdown script for Openbox with configurable buttons and actions +needs pygtk for gui +""" + +# standard stuff +import os +import re +import sys + +# gui modules +try: + import gtk + import pygtk +except ImportError: + sys.stderr.write('pygtk is needed for using this script') + sys.exit(1) + +if sys.version_info < (2, 5, 0): + sys.stderr.write('Python 2.5.0 or higher is needed') + sys.exit(1) + +# a simple parser for the configuration +class ConfigParser(dict): + """ + ConfigParser(file) -> dict + + reads file and creates dict object containing corresponding + configuration + """ + + def __init__(self, filename): + self._section_match_regexp = re.compile(r'\[(\w+)\]') + self._key_val_match_regexp = re.compile(r'\s*=\s*') + self._comment_remov_regexp = re.compile(r'#.*') + try: + config_handle = open(filename, 'r') + except IOError: + sys.stderr.write('Could not read %s. Aborting ...\n' % filename) + sys.exit(1) + linecount = 0 + for line in config_handle.readlines(): + linecount += 1 + line = self._comment_remov_regexp.sub('', line.strip()) + if len(line) == 0: + continue + if self._section_match_regexp.match(line): + section = self._section_match_regexp.match(line).groups()[0] + self[section] = {} + continue + try: + (key, value) = self._key_val_match_regexp.split(line) + except ValueError: + sys.stderr.write('Syntax error in line %d of %s.' % (linecount, + filename)) + sys.exit(1) + if key == 'command' and not key in self[section]: + self[section][key] = [value] + continue + if key in self[section] and key == 'command': + self[section][key].append(value) + else: + self[section][key] = value + + def check(self): + """ + checks if configuration is complete + """ + for section in self: + for needed_key in ['command', 'image', 'info_text', 'label']: + if not needed_key in self[section]: + sys.stderr.write('Missing option %s in %s.' % (needed_key, section)) + sys.exit(1) + +# the main class for this script +class OpenBoxShutdown(object): + """ + main class for this script + + reads configuration and displays buttons as requested + + on click, the specified actions are made + """ + + def __init__(self, basedir): + self._config = ConfigParser(os.path.join(basedir, 'openbox-shutdown.conf')) + self._config.check() + # button box on top for the action buttons + button_box = gtk.HButtonBox() + for key in self._config: + button_box.pack_start(self._create_button(key, + self._config[key]['label'], + self._config[key]['info_text'], + self._config[key]['image'])) + button_box.show() + # create cancel button + cancel_button = gtk.Button(stock = gtk.STOCK_CANCEL) + cancel_button.connect('clicked', self._click_callback, 'cancel') + cancel_button.show() + # label widget for small hints + self._label = gtk.Label() + self._label.show() + # frame widget to contain label + frame = gtk.Frame() + frame.set_shadow_type(gtk.SHADOW_NONE) + frame.add(self._label) + frame.show() + # area containing hints and cancel button + action_area = gtk.HBox() + action_area.pack_end(cancel_button, expand=False) + action_area.pack_end(frame) + action_area.show() + # box containing button box and action area + vbox = gtk.VBox() + vbox.pack_start(button_box) + vbox.pack_end(action_area) + vbox.show() + # the main window with all widgets + # always on top and on all desktops + window = gtk.Window() + window.set_position('center') + window.set_decorated(False) + window.stick() + window.set_keep_above(True) + window.set_skip_pager_hint(True) + window.set_skip_taskbar_hint(True) + window.add(vbox) + window.show() + # give control to gtk + gtk.main() + + def _click_callback(self, event, action): + """ + _click_callback(event, action) + + executes commands on button click + """ + gtk.main_quit() + if action == 'cancel': + sys.exit(0) + for command in self._config[action]['command']: + os.system(command) + + def _create_button(self, key, label_text, info_text, image_file): + """ + _create_button(key, label, info, image) -> button + + creates a button widget with key (used for handling actions), + a label for the button, a small text as hint and an image + """ + button = gtk.Button() + if label_text != 'None': + button.set_label(label_text) + if image_file != 'None': + button.set_image(self._create_image(image_file)) + button.set_image_position(gtk.POS_TOP) + button.connect('clicked', self._click_callback, key) + button.connect('leave', lambda event: self._label.set_text('')) + button.connect('enter', self._set_label, info_text) + button.show() + return button + + def _create_image(self, image_file): + """ + _create_image(image) -> image + + creates an image widget from the specified file + """ + image = gtk.Image() + image.set_from_file(image_file) + return image + + def _set_label(self, event, info_text): + """ + _set_label(event, info_text) + + set label on entering the widget + """ + if info_text != 'None': + self._label.set_markup('%s' % info_text) + +# start the script if called standalone +if __name__ == '__main__': + # get the real path for the configuration file + conf_path = os.path.realpath(os.path.dirname(os.readlink(__file__))) \ + if os.path.islink(__file__) \ + else os.path.realpath(os.path.dirname(__file__)) + shutdown = OpenBoxShutdown(conf_path) diff --git a/python/openbox-shutdown.conf b/python/openbox-shutdown.conf deleted file mode 100644 index eb4350c..0000000 --- a/python/openbox-shutdown.conf +++ /dev/null @@ -1,41 +0,0 @@ -# configuration for openbox-shutdown -# -# buttons are shown in the order they are listed in this file -# -# each section contains the following values: -# command - command to execute when button is pressed -# can be used several times to execute more commands -# commands are executed in the order they are listed in this -# file -# image - location of the image for the button -# absolute path is recommended -# set to None if no image should be shown -# info_text - small text to show in the footer -# set to None if not requested -# label - label for the button -# uses mnemics -# set to None to disable - -[reboot] -#command = openbox --exit -command = sudo shutdown -r now -image = /usr/share/icons/hydroxygen/72x72/apps/gnome-session-reboot.png -info_text = Reboot -#label = _Neu starten -label = None - -[shutdown] -#command = openbox --exit -command = sudo shutdown -h now -image = /usr/share/icons/hydroxygen/72x72/apps/gnome-session-halt.png -info_text = Halt -#label = _Herunterfahren -label = None - -[suspend] -command = screen-locker.sh -command = sudo pm-suspend -image = /usr/share/icons/hydroxygen/72x72/apps/gnome-session-suspend.png -info_text = Suspend2Ram -#label = _Suspend -label = None diff --git a/python/openbox-shutdown.py b/python/openbox-shutdown.py deleted file mode 100755 index cfdd67b..0000000 --- a/python/openbox-shutdown.py +++ /dev/null @@ -1,190 +0,0 @@ -#!/usr/bin/env python -""" - -shutdown script for Openbox with configurable buttons and actions -needs pygtk for gui -""" - -# standard stuff -import os -import re -import sys - -# gui modules -try: - import gtk - import pygtk -except ImportError: - sys.stderr.write('pygtk is needed for using this script') - sys.exit(1) - -if sys.version_info < (2, 5, 0): - sys.stderr.write('Python 2.5.0 or higher is needed') - sys.exit(1) - -# a simple parser for the configuration -class ConfigParser(dict): - """ - ConfigParser(file) -> dict - - reads file and creates dict object containing corresponding - configuration - """ - - def __init__(self, filename): - self._section_match_regexp = re.compile(r'\[(\w+)\]') - self._key_val_match_regexp = re.compile(r'\s*=\s*') - self._comment_remov_regexp = re.compile(r'#.*') - try: - config_handle = open(filename, 'r') - except IOError: - sys.stderr.write('Could not read %s. Aborting ...\n' % filename) - sys.exit(1) - linecount = 0 - for line in config_handle.readlines(): - linecount += 1 - line = self._comment_remov_regexp.sub('', line.strip()) - if len(line) == 0: - continue - if self._section_match_regexp.match(line): - section = self._section_match_regexp.match(line).groups()[0] - self[section] = {} - continue - try: - (key, value) = self._key_val_match_regexp.split(line) - except ValueError: - sys.stderr.write('Syntax error in line %d of %s.' % (linecount, - filename)) - sys.exit(1) - if key == 'command' and not key in self[section]: - self[section][key] = [value] - continue - if key in self[section] and key == 'command': - self[section][key].append(value) - else: - self[section][key] = value - - def check(self): - """ - checks if configuration is complete - """ - for section in self: - for needed_key in ['command', 'image', 'info_text', 'label']: - if not needed_key in self[section]: - sys.stderr.write('Missing option %s in %s.' % (needed_key, section)) - sys.exit(1) - -# the main class for this script -class OpenBoxShutdown(object): - """ - main class for this script - - reads configuration and displays buttons as requested - - on click, the specified actions are made - """ - - def __init__(self, basedir): - self._config = ConfigParser(os.path.join(basedir, 'openbox-shutdown.conf')) - self._config.check() - # button box on top for the action buttons - button_box = gtk.HButtonBox() - for key in self._config: - button_box.pack_start(self._create_button(key, - self._config[key]['label'], - self._config[key]['info_text'], - self._config[key]['image'])) - button_box.show() - # create cancel button - cancel_button = gtk.Button(stock = gtk.STOCK_CANCEL) - cancel_button.connect('clicked', self._click_callback, 'cancel') - cancel_button.show() - # label widget for small hints - self._label = gtk.Label() - self._label.show() - # frame widget to contain label - frame = gtk.Frame() - frame.set_shadow_type(gtk.SHADOW_NONE) - frame.add(self._label) - frame.show() - # area containing hints and cancel button - action_area = gtk.HBox() - action_area.pack_end(cancel_button, expand=False) - action_area.pack_end(frame) - action_area.show() - # box containing button box and action area - vbox = gtk.VBox() - vbox.pack_start(button_box) - vbox.pack_end(action_area) - vbox.show() - # the main window with all widgets - # always on top and on all desktops - window = gtk.Window() - window.set_position('center') - window.set_decorated(False) - window.stick() - window.set_keep_above(True) - window.set_skip_pager_hint(True) - window.set_skip_taskbar_hint(True) - window.add(vbox) - window.show() - # give control to gtk - gtk.main() - - def _click_callback(self, event, action): - """ - _click_callback(event, action) - - executes commands on button click - """ - gtk.main_quit() - if action == 'cancel': - sys.exit(0) - for command in self._config[action]['command']: - os.system(command) - - def _create_button(self, key, label_text, info_text, image_file): - """ - _create_button(key, label, info, image) -> button - - creates a button widget with key (used for handling actions), - a label for the button, a small text as hint and an image - """ - button = gtk.Button() - if label_text != 'None': - button.set_label(label_text) - if image_file != 'None': - button.set_image(self._create_image(image_file)) - button.set_image_position(gtk.POS_TOP) - button.connect('clicked', self._click_callback, key) - button.connect('leave', lambda event: self._label.set_text('')) - button.connect('enter', self._set_label, info_text) - button.show() - return button - - def _create_image(self, image_file): - """ - _create_image(image) -> image - - creates an image widget from the specified file - """ - image = gtk.Image() - image.set_from_file(image_file) - return image - - def _set_label(self, event, info_text): - """ - _set_label(event, info_text) - - set label on entering the widget - """ - if info_text != 'None': - self._label.set_markup('%s' % info_text) - -# start the script if called standalone -if __name__ == '__main__': - # get the real path for the configuration file - conf_path = os.path.realpath(os.path.dirname(os.readlink(__file__))) \ - if os.path.islink(__file__) \ - else os.path.realpath(os.path.dirname(__file__)) - shutdown = OpenBoxShutdown(conf_path) diff --git a/python/shutdown.py b/python/shutdown.py deleted file mode 100755 index 6138a79..0000000 --- a/python/shutdown.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -#---------------------------------------------------- -# Version: 0.1.1 -# Author: Florian "Bluewind" Pritz -# -# Copyright (C) 2008-2009 Florian Pritz -# -# Licensed under GNU General Public License v3 -# (see COPYING for full license text) -# -#---------------------------------------------------- -# Display a small shutdown Dialog -#---------------------------------------------------- -# NOTE: -# Please change the image paths to mach your icons. -# Otherwise the script will crash. - - -import gtk -import os - -def create_bbox(title=None, spacing=0, layout=gtk.BUTTONBOX_SPREAD): - bbox = gtk.VButtonBox() - - bbox.set_spacing(spacing) - - buttons = (('Cancel', 'gtk-cancel'), - ('Suspend', 'gnome-session-suspend'), - ('Reboot', 'gnome-session-reboot'), - ('Halt', 'gnome-session-halt') - ) - for text, stock_id in buttons: - image = gtk.Image() - image.set_from_stock(stock_id, gtk.ICON_SIZE_LARGE_TOOLBAR) - b = gtk.Button(text) - b.set_image(image) - b.set_data("user_data", text) - b.connect("clicked", click, b) - b.set_size_request(100, -1) - b.set_alignment(0,1) - bbox.add(b) - - return bbox - -def click(self, button): - ret = button.get_data("user_data") - self.hide() - self.destroy() - gtk.main_quit() - - if ret == "Reboot": - os.system("sudo shutdown -r now") - if ret == "Suspend": - os.system("sudo pm-suspend") - if ret == "Halt": - os.system("sudo shutdown -h now") - -class ButtonBox(gtk.Window): - def __init__(self, parent=None): - # Create the toplevel window - gtk.Window.__init__(self) - try: - self.set_screen(parent.get_screen()) - except AttributeError: - self.connect('destroy', lambda *w: gtk.main_quit()) - - self.set_title("Shutdown") - #self.set_decorated(False) - self.set_position(gtk.WIN_POS_CENTER) - - hbox = gtk.HBox() - hbox.set_border_width(10) - - self.add(hbox) - hbox.pack_start(create_bbox(None, 8, gtk.BUTTONBOX_START), - padding=0) - - self.show_all() - -def register_iconsets(icon_info): - iconfactory = gtk.IconFactory() - stock_ids = gtk.stock_list_ids() - for stock_id, file in icon_info: - # only load image files when our stock_id is not present - if stock_id not in stock_ids: - pixbuf = gtk.gdk.pixbuf_new_from_file(file) - iconset = gtk.IconSet(pixbuf) - iconfactory.add(stock_id, iconset) - iconfactory.add_default() - -register_iconsets([('gnome-session-suspend', '/usr/share/icons/hydroxygen/128x128/apps/gnome-session-suspend.png'), - ('gnome-session-halt', '/usr/share/icons/hydroxygen/128x128/apps/gnome-session-halt.png'), - ('gnome-session-reboot', '/usr/share/icons/hydroxygen/128x128/apps/gnome-session-reboot.png') - ]) - -ButtonBox() -gtk.main() diff --git a/shutdown.py b/shutdown.py new file mode 100755 index 0000000..6138a79 --- /dev/null +++ b/shutdown.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python +#---------------------------------------------------- +# Version: 0.1.1 +# Author: Florian "Bluewind" Pritz +# +# Copyright (C) 2008-2009 Florian Pritz +# +# Licensed under GNU General Public License v3 +# (see COPYING for full license text) +# +#---------------------------------------------------- +# Display a small shutdown Dialog +#---------------------------------------------------- +# NOTE: +# Please change the image paths to mach your icons. +# Otherwise the script will crash. + + +import gtk +import os + +def create_bbox(title=None, spacing=0, layout=gtk.BUTTONBOX_SPREAD): + bbox = gtk.VButtonBox() + + bbox.set_spacing(spacing) + + buttons = (('Cancel', 'gtk-cancel'), + ('Suspend', 'gnome-session-suspend'), + ('Reboot', 'gnome-session-reboot'), + ('Halt', 'gnome-session-halt') + ) + for text, stock_id in buttons: + image = gtk.Image() + image.set_from_stock(stock_id, gtk.ICON_SIZE_LARGE_TOOLBAR) + b = gtk.Button(text) + b.set_image(image) + b.set_data("user_data", text) + b.connect("clicked", click, b) + b.set_size_request(100, -1) + b.set_alignment(0,1) + bbox.add(b) + + return bbox + +def click(self, button): + ret = button.get_data("user_data") + self.hide() + self.destroy() + gtk.main_quit() + + if ret == "Reboot": + os.system("sudo shutdown -r now") + if ret == "Suspend": + os.system("sudo pm-suspend") + if ret == "Halt": + os.system("sudo shutdown -h now") + +class ButtonBox(gtk.Window): + def __init__(self, parent=None): + # Create the toplevel window + gtk.Window.__init__(self) + try: + self.set_screen(parent.get_screen()) + except AttributeError: + self.connect('destroy', lambda *w: gtk.main_quit()) + + self.set_title("Shutdown") + #self.set_decorated(False) + self.set_position(gtk.WIN_POS_CENTER) + + hbox = gtk.HBox() + hbox.set_border_width(10) + + self.add(hbox) + hbox.pack_start(create_bbox(None, 8, gtk.BUTTONBOX_START), + padding=0) + + self.show_all() + +def register_iconsets(icon_info): + iconfactory = gtk.IconFactory() + stock_ids = gtk.stock_list_ids() + for stock_id, file in icon_info: + # only load image files when our stock_id is not present + if stock_id not in stock_ids: + pixbuf = gtk.gdk.pixbuf_new_from_file(file) + iconset = gtk.IconSet(pixbuf) + iconfactory.add(stock_id, iconset) + iconfactory.add_default() + +register_iconsets([('gnome-session-suspend', '/usr/share/icons/hydroxygen/128x128/apps/gnome-session-suspend.png'), + ('gnome-session-halt', '/usr/share/icons/hydroxygen/128x128/apps/gnome-session-halt.png'), + ('gnome-session-reboot', '/usr/share/icons/hydroxygen/128x128/apps/gnome-session-reboot.png') + ]) + +ButtonBox() +gtk.main() -- cgit v1.2.3-24-g4f1b