diff --git a/scripts/python/app/NUT-Monitor b/scripts/python/app/NUT-Monitor index ce1300411e..f4b9ce1065 100755 --- a/scripts/python/app/NUT-Monitor +++ b/scripts/python/app/NUT-Monitor @@ -28,269 +28,274 @@ # Corrected unsafe permissions on ~/.nut-monitor (Debian #777706) -import gtk, gtk.glade, gobject -import sys import base64 -import os, os.path -import stat +import gettext +import optparse +import os +import os.path import platform -import time +import stat +import sys import threading -import optparse -import ConfigParser -import locale -import gettext -import PyNUT +import time +try: + from configparser import ConfigParser # Py3 +except ImportError: + from Configparser import ConfigParser # Py2 + +import PyNUT +import gobject +import gtk +import gtk.glade # Activate threadings on glib gobject.threads_init() -class interface : - DESIRED_FAVORITES_DIRECTORY_MODE = 0700 +class interface(object): + DESIRED_FAVORITES_DIRECTORY_MODE = 0o700 - __widgets = {} - __callbacks = {} - __favorites = {} - __favorites_file = None - __favorites_path = "" - __fav_menu_items = list() - __window_visible = True - __glade_file = None - __connected = False - __ups_handler = None - __ups_commands = None - __ups_vars = None - __ups_rw_vars = None - __gui_thread = None - __current_ups = None + __widgets = {} + __callbacks = {} + __favorites = {} + __favorites_file = None + __favorites_path = "" + __fav_menu_items = list() + __window_visible = True + __glade_file = None + __connected = False + __ups_handler = None + __ups_commands = None + __ups_vars = None + __ups_rw_vars = None + __gui_thread = None + __current_ups = None - def __init__( self ) : + def __init__(self): # Before anything, parse command line options if any present... opt_parser = optparse.OptionParser() - opt_parser.add_option( "-H", "--start-hidden", action="store_true", default=False, dest="hidden", help="Start iconified in tray" ) - opt_parser.add_option( "-F", "--favorite", dest="favorite", help="Load the specified favorite and connect to UPS" ) - - ( cmd_opts, args ) = opt_parser.parse_args() - - - self.__glade_file = os.path.join( os.path.dirname( sys.argv[0] ), "gui-1.3.glade" ) - - self.__widgets["interface"] = gtk.glade.XML( self.__glade_file, "window1", APP ) - self.__widgets["main_window"] = self.__widgets["interface"].get_widget("window1") - self.__widgets["status_bar"] = self.__widgets["interface"].get_widget("statusbar2") - self.__widgets["ups_host_entry"] = self.__widgets["interface"].get_widget("entry1") - self.__widgets["ups_port_entry"] = self.__widgets["interface"].get_widget("spinbutton1") - self.__widgets["ups_refresh_button"] = self.__widgets["interface"].get_widget("button1") - self.__widgets["ups_authentication_check"] = self.__widgets["interface"].get_widget("checkbutton1") - self.__widgets["ups_authentication_frame"] = self.__widgets["interface"].get_widget("hbox1") - self.__widgets["ups_authentication_login"] = self.__widgets["interface"].get_widget("entry2") + opt_parser.add_option("-H", "--start-hidden", action="store_true", default=False, dest="hidden", help="Start iconified in tray") + opt_parser.add_option("-F", "--favorite", dest="favorite", help="Load the specified favorite and connect to UPS") + + (cmd_opts, args) = opt_parser.parse_args() + + self.__glade_file = os.path.join(os.path.dirname(sys.argv[0]), "gui-1.3.glade") + + self.__widgets["interface"] = gtk.glade.XML(self.__glade_file, "window1", APP) + self.__widgets["main_window"] = self.__widgets["interface"].get_widget("window1") + self.__widgets["status_bar"] = self.__widgets["interface"].get_widget("statusbar2") + self.__widgets["ups_host_entry"] = self.__widgets["interface"].get_widget("entry1") + self.__widgets["ups_port_entry"] = self.__widgets["interface"].get_widget("spinbutton1") + self.__widgets["ups_refresh_button"] = self.__widgets["interface"].get_widget("button1") + self.__widgets["ups_authentication_check"] = self.__widgets["interface"].get_widget("checkbutton1") + self.__widgets["ups_authentication_frame"] = self.__widgets["interface"].get_widget("hbox1") + self.__widgets["ups_authentication_login"] = self.__widgets["interface"].get_widget("entry2") self.__widgets["ups_authentication_password"] = self.__widgets["interface"].get_widget("entry3") - self.__widgets["ups_list_combo"] = self.__widgets["interface"].get_widget("combobox1") - self.__widgets["ups_commands_button"] = self.__widgets["interface"].get_widget("button8") - self.__widgets["ups_connect"] = self.__widgets["interface"].get_widget("button2") - self.__widgets["ups_disconnect"] = self.__widgets["interface"].get_widget("button7") - self.__widgets["ups_params_box"] = self.__widgets["interface"].get_widget("vbox6") - self.__widgets["ups_infos"] = self.__widgets["interface"].get_widget("notebook1") - self.__widgets["ups_vars_tree"] = self.__widgets["interface"].get_widget("treeview1") - self.__widgets["ups_vars_refresh"] = self.__widgets["interface"].get_widget("button9") - self.__widgets["ups_status_image"] = self.__widgets["interface"].get_widget("image1") - self.__widgets["ups_status_left"] = self.__widgets["interface"].get_widget("label10") - self.__widgets["ups_status_right"] = self.__widgets["interface"].get_widget("label11") - self.__widgets["ups_status_time"] = self.__widgets["interface"].get_widget("label15") - self.__widgets["menu_favorites_root"] = self.__widgets["interface"].get_widget("menuitem3") - self.__widgets["menu_favorites"] = self.__widgets["interface"].get_widget("menu2") - self.__widgets["menu_favorites_add"] = self.__widgets["interface"].get_widget("menuitem4") - self.__widgets["menu_favorites_del"] = self.__widgets["interface"].get_widget("menuitem5") - self.__widgets["progress_battery_charge"] = self.__widgets["interface"].get_widget("progressbar1") - self.__widgets["progress_battery_load"] = self.__widgets["interface"].get_widget("progressbar2") + self.__widgets["ups_list_combo"] = self.__widgets["interface"].get_widget("combobox1") + self.__widgets["ups_commands_button"] = self.__widgets["interface"].get_widget("button8") + self.__widgets["ups_connect"] = self.__widgets["interface"].get_widget("button2") + self.__widgets["ups_disconnect"] = self.__widgets["interface"].get_widget("button7") + self.__widgets["ups_params_box"] = self.__widgets["interface"].get_widget("vbox6") + self.__widgets["ups_infos"] = self.__widgets["interface"].get_widget("notebook1") + self.__widgets["ups_vars_tree"] = self.__widgets["interface"].get_widget("treeview1") + self.__widgets["ups_vars_refresh"] = self.__widgets["interface"].get_widget("button9") + self.__widgets["ups_status_image"] = self.__widgets["interface"].get_widget("image1") + self.__widgets["ups_status_left"] = self.__widgets["interface"].get_widget("label10") + self.__widgets["ups_status_right"] = self.__widgets["interface"].get_widget("label11") + self.__widgets["ups_status_time"] = self.__widgets["interface"].get_widget("label15") + self.__widgets["menu_favorites_root"] = self.__widgets["interface"].get_widget("menuitem3") + self.__widgets["menu_favorites"] = self.__widgets["interface"].get_widget("menu2") + self.__widgets["menu_favorites_add"] = self.__widgets["interface"].get_widget("menuitem4") + self.__widgets["menu_favorites_del"] = self.__widgets["interface"].get_widget("menuitem5") + self.__widgets["progress_battery_charge"] = self.__widgets["interface"].get_widget("progressbar1") + self.__widgets["progress_battery_load"] = self.__widgets["interface"].get_widget("progressbar2") # Create the tray icon and connect it to the show/hide method... self.__widgets["status_icon"] = gtk.StatusIcon() - self.__widgets["status_icon"].set_from_file( os.path.join( os.path.dirname( sys.argv[0] ), "pixmaps", "on_line.png" ) ) - self.__widgets["status_icon"].set_visible( True ) - self.__widgets["status_icon"].connect( "activate", self.tray_activated ) + self.__widgets["status_icon"].set_from_file(os.path.join(os.path.dirname(sys.argv[0]), "pixmaps", "on_line.png")) + self.__widgets["status_icon"].set_visible(True) + self.__widgets["status_icon"].connect("activate", self.tray_activated) - self.__widgets["ups_status_image"].set_from_file( os.path.join( os.path.dirname( sys.argv[0] ), "pixmaps", "on_line.png" ) ) + self.__widgets["ups_status_image"].set_from_file(os.path.join(os.path.dirname(sys.argv[0]), "pixmaps", "on_line.png")) # Define interface callbacks actions - self.__callbacks = { "on_window1_destroy" : self.quit, - "on_imagemenuitem1_activate" : self.gui_about_dialog, - "on_imagemenuitem5_activate" : self.quit, - "on_entry1_changed" : self.__check_gui_fields, - "on_entry2_changed" : self.__check_gui_fields, - "on_entry3_changed" : self.__check_gui_fields, - "on_checkbutton1_toggled" : self.__check_gui_fields, - "on_spinbutton1_value_changed" : self.__check_gui_fields, - "on_button1_clicked" : self.__update_ups_list, - "on_button2_clicked" : self.connect_to_ups, - "on_button7_clicked" : self.disconnect_from_ups, - "on_button9_clicked" : self.__gui_update_ups_vars_view, - "on_menuitem4_activate" : self.__gui_add_favorite, - "on_menuitem5_activate" : self.__gui_delete_favorite, - "on_treeview1_button_press_event" : self.__gui_ups_vars_selected - } + self.__callbacks = {"on_window1_destroy": self.quit, + "on_imagemenuitem1_activate": self.gui_about_dialog, + "on_imagemenuitem5_activate": self.quit, + "on_entry1_changed": self.__check_gui_fields, + "on_entry2_changed": self.__check_gui_fields, + "on_entry3_changed": self.__check_gui_fields, + "on_checkbutton1_toggled": self.__check_gui_fields, + "on_spinbutton1_value_changed": self.__check_gui_fields, + "on_button1_clicked": self.__update_ups_list, + "on_button2_clicked": self.connect_to_ups, + "on_button7_clicked": self.disconnect_from_ups, + "on_button9_clicked": self.__gui_update_ups_vars_view, + "on_menuitem4_activate": self.__gui_add_favorite, + "on_menuitem5_activate": self.__gui_delete_favorite, + "on_treeview1_button_press_event": self.__gui_ups_vars_selected + } # Connect the callbacks - self.__widgets["interface"].signal_autoconnect( self.__callbacks ) + self.__widgets["interface"].signal_autoconnect(self.__callbacks) # Remove the dummy combobox entry on UPS List and Commands - self.__widgets["ups_list_combo"].remove_text( 0 ) + self.__widgets["ups_list_combo"].remove_text(0) # Set UPS vars treeview properties ----------------------------- - store = gtk.ListStore( gtk.gdk.Pixbuf, gobject.TYPE_STRING, gobject.TYPE_STRING ) - self.__widgets["ups_vars_tree"].set_model( store ) - self.__widgets["ups_vars_tree"].set_headers_visible( True ) + store = gtk.ListStore(gtk.gdk.Pixbuf, gobject.TYPE_STRING, gobject.TYPE_STRING) + self.__widgets["ups_vars_tree"].set_model(store) + self.__widgets["ups_vars_tree"].set_headers_visible(True) # Column 0 cr = gtk.CellRendererPixbuf() - column = gtk.TreeViewColumn( '', cr ) - column.add_attribute( cr, 'pixbuf', 0 ) - self.__widgets["ups_vars_tree"].append_column( column ) + column = gtk.TreeViewColumn('', cr) + column.add_attribute(cr, 'pixbuf', 0) + self.__widgets["ups_vars_tree"].append_column(column) # Column 1 cr = gtk.CellRendererText() - cr.set_property( 'editable', False ) - column = gtk.TreeViewColumn( _('Var name'), cr ) - column.set_sort_column_id( 1 ) - column.add_attribute( cr, 'text', 1 ) - self.__widgets["ups_vars_tree"].append_column( column ) + cr.set_property('editable', False) + column = gtk.TreeViewColumn(_('Var name'), cr) + column.set_sort_column_id(1) + column.add_attribute(cr, 'text', 1) + self.__widgets["ups_vars_tree"].append_column(column) # Column 2 cr = gtk.CellRendererText() - cr.set_property( 'editable', False ) - column = gtk.TreeViewColumn( _('Value'), cr ) - column.add_attribute( cr, 'text', 2 ) - self.__widgets["ups_vars_tree"].append_column( column ) + cr.set_property('editable', False) + column = gtk.TreeViewColumn(_('Value'), cr) + column.add_attribute(cr, 'text', 2) + self.__widgets["ups_vars_tree"].append_column(column) - self.__widgets["ups_vars_tree"].get_model().set_sort_column_id( 1, gtk.SORT_ASCENDING ) + self.__widgets["ups_vars_tree"].get_model().set_sort_column_id(1, gtk.SORT_ASCENDING) self.__widgets["ups_vars_tree_store"] = store - self.__widgets["ups_vars_tree"].set_size_request( -1, 50 ) - #--------------------------------------------------------------- + self.__widgets["ups_vars_tree"].set_size_request(-1, 50) + # --------------------------------------------------------------- # UPS Commands combo box creation ------------------------------ container = self.__widgets["ups_commands_button"].get_parent() self.__widgets["ups_commands_button"].destroy() self.__widgets["ups_commands_combo"] = gtk.ComboBox() - list_store = gtk.ListStore( gobject.TYPE_STRING ) + list_store = gtk.ListStore(gobject.TYPE_STRING) - self.__widgets["ups_commands_combo"].set_model( list_store ) + self.__widgets["ups_commands_combo"].set_model(list_store) cell_renderer = gtk.CellRendererText() - cell_renderer.set_property( "xalign", 0 ) - self.__widgets["ups_commands_combo"].pack_start( cell_renderer, True ) - self.__widgets["ups_commands_combo"].add_attribute( cell_renderer, "markup", 0 ) + cell_renderer.set_property("xalign", 0) + self.__widgets["ups_commands_combo"].pack_start(cell_renderer, True) + self.__widgets["ups_commands_combo"].add_attribute(cell_renderer, "markup", 0) - container.pack_start( self.__widgets["ups_commands_combo"], True ) - self.__widgets["ups_commands_combo"].set_active( 0 ) + container.pack_start(self.__widgets["ups_commands_combo"], True) + self.__widgets["ups_commands_combo"].set_active(0) self.__widgets["ups_commands_combo"].show_all() - self.__widgets["ups_commands_button"] = gtk.Button( stock=gtk.STOCK_EXECUTE ) - container.pack_start( self.__widgets["ups_commands_button"], True ) + self.__widgets["ups_commands_button"] = gtk.Button(stock=gtk.STOCK_EXECUTE) + container.pack_start(self.__widgets["ups_commands_button"], True) self.__widgets["ups_commands_button"].show() - self.__widgets["ups_commands_button"].connect( "clicked", self.__gui_send_ups_command ) + self.__widgets["ups_commands_button"].connect("clicked", self.__gui_send_ups_command) self.__widgets["ups_commands_combo_store"] = list_store - #--------------------------------------------------------------- + # --------------------------------------------------------------- - if ( cmd_opts.hidden != True ) : + if not cmd_opts.hidden: self.__widgets["main_window"].show() # Define favorites path and load favorites - if ( platform.system() == "Linux" ) : - self.__favorites_path = os.path.join( os.environ.get("HOME"), ".nut-monitor" ) - elif ( platform.system() == "Windows" ) : - self.__favorites_path = os.path.join( os.environ.get("USERPROFILE"), "Application Data", "NUT-Monitor" ) + if platform.system() == "Linux": + self.__favorites_path = os.path.join(os.environ.get("HOME"), ".nut-monitor") + elif platform.system() == "Windows": + self.__favorites_path = os.path.join(os.environ.get("USERPROFILE"), "Application Data", "NUT-Monitor") - self.__favorites_file = os.path.join( self.__favorites_path, "favorites.ini" ) + self.__favorites_file = os.path.join(self.__favorites_path, "favorites.ini") self.__parse_favorites() - self.gui_status_message( _("Welcome to NUT Monitor") ) + self.gui_status_message(_("Welcome to NUT Monitor")) - if ( cmd_opts.favorite != None ) : - if ( self.__favorites.has_key( cmd_opts.favorite ) ) : - self.__gui_load_favorite( fav_name=cmd_opts.favorite ) + if cmd_opts.favorite is not None: + if cmd_opts.favorite in self.__favorites.keys(): + self.__gui_load_favorite(fav_name=cmd_opts.favorite) self.connect_to_ups() - else : + else: # Try to scan localhost for available ups and connect to it if there is only one - self.__widgets["ups_host_entry"].set_text( "localhost" ) + self.__widgets["ups_host_entry"].set_text("localhost") self.__update_ups_list() - if ( len( self.__widgets["ups_list_combo"].get_model() ) == 1 ) : + if len(self.__widgets["ups_list_combo"].get_model()) == 1: self.connect_to_ups() # Check if correct fields are filled to enable connection to the UPS - def __check_gui_fields( self, widget=None ) : + def __check_gui_fields(self, widget=None): # If UPS list contains something, clear it - if self.__widgets["ups_list_combo"].get_active() != -1 : + if self.__widgets["ups_list_combo"].get_active() != -1: self.__widgets["ups_list_combo"].get_model().clear() - self.__widgets["ups_connect"].set_sensitive( False ) - self.__widgets["menu_favorites_add"].set_sensitive( False ) + self.__widgets["ups_connect"].set_sensitive(False) + self.__widgets["menu_favorites_add"].set_sensitive(False) # Host/Port selection - if len( self.__widgets["ups_host_entry"].get_text() ) > 0 : + if len(self.__widgets["ups_host_entry"].get_text()) > 0: sensitive = True # If authentication is selected, check that we have a login and password - if self.__widgets["ups_authentication_check"].get_active() : - if len( self.__widgets["ups_authentication_login"].get_text() ) == 0 : + if self.__widgets["ups_authentication_check"].get_active(): + if len(self.__widgets["ups_authentication_login"].get_text()) == 0: sensitive = False - if len( self.__widgets["ups_authentication_password"].get_text() ) == 0 : + if len(self.__widgets["ups_authentication_password"].get_text()) == 0: sensitive = False - self.__widgets["ups_refresh_button"].set_sensitive( sensitive ) - if not sensitive : - self.__widgets["ups_connect"].set_sensitive( False ) - self.__widgets["menu_favorites_add"].set_sensitive( False ) - else : - self.__widgets["ups_refresh_button"].set_sensitive( False ) - self.__widgets["ups_connect"].set_sensitive( False ) - self.__widgets["menu_favorites_add"].set_sensitive( False ) + self.__widgets["ups_refresh_button"].set_sensitive(sensitive) + if not sensitive: + self.__widgets["ups_connect"].set_sensitive(False) + self.__widgets["menu_favorites_add"].set_sensitive(False) + else: + self.__widgets["ups_refresh_button"].set_sensitive(False) + self.__widgets["ups_connect"].set_sensitive(False) + self.__widgets["menu_favorites_add"].set_sensitive(False) # Use authentication fields... - if self.__widgets["ups_authentication_check"].get_active() : - self.__widgets["ups_authentication_frame"].set_sensitive( True ) - else : - self.__widgets["ups_authentication_frame"].set_sensitive( False ) + if self.__widgets["ups_authentication_check"].get_active(): + self.__widgets["ups_authentication_frame"].set_sensitive(True) + else: + self.__widgets["ups_authentication_frame"].set_sensitive(False) self.gui_status_message() - #------------------------------------------------------------------- + # ------------------------------------------------------------------- # This method is used to show/hide the main window when user clicks on the tray icon - def tray_activated( self, widget=None, data=None ) : - if self.__window_visible : + def tray_activated(self, widget=None, data=None): + if self.__window_visible: self.__widgets["main_window"].hide() - else : + else: self.__widgets["main_window"].show() self.__window_visible = not self.__window_visible - #------------------------------------------------------------------- + # ------------------------------------------------------------------- # Change the status icon and tray icon - def change_status_icon( self, icon="on_line", blink=False ) : - self.__widgets["status_icon"].set_from_file( os.path.join( os.path.dirname( sys.argv[0] ), "pixmaps", "%s.png" % icon ) ) - self.__widgets["ups_status_image"].set_from_file( os.path.join( os.path.dirname( sys.argv[0] ), "pixmaps", "%s.png" % icon ) ) - self.__widgets["status_icon"].set_blinking( blink ) + def change_status_icon(self, icon="on_line", blink=False): + self.__widgets["status_icon"].set_from_file(os.path.join(os.path.dirname(sys.argv[0]), "pixmaps", "%s.png" % icon)) + self.__widgets["ups_status_image"].set_from_file(os.path.join(os.path.dirname(sys.argv[0]), "pixmaps", "%s.png" % icon)) + self.__widgets["status_icon"].set_blinking(blink) - #------------------------------------------------------------------- + # ------------------------------------------------------------------- # This method connects to the NUT server and retrieve availables UPSes # using connection parameters (host, port, login, pass...) - def __update_ups_list( self, widget=None ) : + def __update_ups_list(self, widget=None): - host = self.__widgets["ups_host_entry"].get_text() - port = int( self.__widgets["ups_port_entry"].get_value() ) - login = None + host = self.__widgets["ups_host_entry"].get_text() + port = int(self.__widgets["ups_port_entry"].get_value()) + login = None password = None - if self.__widgets["ups_authentication_check"].get_active() : - login = self.__widgets["ups_authentication_login"].get_text() + if self.__widgets["ups_authentication_check"].get_active(): + login = self.__widgets["ups_authentication_login"].get_text() password = self.__widgets["ups_authentication_password"].get_text() - try : - nut_handler = PyNUT.PyNUTClient( host=host, port=port, login=login, password=password ) + try: + nut_handler = PyNUT.PyNUTClient(host=host, port=port, login=login, password=password) upses = nut_handler.GetUPSList() ups_list = upses.keys() @@ -299,614 +304,611 @@ class interface : # If UPS list contains something, clear it self.__widgets["ups_list_combo"].get_model().clear() - for current in ups_list : - self.__widgets["ups_list_combo"].append_text( current ) + for current in ups_list: + self.__widgets["ups_list_combo"].append_text(current) - self.__widgets["ups_list_combo"].set_active( 0 ) + self.__widgets["ups_list_combo"].set_active(0) - self.__widgets["ups_connect"].set_sensitive( True ) - self.__widgets["menu_favorites_add"].set_sensitive( True ) + self.__widgets["ups_connect"].set_sensitive(True) + self.__widgets["menu_favorites_add"].set_sensitive(True) - self.gui_status_message( _("Found {0} devices on {1}").format( len( ups_list ), host ) ) + self.gui_status_message(_("Found {0} devices on {1}").format(len(ups_list), host)) - except : - error_msg = _("Error connecting to '{0}' ({1})").format( host, sys.exc_info()[1] ) - self.gui_status_message( error_msg ) + except: + error_msg = _("Error connecting to '{0}' ({1})").format(host, sys.exc_info()[1]) + self.gui_status_message(error_msg) - #------------------------------------------------------------------- + # ------------------------------------------------------------------- # Quit program - def quit( self, widget=None ) : + def quit(self, widget=None): # If we are connected to an UPS, disconnect first... - if self.__connected : - self.gui_status_message( _("Disconnecting from device") ) + if self.__connected: + self.gui_status_message(_("Disconnecting from device")) self.disconnect_from_ups() gtk.main_quit() - #------------------------------------------------------------------- + # ------------------------------------------------------------------- # Method called when user wants to add a new favorite entry. It # displays a dialog to enable user to select the name of the favorite - def __gui_add_favorite( self, widget=None ) : - dialog_interface = gtk.glade.XML( self.__glade_file, "dialog1" ) - dialog = dialog_interface.get_widget( "dialog1" ) + def __gui_add_favorite(self, widget=None): + dialog_interface = gtk.glade.XML(self.__glade_file, "dialog1") + dialog = dialog_interface.get_widget("dialog1") self.__widgets["favorites_dialog_button_add"] = dialog_interface.get_widget("button3") # Define interface callbacks actions - callbacks = { "on_entry4_changed" : self.__gui_add_favorite_check_gui_fields } - dialog_interface.signal_autoconnect( callbacks ) + callbacks = {"on_entry4_changed": self.__gui_add_favorite_check_gui_fields} + dialog_interface.signal_autoconnect(callbacks) - self.__widgets["main_window"].set_sensitive( False ) + self.__widgets["main_window"].set_sensitive(False) rc = dialog.run() - if rc == 1 : + if rc == 1: fav_data = {} fav_data["host"] = self.__widgets["ups_host_entry"].get_text() fav_data["port"] = "%d" % self.__widgets["ups_port_entry"].get_value() - fav_data["ups"] = self.__widgets["ups_list_combo"].get_active_text() + fav_data["ups"] = self.__widgets["ups_list_combo"].get_active_text() fav_data["auth"] = self.__widgets["ups_authentication_check"].get_active() - if fav_data["auth"] : - fav_data["login"] = self.__widgets["ups_authentication_login"].get_text() - fav_data["password"] = base64.b64encode( self.__widgets["ups_authentication_password"].get_text() ) + if fav_data["auth"]: + fav_data["login"] = self.__widgets["ups_authentication_login"].get_text() + fav_data["password"] = base64.b64encode(self.__widgets["ups_authentication_password"].get_text()) fav_name = dialog_interface.get_widget("entry4").get_text() - self.__favorites[ fav_name ] = fav_data + self.__favorites[fav_name] = fav_data self.__gui_refresh_favorites_menu() # Save all favorites self.__save_favorites() dialog.destroy() - self.__widgets["main_window"].set_sensitive( True ) + self.__widgets["main_window"].set_sensitive(True) - #------------------------------------------------------------------- + # ------------------------------------------------------------------- # Method called when user wants to delete an entry from favorites - def __gui_delete_favorite( self, widget=None ) : + def __gui_delete_favorite(self, widget=None): - dialog_interface = gtk.glade.XML( self.__glade_file, "dialog2" ) - dialog = dialog_interface.get_widget( "dialog2" ) + dialog_interface = gtk.glade.XML(self.__glade_file, "dialog2") + dialog = dialog_interface.get_widget("dialog2") # Remove the dummy combobox entry on list - dialog_interface.get_widget("combobox2").remove_text( 0 ) + dialog_interface.get_widget("combobox2").remove_text(0) - favs = self.__favorites.keys() + favs = list(self.__favorites.keys()) favs.sort() - for current in favs : - dialog_interface.get_widget("combobox2").append_text( current ) + for current in favs: + dialog_interface.get_widget("combobox2").append_text(current) - dialog_interface.get_widget("combobox2").set_active( 0 ) + dialog_interface.get_widget("combobox2").set_active(0) - self.__widgets["main_window"].set_sensitive( False ) + self.__widgets["main_window"].set_sensitive(False) rc = dialog.run() fav_name = dialog_interface.get_widget("combobox2").get_active_text() dialog.destroy() - self.__widgets["main_window"].set_sensitive( True ) + self.__widgets["main_window"].set_sensitive(True) - if ( rc == 1 ) : + if rc == 1: # Remove entry, show confirmation dialog - md = gtk.MessageDialog( None, gtk.DIALOG_MODAL, gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, _("Are you sure that you want to remove this favorite ?") ) + md = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, _("Are you sure that you want to remove this favorite ?")) resp = md.run() md.destroy() - if ( resp == gtk.RESPONSE_YES ) : - del self.__favorites[ fav_name ] + if resp == gtk.RESPONSE_YES: + del self.__favorites[fav_name] self.__gui_refresh_favorites_menu() self.__save_favorites() - self.gui_status_message( _("Removed favorite '%s'") % fav_name ) + self.gui_status_message(_("Removed favorite '%s'") % fav_name) - #------------------------------------------------------------------- + # ------------------------------------------------------------------- # Method called when user selects a favorite from the favorites menu - def __gui_load_favorite( self, fav_name="" ) : + def __gui_load_favorite(self, fav_name=""): - if ( self.__favorites.has_key( fav_name ) ) : + if fav_name in self.__favorites.keys(): # If auth is activated, process it before other fields to avoir weird # reactions with the 'check_gui_fields' function. - if ( self.__favorites[fav_name].get("auth", False ) ) : - self.__widgets["ups_authentication_check"].set_active( True ) - self.__widgets["ups_authentication_login"].set_text( self.__favorites[fav_name].get("login","") ) - self.__widgets["ups_authentication_password"].set_text( self.__favorites[fav_name].get("password","") ) + if self.__favorites[fav_name].get("auth", False): + self.__widgets["ups_authentication_check"].set_active(True) + self.__widgets["ups_authentication_login"].set_text(self.__favorites[fav_name].get("login", "")) + self.__widgets["ups_authentication_password"].set_text(self.__favorites[fav_name].get("password", "")) - self.__widgets["ups_host_entry"].set_text( self.__favorites[fav_name].get("host","") ) - self.__widgets["ups_port_entry"].set_value( float(self.__favorites[fav_name].get("port",3493.0)) ) + self.__widgets["ups_host_entry"].set_text(self.__favorites[fav_name].get("host", "")) + self.__widgets["ups_port_entry"].set_value(float(self.__favorites[fav_name].get("port", 3493.0))) # Clear UPS list and add current UPS name self.__widgets["ups_list_combo"].get_model().clear() - self.__widgets["ups_list_combo"].append_text( self.__favorites[fav_name].get("ups","") ) - self.__widgets["ups_list_combo"].set_active( 0 ) + self.__widgets["ups_list_combo"].append_text(self.__favorites[fav_name].get("ups", "")) + self.__widgets["ups_list_combo"].set_active(0) # Activate the connect button - self.__widgets["ups_connect"].set_sensitive( True ) + self.__widgets["ups_connect"].set_sensitive(True) - self.gui_status_message( _("Loaded '%s'") % fav_name ) + self.gui_status_message(_("Loaded '%s'") % fav_name) - #------------------------------------------------------------------- + # ------------------------------------------------------------------- # Send the selected command to the UPS - def __gui_send_ups_command( self, widget=None ) : + def __gui_send_ups_command(self, widget=None): offset = self.__widgets["ups_commands_combo"].get_active() - cmd = self.__ups_commands[ offset ] + cmd = self.__ups_commands[offset] - md = gtk.MessageDialog( None, gtk.DIALOG_MODAL, gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, _("Are you sure that you want to send\n'%s' to the device ?") % cmd ) - self.__widgets["main_window"].set_sensitive( False ) + md = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, _("Are you sure that you want to send\n'%s' to the device ?") % cmd) + self.__widgets["main_window"].set_sensitive(False) resp = md.run() md.destroy() - self.__widgets["main_window"].set_sensitive( True ) + self.__widgets["main_window"].set_sensitive(True) - if ( resp == gtk.RESPONSE_YES ) : - try : - self.__ups_handler.RunUPSCommand( self.__current_ups, cmd ) - self.gui_status_message( _("Sent '{0}' command to {1}").format( cmd, self.__current_ups ) ) + if resp == gtk.RESPONSE_YES: + try: + self.__ups_handler.RunUPSCommand(self.__current_ups, cmd) + self.gui_status_message(_("Sent '{0}' command to {1}").format(cmd, self.__current_ups)) - except : - self.gui_status_message( _("Failed to send '{0}' ({1})").format( cmd, sys.exc_info()[1] ) ) + except: + self.gui_status_message(_("Failed to send '{0}' ({1})").format(cmd, sys.exc_info()[1])) - #------------------------------------------------------------------- + # ------------------------------------------------------------------- # Method called when user clicks on the UPS vars treeview. If the user # performs a double click on a RW var, the GUI shows the update var dialog. - def __gui_ups_vars_selected( self, widget, event ) : + def __gui_ups_vars_selected(self, widget, event): # Check if it's a double click... - if ( (event.button == 1) and (event.type == gtk.gdk._2BUTTON_PRESS) ) : + if (event.button == 1) and (event.type == gtk.gdk._2BUTTON_PRESS): treeselection = self.__widgets["ups_vars_tree"].get_selection() - (model,iter) = treeselection.get_selected() - try : - ups_var = model.get_value( iter, 1 ) - if ( ups_var in self.__ups_rw_vars ) : + (model, iter_) = treeselection.get_selected() + try: + ups_var = model.get_value(iter_, 1) + if ups_var in self.__ups_rw_vars: # The selected var is RW, then we can show the update dialog - dialog_interface = gtk.glade.XML( self.__glade_file, "dialog3" ) - dialog = dialog_interface.get_widget( "dialog3" ) + dialog_interface = gtk.glade.XML(self.__glade_file, "dialog3") + dialog = dialog_interface.get_widget("dialog3") - lab = dialog_interface.get_widget( "label9" ) - lab.set_markup( _("Enter a new value for the variable.\n\n{0} = {1} (current value)").format( ups_var, self.__ups_rw_vars.get(ups_var)) ) + lab = dialog_interface.get_widget("label9") + lab.set_markup(_("Enter a new value for the variable.\n\n{0} = {1} (current value)").format(ups_var, self.__ups_rw_vars.get(ups_var))) - str = dialog_interface.get_widget( "entry5" ) - str.set_text( self.__ups_rw_vars.get(ups_var) ) + str_ = dialog_interface.get_widget("entry5") + str_.set_text(self.__ups_rw_vars.get(ups_var)) - self.__widgets["main_window"].set_sensitive( False ) + self.__widgets["main_window"].set_sensitive(False) rc = dialog.run() - new_val = str.get_text() + new_val = str_.get_text() dialog.destroy() - self.__widgets["main_window"].set_sensitive( True ) + self.__widgets["main_window"].set_sensitive(True) - if ( rc == 1 ) : - try : - self.__ups_handler.SetRWVar( ups=self.__current_ups, var=ups_var, value=new_val ) - self.gui_status_message( _("Updated variable on %s") % self.__current_ups ) + if rc == 1: + try: + self.__ups_handler.SetRWVar(ups=self.__current_ups, var=ups_var, value=new_val) + self.gui_status_message(_("Updated variable on %s") % self.__current_ups) # Change the value on the local dict to update the GUI - self.__ups_vars[ups_var] = new_val + self.__ups_vars[ups_var] = new_val self.__ups_rw_vars[ups_var] = new_val self.__gui_update_ups_vars_view() - except : - error_msg = _("Error updating variable on '{0}' ({1})").format( self.__current_ups, sys.exc_info()[1] ) - self.gui_status_message( error_msg ) + except: + error_msg = _("Error updating variable on '{0}' ({1})").format(self.__current_ups, sys.exc_info()[1]) + self.gui_status_message(error_msg) - else : + else: # User cancelled modification... error_msg = _("No variable modified on %s - User cancelled") % self.__current_ups - self.gui_status_message( error_msg ) + self.gui_status_message(error_msg) - except : + except: # Failed to get information from the treeview... skip action pass - #------------------------------------------------------------------- + # ------------------------------------------------------------------- # Refresh the content of the favorites menu according to the defined favorites - def __gui_refresh_favorites_menu( self ) : - for current in self.__fav_menu_items : + def __gui_refresh_favorites_menu(self): + for current in self.__fav_menu_items: current.destroy() self.__fav_menu_items = list() - items = self.__favorites.keys() + items = list(self.__favorites.keys()) items.sort() - for current in items : - menu_item = gtk.MenuItem( current ) + for current in items: + menu_item = gtk.MenuItem(current) menu_item.show() - self.__fav_menu_items.append( menu_item ) - self.__widgets["menu_favorites"].append( menu_item ) + self.__fav_menu_items.append(menu_item) + self.__widgets["menu_favorites"].append(menu_item) - menu_item.connect_object( "activate", self.__gui_load_favorite, current ) + menu_item.connect_object("activate", self.__gui_load_favorite, current) - if len( items ) > 0 : - self.__widgets["menu_favorites_del"].set_sensitive( True ) - else : - self.__widgets["menu_favorites_del"].set_sensitive( False ) + if len(items) > 0: + self.__widgets["menu_favorites_del"].set_sensitive(True) + else: + self.__widgets["menu_favorites_del"].set_sensitive(False) - #------------------------------------------------------------------- + # ------------------------------------------------------------------- # In 'add favorites' dialog, this method compares the content of the # text widget representing the name of the new favorite with existing # ones. If they match, the 'add' button will be set to non sensitive # to avoid creating entries with the same name. - def __gui_add_favorite_check_gui_fields( self, widget=None ) : + def __gui_add_favorite_check_gui_fields(self, widget=None): fav_name = widget.get_text() - if ( len( fav_name ) > 0 ) and ( fav_name not in self.__favorites.keys() ) : - self.__widgets["favorites_dialog_button_add"].set_sensitive( True ) - else : - self.__widgets["favorites_dialog_button_add"].set_sensitive( False ) + if (len(fav_name) > 0) and (fav_name not in self.__favorites.keys()): + self.__widgets["favorites_dialog_button_add"].set_sensitive(True) + else: + self.__widgets["favorites_dialog_button_add"].set_sensitive(False) - #------------------------------------------------------------------- + # ------------------------------------------------------------------- # Load and parse favorites - def __parse_favorites( self ) : + def __parse_favorites(self): - if ( not os.path.exists( self.__favorites_file ) ) : + if not os.path.exists(self.__favorites_file): # There is no favorites files, do nothing return - try : - if ( not stat.S_IMODE( os.stat( self.__favorites_path ).st_mode ) == self.DESIRED_FAVORITES_DIRECTORY_MODE ) : # unsafe pre-1.2 directory found - os.chmod( self.__favorites_path, self.DESIRED_FAVORITES_DIRECTORY_MODE ) + try: + if not stat.S_IMODE(os.stat(self.__favorites_path).st_mode) == self.DESIRED_FAVORITES_DIRECTORY_MODE: # unsafe pre-1.2 directory found + os.chmod(self.__favorites_path, self.DESIRED_FAVORITES_DIRECTORY_MODE) - conf = ConfigParser.ConfigParser() - conf.read( self.__favorites_file ) - for current in conf.sections() : + conf = ConfigParser() + conf.read(self.__favorites_file) + for current in conf.sections(): # Check if mandatory fields are present - if ( conf.has_option( current, "host" ) and conf.has_option( current, "ups" ) ) : + if conf.has_option(current, "host") and conf.has_option(current, "ups"): # Valid entry found, add it to the list fav_data = {} - fav_data["host"] = conf.get( current, "host" ) - fav_data["ups"] = conf.get( current, "ups" ) + fav_data["host"] = conf.get(current, "host") + fav_data["ups"] = conf.get(current, "ups") - if ( conf.has_option( current, "port" ) ) : - fav_data["port"] = conf.get( current, "port" ) - else : + if conf.has_option(current, "port"): + fav_data["port"] = conf.get(current, "port") + else: fav_data["port"] = "3493" # If auth is defined the section must have login and pass defined - if ( conf.has_option( current, "auth" ) ) : - if( conf.has_option( current, "login" ) and conf.has_option( current, "password" ) ) : + if conf.has_option(current, "auth"): + if conf.has_option(current, "login") and conf.has_option(current, "password"): # Add the entry - fav_data["auth"] = conf.getboolean( current, "auth" ) - fav_data["login"] = conf.get( current, "login" ) + fav_data["auth"] = conf.getboolean(current, "auth") + fav_data["login"] = conf.get(current, "login") - try : - fav_data["password"] = base64.decodestring( conf.get( current, "password" ) ) + try: + fav_data["password"] = base64.decodestring(conf.get(current, "password")) - except : + except: # If the password is not in base64, let the field empty - print( _("Error parsing favorites, password for '%s' is not in base64\nSkipping password for this entry") % current ) + print(_("Error parsing favorites, password for '%s' is not in base64\nSkipping password for this entry") % current) fav_data["password"] = "" - else : + else: fav_data["auth"] = False self.__favorites[current] = fav_data self.__gui_refresh_favorites_menu() - except : - self.gui_status_message( _("Error while parsing favorites file (%s)") % sys.exc_info()[1] ) + except: + self.gui_status_message(_("Error while parsing favorites file (%s)") % sys.exc_info()[1]) - #------------------------------------------------------------------- + # ------------------------------------------------------------------- # Save favorites to the defined favorites file using ini format - def __save_favorites( self ) : + def __save_favorites(self): # If path does not exists, try to create it - if ( not os.path.exists( self.__favorites_file ) ) : - try : - os.makedirs( self.__favorites_path, mode=self.DESIRED_FAVORITES_DIRECTORY_MODE ) - except : - self.gui_status_message( _("Error while creating configuration folder (%s)") % sys.exc_info()[1] ) - - save_conf = ConfigParser.ConfigParser() - for current in self.__favorites.keys() : - save_conf.add_section( current ) - for k, v in self.__favorites[ current ].iteritems() : - save_conf.set( current, k, v ) - - try : - fh = open( self.__favorites_file, "w" ) - save_conf.write( fh ) + if not os.path.exists(self.__favorites_file): + try: + os.makedirs(self.__favorites_path, mode=self.DESIRED_FAVORITES_DIRECTORY_MODE) + except: + self.gui_status_message(_("Error while creating configuration folder (%s)") % sys.exc_info()[1]) + + save_conf = ConfigParser() + for current in self.__favorites.keys(): + save_conf.add_section(current) + for k, v in self.__favorites[current].items(): + save_conf.set(current, k, v) + + try: + fh = open(self.__favorites_file, "w") + save_conf.write(fh) fh.close() - self.gui_status_message( _("Saved favorites...") ) + self.gui_status_message(_("Saved favorites...")) - except : - self.gui_status_message( _("Error while saving favorites (%s)") % sys.exc_info()[1] ) + except: + self.gui_status_message(_("Error while saving favorites (%s)") % sys.exc_info()[1]) - #------------------------------------------------------------------- + # ------------------------------------------------------------------- # Display the about dialog - def gui_about_dialog( self, widget=None ) : - dialog_interface = gtk.glade.XML( self.__glade_file, "aboutdialog1" ) - dialog = dialog_interface.get_widget( "aboutdialog1" ) + def gui_about_dialog(self, widget=None): + dialog_interface = gtk.glade.XML(self.__glade_file, "aboutdialog1") + dialog = dialog_interface.get_widget("aboutdialog1") - self.__widgets["main_window"].set_sensitive( False ) + self.__widgets["main_window"].set_sensitive(False) dialog.run() dialog.destroy() - self.__widgets["main_window"].set_sensitive( True ) + self.__widgets["main_window"].set_sensitive(True) - #------------------------------------------------------------------- + # ------------------------------------------------------------------- # Display a message on the status bar. The message is also set as # tooltip to enable users to see long messages. - def gui_status_message( self, msg="" ) : + def gui_status_message(self, msg=""): context_id = self.__widgets["status_bar"].get_context_id("Infos") - self.__widgets["status_bar"].pop( context_id ) + self.__widgets["status_bar"].pop(context_id) - if ( platform.system() == "Windows" ) : + if platform.system() == "Windows": text = msg.decode("cp1250").encode("utf8") - else : + else: text = msg - message_id = self.__widgets["status_bar"].push( context_id, text.replace("\n", "") ) - self.__widgets["status_bar"].set_tooltip_text( text ) + self.__widgets["status_bar"].push(context_id, text.replace("\n", "")) + self.__widgets["status_bar"].set_tooltip_text(text) - #------------------------------------------------------------------- + # ------------------------------------------------------------------- # Display a notification using PyNotify with an optional icon - def gui_status_notification( self, message="", icon_file="" ) : + def gui_status_notification(self, message="", icon_file=""): # Try to init pynotify - try : + try: import pynotify - pynotify.init( "NUT Monitor" ) + pynotify.init("NUT Monitor") - if ( icon_file != "" ) : - icon = "file://%s" % os.path.abspath( os.path.join( os.path.dirname( sys.argv[0] ), "pixmaps", icon_file ) ) - else : + if icon_file != "": + icon = "file://%s" % os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), "pixmaps", icon_file)) + else: icon = None - notif = pynotify.Notification( "NUT Monitor", message, icon ) + notif = pynotify.Notification("NUT Monitor", message, icon) notif.show() - except : + except: pass - #------------------------------------------------------------------- + # ------------------------------------------------------------------- # Let GTK refresh GUI :) - def refresh_gui( self ) : - while gtk.events_pending() : - gtk.main_iteration( False ) - return( True ) + def refresh_gui(self): + while gtk.events_pending(): + gtk.main_iteration(False) + return True - #------------------------------------------------------------------- + # ------------------------------------------------------------------- # Connect to the selected UPS using parameters (host,port,login,pass) - def connect_to_ups( self, widget=None ) : + def connect_to_ups(self, widget=None): - host = self.__widgets["ups_host_entry"].get_text() - port = int( self.__widgets["ups_port_entry"].get_value() ) - login = None + host = self.__widgets["ups_host_entry"].get_text() + port = int(self.__widgets["ups_port_entry"].get_value()) + login = None password = None - if self.__widgets["ups_authentication_check"].get_active() : - login = self.__widgets["ups_authentication_login"].get_text() + if self.__widgets["ups_authentication_check"].get_active(): + login = self.__widgets["ups_authentication_login"].get_text() password = self.__widgets["ups_authentication_password"].get_text() - try : - self.__ups_handler = PyNUT.PyNUTClient( host=host, port=port, login=login, password=password ) + try: + self.__ups_handler = PyNUT.PyNUTClient(host=host, port=port, login=login, password=password) - except : - self.gui_status_message( _("Error connecting to '{0}' ({1})").format( host, sys.exc_info()[1] ) ) - self.gui_status_notification( _("Error connecting to '{0}'\n{1}").format( host, sys.exc_info()[1] ), "warning.png" ) + except: + self.gui_status_message(_("Error connecting to '{0}' ({1})").format(host, sys.exc_info()[1])) + self.gui_status_notification(_("Error connecting to '{0}'\n{1}").format(host, sys.exc_info()[1]), "warning.png") return # Check if selected UPS exists on server... - srv_upses = self.__ups_handler.GetUPSList() + srv_upses = self.__ups_handler.GetUPSList() self.__current_ups = self.__widgets["ups_list_combo"].get_active_text() - if not srv_upses.has_key( self.__current_ups ) : - self.gui_status_message( _("Device '%s' not found on server") % self.__current_ups ) - self.gui_status_notification( _("Device '%s' not found on server") % self.__current_ups, "warning.png" ) + if self.__current_ups not in srv_upses.keys(): + self.gui_status_message(_("Device '%s' not found on server") % self.__current_ups) + self.gui_status_notification(_("Device '%s' not found on server") % self.__current_ups, "warning.png") return self.__connected = True self.__widgets["ups_connect"].hide() self.__widgets["ups_disconnect"].show() self.__widgets["ups_infos"].show() - self.__widgets["ups_params_box"].set_sensitive( False ) - self.__widgets["menu_favorites_root"].set_sensitive( False ) + self.__widgets["ups_params_box"].set_sensitive(False) + self.__widgets["menu_favorites_root"].set_sensitive(False) self.__widgets["ups_params_box"].hide() - commands = self.__ups_handler.GetUPSCommands( self.__current_ups ) + commands = self.__ups_handler.GetUPSCommands(self.__current_ups) self.__ups_commands = commands.keys() self.__ups_commands.sort() # Refresh UPS commands combo box self.__widgets["ups_commands_combo_store"].clear() - for desc in self.__ups_commands : - self.__widgets["ups_commands_combo_store"].append( [ "%s\n%s" % ( desc, commands[desc] ) ] ) + for desc in self.__ups_commands: + self.__widgets["ups_commands_combo_store"].append(["%s\n%s" % (desc, commands[desc])]) - self.__widgets["ups_commands_combo"].set_active( 0 ) + self.__widgets["ups_commands_combo"].set_active(0) # Update UPS vars manually before the thread - self.__ups_vars = self.__ups_handler.GetUPSVars( self.__current_ups ) - self.__ups_rw_vars = self.__ups_handler.GetRWVars( self.__current_ups ) + self.__ups_vars = self.__ups_handler.GetUPSVars(self.__current_ups) + self.__ups_rw_vars = self.__ups_handler.GetRWVars(self.__current_ups) self.__gui_update_ups_vars_view() # Try to resize the main window... - self.__widgets["main_window"].resize( 1, 1 ) + self.__widgets["main_window"].resize(1, 1) # Start the GUI updater thread - self.__gui_thread = gui_updater( self ) + self.__gui_thread = gui_updater(self) self.__gui_thread.start() - self.gui_status_message( _("Connected to '{0}' on {1}").format( self.__current_ups, host ) ) + self.gui_status_message(_("Connected to '{0}' on {1}").format(self.__current_ups, host)) - - #------------------------------------------------------------------- + # ------------------------------------------------------------------- # Refresh UPS vars in the treeview - def __gui_update_ups_vars_view( self, widget=None ) : - if self.__ups_handler : - vars = self.__ups_vars + def __gui_update_ups_vars_view(self, widget=None): + if self.__ups_handler: + vars_ = self.__ups_vars rwvars = self.__ups_rw_vars self.__widgets["ups_vars_tree_store"].clear() - for k,v in vars.iteritems() : - if ( rwvars.has_key( k ) ) : - icon_file = os.path.join( os.path.dirname( sys.argv[0] ), "pixmaps", "var-rw.png" ) - else : - icon_file = os.path.join( os.path.dirname( sys.argv[0] ), "pixmaps", "var-ro.png" ) - - icon = gtk.gdk.pixbuf_new_from_file( icon_file ) - self.__widgets["ups_vars_tree_store"].append( [ icon, k, v ] ) + for k, v in vars_.items(): + if k in rwvars.keys(): + icon_file = os.path.join(os.path.dirname(sys.argv[0]), "pixmaps", "var-rw.png") + else: + icon_file = os.path.join(os.path.dirname(sys.argv[0]), "pixmaps", "var-ro.png") + icon = gtk.gdk.pixbuf_new_from_file(icon_file) + self.__widgets["ups_vars_tree_store"].append([icon, k, v]) - #------------------------------------------------------------------- + # ------------------------------------------------------------------- # Disconnect from the UPS - def disconnect_from_ups( self, widget=None ) : + def disconnect_from_ups(self, widget=None): self.__connected = False self.__widgets["ups_connect"].show() self.__widgets["ups_disconnect"].hide() self.__widgets["ups_infos"].hide() - self.__widgets["ups_params_box"].set_sensitive( True ) - self.__widgets["menu_favorites_root"].set_sensitive( True ) - self.__widgets["status_icon"].set_tooltip_markup( _("Not connected") ) + self.__widgets["ups_params_box"].set_sensitive(True) + self.__widgets["menu_favorites_root"].set_sensitive(True) + self.__widgets["status_icon"].set_tooltip_markup(_("Not connected")) self.__widgets["ups_params_box"].show() # Try to resize the main window... - self.__widgets["main_window"].resize( 1, 1 ) + self.__widgets["main_window"].resize(1, 1) # Stop the GUI updater thread self.__gui_thread.stop_thread() del self.__ups_handler - self.gui_status_message( _("Disconnected from '%s'") % self.__current_ups ) - self.change_status_icon( "on_line", blink=False ) + self.gui_status_message(_("Disconnected from '%s'") % self.__current_ups) + self.change_status_icon("on_line", blink=False) self.__current_ups = None -#----------------------------------------------------------------------- + +# ----------------------------------------------------------------------- # GUI Updater class # This class updates the main gui with data from connected UPS -class gui_updater( threading.Thread ) : - +class gui_updater(threading.Thread): __parent_class = None - __stop_thread = False + __stop_thread = False - def __init__( self, parent_class ) : - threading.Thread.__init__( self ) + def __init__(self, parent_class): + threading.Thread.__init__(self) self.__parent_class = parent_class - def run( self ) : + def run(self): - ups = self.__parent_class._interface__current_ups + ups = self.__parent_class._interface__current_ups was_online = True # Define a dict containing different UPS status - status_mapper = { "LB" : "%s" % _("Low batteries"), - "RB" : "%s" % _("Replace batteries !"), - "BYPASS" : "Bypass %s" % _("(no battery protection)"), - "CAL" : _("Performing runtime calibration"), - "OFF" : "%s (%s)" % ( _("Offline"), _("not providing power to the load") ), - "OVER" : "%s (%s)" % ( _("Overloaded !"), _("there is too much load for device") ), - "TRIM" : _("Triming (UPS is triming incoming voltage)"), - "BOOST" : _("Boost (UPS is boosting incoming voltage)") - } - - while not self.__stop_thread : - try : - vars = self.__parent_class._interface__ups_handler.GetUPSVars( ups ) - self.__parent_class._interface__ups_vars = vars + status_mapper = {"LB": "%s" % _("Low batteries"), + "RB": "%s" % _("Replace batteries !"), + "BYPASS": "Bypass %s" % _("(no battery protection)"), + "CAL": _("Performing runtime calibration"), + "OFF": "%s (%s)" % (_("Offline"), _("not providing power to the load")), + "OVER": "%s (%s)" % (_("Overloaded !"), _("there is too much load for device")), + "TRIM": _("Triming (UPS is triming incoming voltage)"), + "BOOST": _("Boost (UPS is boosting incoming voltage)") + } + + while not self.__stop_thread: + try: + vars_ = self.__parent_class._interface__ups_handler.GetUPSVars(ups) + self.__parent_class._interface__ups_vars = vars_ # Text displayed on the status frame - text_left = "" - text_right = "" + text_left = "" + text_right = "" status_text = "" - text_left += "%s\n" % _("Device status :") + text_left += "%s\n" % _("Device status :") - if ( vars.get("ups.status").find("OL") != -1 ) : + if vars_.get("ups.status").find("OL") != -1: text_right += "%s" % _("Online") - if not was_online : - self.__parent_class.change_status_icon( "on_line", blink=False ) + if not was_online: + self.__parent_class.change_status_icon("on_line", blink=False) was_online = True - if ( vars.get("ups.status").find("OB") != -1 ) : + if vars_.get("ups.status").find("OB") != -1: text_right += "%s" % _("On batteries") - if was_online : - self.__parent_class.change_status_icon( "on_battery", blink=True ) - self.__parent_class.gui_status_notification( _("Device is running on batteries"), "on_battery.png" ) + if was_online: + self.__parent_class.change_status_icon("on_battery", blink=True) + self.__parent_class.gui_status_notification(_("Device is running on batteries"), "on_battery.png") was_online = False - # Check for additionnal information - for k,v in status_mapper.iteritems() : - if vars.get("ups.status").find(k) != -1 : - if ( text_right != "" ) : + # Check for additional information + for k, v in status_mapper.items(): + if vars_.get("ups.status").find(k) != -1: + if text_right != "": text_right += " - %s" % v - else : + else: text_right += "%s" % v # CHRG and DISCHRG cannot be trated with the previous loop ;) - if ( vars.get("ups.status").find("DISCHRG") != -1 ) : + if vars_.get("ups.status").find("DISCHRG") != -1: text_right += " - %s" % _("discharging") - elif ( vars.get("ups.status").find("CHRG") != -1 ) : + elif vars_.get("ups.status").find("CHRG") != -1: text_right += " - %s" % _("charging") status_text += text_right text_right += "\n" - if ( vars.has_key( "ups.mfr" ) ) : - text_left += "%s\n\n" % _("Model :") - text_right += "%s\n%s\n" % ( vars.get("ups.mfr",""), vars.get("ups.model","") ) + if "ups.mfr" in vars_.keys(): + text_left += "%s\n\n" % _("Model :") + text_right += "%s\n%s\n" % (vars_.get("ups.mfr", ""), vars_.get("ups.model", "")) - if ( vars.has_key( "ups.temperature" ) ) : - text_left += "%s\n" % _("Temperature :") - text_right += "%s\n" % int( float( vars.get( "ups.temperature", 0 ) ) ) + if "ups.temperature" in vars_.keys(): + text_left += "%s\n" % _("Temperature :") + text_right += "%s\n" % int(float(vars_.get("ups.temperature", 0))) - if ( vars.has_key( "battery.voltage" ) ) : - text_left += "%s\n" % _("Battery voltage :") - text_right += "%sv\n" % vars.get( "battery.voltage", 0 ) + if "battery.voltage" in vars_.keys(): + text_left += "%s\n" % _("Battery voltage :") + text_right += "%sv\n" % vars_.get("battery.voltage", 0) - self.__parent_class._interface__widgets["ups_status_left"].set_markup( text_left[:-1] ) - self.__parent_class._interface__widgets["ups_status_right"].set_markup( text_right[:-1] ) + self.__parent_class._interface__widgets["ups_status_left"].set_markup(text_left[:-1]) + self.__parent_class._interface__widgets["ups_status_right"].set_markup(text_right[:-1]) # UPS load and battery charge progress bars - if ( vars.has_key( "battery.charge" ) ) : - charge = vars.get( "battery.charge", "0" ) - self.__parent_class._interface__widgets["progress_battery_charge"].set_fraction( float( charge ) / 100.0 ) - self.__parent_class._interface__widgets["progress_battery_charge"].set_text( "%s %%" % int( float( charge ) ) ) - status_text += "\n%s %s%%" % ( _("Battery charge :"), int( float( charge ) ) ) - else : - self.__parent_class._interface__widgets["progress_battery_charge"].set_fraction( 0.0 ) - self.__parent_class._interface__widgets["progress_battery_charge"].set_text( _("Not available") ) - - if ( vars.has_key( "ups.load" ) ) : - load = vars.get( "ups.load", "0" ) - self.__parent_class._interface__widgets["progress_battery_load"].set_fraction( float( load ) / 100.0 ) - self.__parent_class._interface__widgets["progress_battery_load"].set_text( "%s %%" % int( float( load ) ) ) - status_text += "\n%s %s%%" % ( _("UPS load :"), int( float( load ) ) ) - else : - self.__parent_class._interface__widgets["progress_battery_load"].set_fraction( 0.0 ) - self.__parent_class._interface__widgets["progress_battery_load"].set_text( _("Not available") ) - - if ( vars.has_key( "battery.runtime" ) ) : - autonomy = int( float( vars.get( "battery.runtime", 0 ) ) ) - - if ( autonomy >= 3600 ) : - info = time.strftime( _("%H hours %M minutes %S seconds"), time.gmtime( autonomy ) ) - elif ( autonomy > 300 ) : - info = time.strftime( _("%M minutes %S seconds"), time.gmtime( autonomy ) ) - else : - info = time.strftime( _("%M minutes %S seconds"), time.gmtime( autonomy ) ) - else : + if "battery.charge" in vars_.keys(): + charge = vars_.get("battery.charge", "0") + self.__parent_class._interface__widgets["progress_battery_charge"].set_fraction(float(charge) / 100.0) + self.__parent_class._interface__widgets["progress_battery_charge"].set_text("%s %%" % int(float(charge))) + status_text += "\n%s %s%%" % (_("Battery charge :"), int(float(charge))) + else: + self.__parent_class._interface__widgets["progress_battery_charge"].set_fraction(0.0) + self.__parent_class._interface__widgets["progress_battery_charge"].set_text(_("Not available")) + + if "ups.load" in vars_.keys(): + load = vars_.get("ups.load", "0") + self.__parent_class._interface__widgets["progress_battery_load"].set_fraction(float(load) / 100.0) + self.__parent_class._interface__widgets["progress_battery_load"].set_text("%s %%" % int(float(load))) + status_text += "\n%s %s%%" % (_("UPS load :"), int(float(load))) + else: + self.__parent_class._interface__widgets["progress_battery_load"].set_fraction(0.0) + self.__parent_class._interface__widgets["progress_battery_load"].set_text(_("Not available")) + + if "battery.runtime" in vars_.keys(): + autonomy = int(float(vars_.get("battery.runtime", 0))) + + if autonomy >= 3600: + info = time.strftime(_("%H hours %M minutes %S seconds"), time.gmtime(autonomy)) + elif autonomy > 300: + info = time.strftime(_("%M minutes %S seconds"), time.gmtime(autonomy)) + else: + info = time.strftime(_("%M minutes %S seconds"), time.gmtime(autonomy)) + else: info = _("Not available") - self.__parent_class._interface__widgets["ups_status_time"].set_markup( info ) + self.__parent_class._interface__widgets["ups_status_time"].set_markup(info) # Display UPS status as tooltip for tray icon - self.__parent_class._interface__widgets["status_icon"].set_tooltip_markup( status_text ) + self.__parent_class._interface__widgets["status_icon"].set_tooltip_markup(status_text) - except : - self.__parent_class.gui_status_message( _("Error from '{0}' ({1})").format( ups, sys.exc_info()[1] ) ) - self.__parent_class.gui_status_notification( _("Error from '{0}'\n{1}").format( ups, sys.exc_info()[1] ), "warning.png" ) + except: + self.__parent_class.gui_status_message(_("Error from '{0}' ({1})").format(ups, sys.exc_info()[1])) + self.__parent_class.gui_status_notification(_("Error from '{0}'\n{1}").format(ups, sys.exc_info()[1]), "warning.png") - time.sleep( 1 ) + time.sleep(1) - def stop_thread( self ) : + def stop_thread(self): self.__stop_thread = True -#----------------------------------------------------------------------- +# ----------------------------------------------------------------------- # The main program starts here :-) -if __name__ == "__main__" : +if __name__ == "__main__": # Init the localisation APP = "NUT-Monitor" DIR = "locale" - gettext.bindtextdomain( APP, DIR ) - gettext.textdomain( APP ) + gettext.bindtextdomain(APP, DIR) + gettext.textdomain(APP) _ = gettext.gettext - for module in ( gettext, gtk.glade ) : - module.bindtextdomain( APP, DIR ) - module.textdomain( APP ) + for module in (gettext, gtk.glade): + module.bindtextdomain(APP, DIR) + module.textdomain(APP) gui = interface() gtk.main() - diff --git a/scripts/python/module/PyNUT.py b/scripts/python/module/PyNUT.py index f9d7706932..517b745c24 100644 --- a/scripts/python/module/PyNUT.py +++ b/scripts/python/module/PyNUT.py @@ -41,26 +41,26 @@ import telnetlib -class PyNUTError( Exception ) : + +class PyNUTError(Exception): """ Base class for custom exceptions """ -class PyNUTClient : +class PyNUTClient: """ Abstraction class to access NUT (Network UPS Tools) server """ - __debug = None # Set class to debug mode (prints everything useful for debuging...) - __host = None - __port = None - __login = None - __password = None - __timeout = None + __debug = None # Set class to debug mode (prints everything useful for debuging...) + __host = None + __port = None + __login = None + __password = None + __timeout = None __srv_handler = None - __version = "1.3.0" - __release = "2014-06-03" - + __version = "1.3.0" + __release = "2014-06-03" - def __init__( self, host="127.0.0.1", port=3493, login=None, password=None, debug=False, timeout=5 ) : + def __init__(self, host="127.0.0.1", port=3493, login=None, password=None, debug=False, timeout=5): """ Class initialization method host : Host to connect (default to localhost) @@ -72,197 +72,197 @@ def __init__( self, host="127.0.0.1", port=3493, login=None, password=None, debu """ self.__debug = debug - if self.__debug : - print( "[DEBUG] Class initialization..." ) - print( "[DEBUG] -> Host = %s (port %s)" % ( host, port ) ) - print( "[DEBUG] -> Login = '%s' / '%s'" % ( login, password ) ) + if self.__debug: + print("[DEBUG] Class initialization...") + print("[DEBUG] -> Host = %s (port %s)" % (host, port)) + print("[DEBUG] -> Login = '%s' / '%s'" % (login, password)) - self.__host = host - self.__port = port - self.__login = login + self.__host = host + self.__port = port + self.__login = login self.__password = password - self.__timeout = 5 + self.__timeout = 5 self.__connect() # Try to disconnect cleanly when class is deleted ;) - def __del__( self ) : + def __del__(self): """ Class destructor method """ - try : - self.__srv_handler.write( "LOGOUT\n" ) - except : + try: + self.__srv_handler.write("LOGOUT\n") + except: pass - def __connect( self ) : + def __connect(self): """ Connects to the defined server If login/pass was specified, the class tries to authenticate. An error is raised if something goes wrong. """ - if self.__debug : - print( "[DEBUG] Connecting to host" ) + if self.__debug: + print("[DEBUG] Connecting to host") - self.__srv_handler = telnetlib.Telnet( self.__host, self.__port ) + self.__srv_handler = telnetlib.Telnet(self.__host, self.__port) - if self.__login != None : - self.__srv_handler.write( "USERNAME %s\n" % self.__login ) - result = self.__srv_handler.read_until( "\n", self.__timeout ) - if result[:2] != "OK" : - raise PyNUTError( result.replace( "\n", "" ) ) + if self.__login is not None: + self.__srv_handler.write("USERNAME %s\n" % self.__login) + result = self.__srv_handler.read_until("\n", self.__timeout) + if result[:2] != "OK": + raise PyNUTError(result.replace("\n", "")) - if self.__password != None : - self.__srv_handler.write( "PASSWORD %s\n" % self.__password ) - result = self.__srv_handler.read_until( "\n", self.__timeout ) - if result[:2] != "OK" : - raise PyNUTError( result.replace( "\n", "" ) ) + if self.__password is not None: + self.__srv_handler.write("PASSWORD %s\n" % self.__password) + result = self.__srv_handler.read_until("\n", self.__timeout) + if result[:2] != "OK": + raise PyNUTError(result.replace("\n", "")) - def GetUPSList( self ) : + def GetUPSList(self): """ Returns the list of available UPS from the NUT server The result is a dictionary containing 'key->val' pairs of 'UPSName' and 'UPS Description' """ - if self.__debug : - print( "[DEBUG] GetUPSList from server" ) + if self.__debug: + print("[DEBUG] GetUPSList from server") - self.__srv_handler.write( "LIST UPS\n" ) - result = self.__srv_handler.read_until( "\n" ) - if result != "BEGIN LIST UPS\n" : - raise PyNUTError( result.replace( "\n", "" ) ) + self.__srv_handler.write("LIST UPS\n") + result = self.__srv_handler.read_until("\n") + if result != "BEGIN LIST UPS\n": + raise PyNUTError(result.replace("\n", "")) - result = self.__srv_handler.read_until( "END LIST UPS\n" ) + result = self.__srv_handler.read_until("END LIST UPS\n") ups_list = {} - for line in result.split( "\n" ) : - if line[:3] == "UPS" : - ups, desc = line[4:-1].split( '"' ) - ups_list[ ups.replace( " ", "" ) ] = desc + for line in result.split("\n"): + if line[:3] == "UPS": + ups, desc = line[4:-1].split('"') + ups_list[ups.replace(" ", "")] = desc - return( ups_list ) + return ups_list - def GetUPSVars( self, ups="" ) : + def GetUPSVars(self, ups=""): """ Get all available vars from the specified UPS The result is a dictionary containing 'key->val' pairs of all available vars. """ - if self.__debug : - print( "[DEBUG] GetUPSVars called..." ) + if self.__debug: + print("[DEBUG] GetUPSVars called...") - self.__srv_handler.write( "LIST VAR %s\n" % ups ) - result = self.__srv_handler.read_until( "\n" ) - if result != "BEGIN LIST VAR %s\n" % ups : - raise PyNUTError( result.replace( "\n", "" ) ) + self.__srv_handler.write("LIST VAR %s\n" % ups) + result = self.__srv_handler.read_until("\n") + if result != "BEGIN LIST VAR %s\n" % ups: + raise PyNUTError(result.replace("\n", "")) - ups_vars = {} - result = self.__srv_handler.read_until( "END LIST VAR %s\n" % ups ) - offset = len( "VAR %s " % ups ) - end_offset = 0 - ( len( "END LIST VAR %s\n" % ups ) + 1 ) + ups_vars = {} + result = self.__srv_handler.read_until("END LIST VAR %s\n" % ups) + offset = len("VAR %s " % ups) + end_offset = 0 - (len("END LIST VAR %s\n" % ups) + 1) - for current in result[:end_offset].split( "\n" ) : - var = current[ offset: ].split( '"' )[0].replace( " ", "" ) - data = current[ offset: ].split( '"' )[1] - ups_vars[ var ] = data + for current in result[:end_offset].split("\n"): + var = current[offset:].split('"')[0].replace(" ", "") + data = current[offset:].split('"')[1] + ups_vars[var] = data - return( ups_vars ) + return ups_vars - def GetUPSCommands( self, ups="" ) : + def GetUPSCommands(self, ups=""): """ Get all available commands for the specified UPS The result is a dict object with command name as key and a description of the command as value """ - if self.__debug : - print( "[DEBUG] GetUPSCommands called..." ) + if self.__debug: + print("[DEBUG] GetUPSCommands called...") - self.__srv_handler.write( "LIST CMD %s\n" % ups ) - result = self.__srv_handler.read_until( "\n" ) - if result != "BEGIN LIST CMD %s\n" % ups : - raise PyNUTError( result.replace( "\n", "" ) ) + self.__srv_handler.write("LIST CMD %s\n" % ups) + result = self.__srv_handler.read_until("\n") + if result != "BEGIN LIST CMD %s\n" % ups: + raise PyNUTError(result.replace("\n", "")) - ups_cmds = {} - result = self.__srv_handler.read_until( "END LIST CMD %s\n" % ups ) - offset = len( "CMD %s " % ups ) - end_offset = 0 - ( len( "END LIST CMD %s\n" % ups ) + 1 ) + ups_cmds = {} + result = self.__srv_handler.read_until("END LIST CMD %s\n" % ups) + offset = len("CMD %s " % ups) + end_offset = 0 - (len("END LIST CMD %s\n" % ups) + 1) - for current in result[:end_offset].split( "\n" ) : - var = current[ offset: ].split( '"' )[0].replace( " ", "" ) + for current in result[:end_offset].split("\n"): + var = current[offset:].split('"')[0].replace(" ", "") # For each var we try to get the available description - try : - self.__srv_handler.write( "GET CMDDESC %s %s\n" % ( ups, var ) ) - temp = self.__srv_handler.read_until( "\n" ) - if temp[:7] != "CMDDESC" : + try: + self.__srv_handler.write("GET CMDDESC %s %s\n" % (ups, var)) + temp = self.__srv_handler.read_until("\n") + if temp[:7] != "CMDDESC": raise PyNUTError - else : - off = len( "CMDDESC %s %s " % ( ups, var ) ) + else: + off = len("CMDDESC %s %s " % (ups, var)) desc = temp[off:-1].split('"')[1] - except : + except: desc = var - ups_cmds[ var ] = desc + ups_cmds[var] = desc - return( ups_cmds ) + return ups_cmds - def GetRWVars( self, ups="" ) : + def GetRWVars(self, ups=""): """ Get a list of all writable vars from the selected UPS The result is presented as a dictionary containing 'key->val' pairs """ - if self.__debug : - print( "[DEBUG] GetUPSVars from '%s'..." % ups ) - - self.__srv_handler.write( "LIST RW %s\n" % ups ) - result = self.__srv_handler.read_until( "\n" ) - if ( result != "BEGIN LIST RW %s\n" % ups ) : - raise PyNUTError( result.replace( "\n", "" ) ) - - result = self.__srv_handler.read_until( "END LIST RW %s\n" % ups ) - offset = len( "VAR %s" % ups ) - end_offset = 0 - ( len( "END LIST RW %s\n" % ups ) + 1 ) - rw_vars = {} - - try : - for current in result[:end_offset].split( "\n" ) : - var = current[ offset: ].split( '"' )[0].replace( " ", "" ) - data = current[ offset: ].split( '"' )[1] - rw_vars[ var ] = data - - except : + if self.__debug: + print("[DEBUG] GetUPSVars from '%s'..." % ups) + + self.__srv_handler.write("LIST RW %s\n" % ups) + result = self.__srv_handler.read_until("\n") + if result != "BEGIN LIST RW %s\n" % ups: + raise PyNUTError(result.replace("\n", "")) + + result = self.__srv_handler.read_until("END LIST RW %s\n" % ups) + offset = len("VAR %s" % ups) + end_offset = 0 - (len("END LIST RW %s\n" % ups) + 1) + rw_vars = {} + + try: + for current in result[:end_offset].split("\n"): + var = current[offset:].split('"')[0].replace(" ", "") + data = current[offset:].split('"')[1] + rw_vars[var] = data + + except: pass - return( rw_vars ) + return rw_vars - def SetRWVar( self, ups="", var="", value="" ): + def SetRWVar(self, ups="", var="", value=""): """ Set a variable to the specified value on selected UPS The variable must be a writable value (cf GetRWVars) and you must have the proper rights to set it (maybe login/password). """ - self.__srv_handler.write( "SET VAR %s %s %s\n" % ( ups, var, value ) ) - result = self.__srv_handler.read_until( "\n" ) - if ( result == "OK\n" ) : - return( "OK" ) - else : - raise PyNUTError( result ) + self.__srv_handler.write("SET VAR %s %s %s\n" % (ups, var, value)) + result = self.__srv_handler.read_until("\n") + if result == "OK\n": + return "OK" + else: + raise PyNUTError(result) - def RunUPSCommand( self, ups="", command="" ) : + def RunUPSCommand(self, ups="", command=""): """ Send a command to the specified UPS Returns OK on success or raises an error """ - if self.__debug : - print( "[DEBUG] RunUPSCommand called..." ) + if self.__debug: + print("[DEBUG] RunUPSCommand called...") - self.__srv_handler.write( "INSTCMD %s %s\n" % ( ups, command ) ) - result = self.__srv_handler.read_until( "\n" ) - if ( result == "OK\n" ) : - return( "OK" ) - else : - raise PyNUTError( result.replace( "\n", "" ) ) + self.__srv_handler.write("INSTCMD %s %s\n" % (ups, command)) + result = self.__srv_handler.read_until("\n") + if result == "OK\n": + return "OK" + else: + raise PyNUTError(result.replace("\n", "")) - def FSD( self, ups="") : + def FSD(self, ups=""): """ Send FSD command Returns OK on success or raises an error @@ -271,71 +271,71 @@ def FSD( self, ups="") : (and backwards-compatible alias handling) """ - if self.__debug : - print( "[DEBUG] MASTER called..." ) + if self.__debug: + print("[DEBUG] MASTER called...") - self.__srv_handler.write( "MASTER %s\n" % ups ) - result = self.__srv_handler.read_until( "\n" ) - if ( result != "OK MASTER-GRANTED\n" ) : - raise PyNUTError( ( "Master level function are not available", "" ) ) + self.__srv_handler.write("MASTER %s\n" % ups) + result = self.__srv_handler.read_until("\n") + if result != "OK MASTER-GRANTED\n": + raise PyNUTError(("Master level function are not available", "")) - if self.__debug : - print( "[DEBUG] FSD called..." ) - self.__srv_handler.write( "FSD %s\n" % ups ) - result = self.__srv_handler.read_until( "\n" ) - if ( result == "OK FSD-SET\n" ) : - return( "OK" ) - else : - raise PyNUTError( result.replace( "\n", "" ) ) + if self.__debug: + print("[DEBUG] FSD called...") + self.__srv_handler.write("FSD %s\n" % ups) + result = self.__srv_handler.read_until("\n") + if result == "OK FSD-SET\n": + return "OK" + else: + raise PyNUTError(result.replace("\n", "")) - def help(self) : + def help(self): """ Send HELP command """ - if self.__debug : - print( "[DEBUG] HELP called..." ) + if self.__debug: + print("[DEBUG] HELP called...") - self.__srv_handler.write( "HELP\n") - return self.__srv_handler.read_until( "\n" ) + self.__srv_handler.write("HELP\n") + return self.__srv_handler.read_until("\n") - def ver(self) : + def ver(self): """ Send VER command """ - if self.__debug : - print( "[DEBUG] VER called..." ) + if self.__debug: + print("[DEBUG] VER called...") - self.__srv_handler.write( "VER\n") - return self.__srv_handler.read_until( "\n" ) + self.__srv_handler.write("VER\n") + return self.__srv_handler.read_until("\n") - def ListClients( self, ups = None ) : + def ListClients(self, ups=None): """ Returns the list of connected clients from the NUT server The result is a dictionary containing 'key->val' pairs of 'UPSName' and a list of clients """ - if self.__debug : - print( "[DEBUG] ListClients from server" ) + if self.__debug: + print("[DEBUG] ListClients from server") if ups and (ups not in self.GetUPSList()): - raise PyNUTError( "%s is not a valid UPS" % ups ) + raise PyNUTError("%s is not a valid UPS" % ups) if ups: - self.__srv_handler.write( "LIST CLIENTS %s\n" % ups) + self.__srv_handler.write("LIST CLIENTS %s\n" % ups) else: - self.__srv_handler.write( "LIST CLIENTS\n" ) - result = self.__srv_handler.read_until( "\n" ) - if result != "BEGIN LIST CLIENTS\n" : - raise PyNUTError( result.replace( "\n", "" ) ) + self.__srv_handler.write("LIST CLIENTS\n") + result = self.__srv_handler.read_until("\n") + if result != "BEGIN LIST CLIENTS\n": + raise PyNUTError(result.replace("\n", "")) - result = self.__srv_handler.read_until( "END LIST CLIENTS\n" ) + result = self.__srv_handler.read_until("END LIST CLIENTS\n") ups_list = {} - for line in result.split( "\n" ): - if line[:6] == "CLIENT" : + for line in result.split("\n"): + if line[:6] == "CLIENT": host, ups = line[7:].split(' ') ups.replace(' ', '') - if not ups in ups_list: + if ups not in ups_list: ups_list[ups] = [] ups_list[ups].append(host) - return( ups_list ) + return ups_list diff --git a/scripts/python/module/test_nutclient.py b/scripts/python/module/test_nutclient.py index 39993e02b8..2316378871 100755 --- a/scripts/python/module/test_nutclient.py +++ b/scripts/python/module/test_nutclient.py @@ -3,41 +3,42 @@ # This source code is provided for testing/debuging purpose ;) -import PyNUT import sys -if __name__ == "__main__" : +import PyNUT + +if __name__ == "__main__": - print( "PyNUTClient test..." ) - nut = PyNUT.PyNUTClient( debug=True ) - #nut = PyNUT.PyNUTClient( login="upsadmin", password="upsadmin", debug=True ) + print("PyNUTClient test...") + nut = PyNUT.PyNUTClient(debug=True) + # nut = PyNUT.PyNUTClient( login="upsadmin", password="upsadmin", debug=True ) - print( 80*"-" + "\nTesting 'GetUPSList' :") - result = nut.GetUPSList( ) - print( "\033[01;33m%s\033[0m\n" % result ) + print(80 * "-" + "\nTesting 'GetUPSList' :") + result = nut.GetUPSList() + print("\033[01;33m%s\033[0m\n" % result) - print( 80*"-" + "\nTesting 'GetUPSVars' :") - result = nut.GetUPSVars( "dummy" ) - print( "\033[01;33m%s\033[0m\n" % result ) + print(80 * "-" + "\nTesting 'GetUPSVars' :") + result = nut.GetUPSVars("dummy") + print("\033[01;33m%s\033[0m\n" % result) - print( 80*"-" + "\nTesting 'GetUPSCommands' :") - result = nut.GetUPSCommands( "dummy" ) - print( "\033[01;33m%s\033[0m\n" % result ) + print(80 * "-" + "\nTesting 'GetUPSCommands' :") + result = nut.GetUPSCommands("dummy") + print("\033[01;33m%s\033[0m\n" % result) - print( 80*"-" + "\nTesting 'GetRWVars' :") - result = nut.GetRWVars( "dummy" ) - print( "\033[01;33m%s\033[0m\n" % result ) + print(80 * "-" + "\nTesting 'GetRWVars' :") + result = nut.GetRWVars("dummy") + print("\033[01;33m%s\033[0m\n" % result) - print( 80*"-" + "\nTesting 'RunUPSCommand' (Test front panel) :") - try : - result = nut.RunUPSCommand( "UPS1", "test.panel.start" ) - except : + print(80 * "-" + "\nTesting 'RunUPSCommand' (Test front panel) :") + try: + result = nut.RunUPSCommand("UPS1", "test.panel.start") + except: result = sys.exc_info()[1] - print( "\033[01;33m%s\033[0m\n" % result ) + print("\033[01;33m%s\033[0m\n" % result) - print( 80*"-" + "\nTesting 'SetUPSVar' (set ups.id to test):") - try : - result = nut.SetRWVar( "UPS1", "ups.id", "test" ) - except : + print(80 * "-" + "\nTesting 'SetUPSVar' (set ups.id to test):") + try: + result = nut.SetRWVar("UPS1", "ups.id", "test") + except: result = sys.exc_info()[1] - print( "\033[01;33m%s\033[0m\n" % result ) + print("\033[01;33m%s\033[0m\n" % result)