Imported Upstream version 2.7.3

This commit is contained in:
Arnaud Quette 2015-04-30 15:53:36 +02:00
parent a356b56d11
commit fd413a3168
283 changed files with 14978 additions and 6511 deletions
scripts/python/module

View file

@ -35,9 +35,16 @@
# 2012-02-07 René Martín Rodríguez <rmrodri@ull.es> - Version 1.2.2
# Added support for LIST CLIENTS command
#
# 2014-06-03 george2 - Version 1.3.0
# Added custom exception class, fixed minor bug, added Python 3 support.
#
import telnetlib
class PyNUTError( Exception ) :
""" Base class for custom exceptions """
class PyNUTClient :
""" Abstraction class to access NUT (Network UPS Tools) server """
@ -49,8 +56,8 @@ class PyNUTClient :
__timeout = None
__srv_handler = None
__version = "1.2.2"
__release = "2012-02-07"
__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 ) :
@ -101,13 +108,13 @@ if something goes wrong.
self.__srv_handler.write( "USERNAME %s\n" % self.__login )
result = self.__srv_handler.read_until( "\n", self.__timeout )
if result[:2] != "OK" :
raise Exception, result.replace( "\n", "" )
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 Exception, result.replace( "\n", "" )
raise PyNUTError( result.replace( "\n", "" ) )
def GetUPSList( self ) :
""" Returns the list of available UPS from the NUT server
@ -120,7 +127,7 @@ The result is a dictionary containing 'key->val' pairs of 'UPSName' and 'UPS Des
self.__srv_handler.write( "LIST UPS\n" )
result = self.__srv_handler.read_until( "\n" )
if result != "BEGIN LIST UPS\n" :
raise Exception, result.replace( "\n", "" )
raise PyNUTError( result.replace( "\n", "" ) )
result = self.__srv_handler.read_until( "END LIST UPS\n" )
ups_list = {}
@ -144,7 +151,7 @@ available vars.
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 Exception, result.replace( "\n", "" )
raise PyNUTError( result.replace( "\n", "" ) )
ups_vars = {}
result = self.__srv_handler.read_until( "END LIST VAR %s\n" % ups )
@ -170,7 +177,7 @@ of the command as value
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 Exception, result.replace( "\n", "" )
raise PyNUTError( result.replace( "\n", "" ) )
ups_cmds = {}
result = self.__srv_handler.read_until( "END LIST CMD %s\n" % ups )
@ -185,7 +192,7 @@ of the command as value
self.__srv_handler.write( "GET CMDDESC %s %s\n" % ( ups, var ) )
temp = self.__srv_handler.read_until( "\n" )
if temp[:7] != "CMDDESC" :
raise
raise PyNUTError
else :
off = len( "CMDDESC %s %s " % ( ups, var ) )
desc = temp[off:-1].split('"')[1]
@ -207,7 +214,7 @@ The result is presented as a dictionary containing 'key->val' pairs
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 Exception, result.replace( "\n", "" )
raise PyNUTError( result.replace( "\n", "" ) )
result = self.__srv_handler.read_until( "END LIST RW %s\n" % ups )
offset = len( "VAR %s" % ups )
@ -237,7 +244,7 @@ rights to set it (maybe login/password).
if ( result == "OK\n" ) :
return( "OK" )
else :
raise Exception, result
raise PyNUTError( result )
def RunUPSCommand( self, ups="", command="" ) :
""" Send a command to the specified UPS
@ -253,7 +260,7 @@ Returns OK on success or raises an error
if ( result == "OK\n" ) :
return( "OK" )
else :
raise Exception, result.replace( "\n", "" )
raise PyNUTError( result.replace( "\n", "" ) )
def FSD( self, ups="") :
""" Send FSD command
@ -267,7 +274,7 @@ Returns OK on success or raises an error
self.__srv_handler.write( "MASTER %s\n" % ups )
result = self.__srv_handler.read_until( "\n" )
if ( result != "OK MASTER-GRANTED\n" ) :
raise Exception, ( "Master level function are not available", "" )
raise PyNUTError( ( "Master level function are not available", "" ) )
if self.__debug :
print( "[DEBUG] FSD called..." )
@ -276,7 +283,7 @@ Returns OK on success or raises an error
if ( result == "OK FSD-SET\n" ) :
return( "OK" )
else :
raise Exception, result.replace( "\n", "" )
raise PyNUTError( result.replace( "\n", "" ) )
def help(self) :
""" Send HELP command
@ -307,7 +314,7 @@ The result is a dictionary containing 'key->val' pairs of 'UPSName' and a list o
print( "[DEBUG] ListClients from server" )
if ups and (ups not in self.GetUPSList()):
raise Exception, "%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)
@ -315,7 +322,7 @@ The result is a dictionary containing 'key->val' pairs of 'UPSName' and a list o
self.__srv_handler.write( "LIST CLIENTS\n" )
result = self.__srv_handler.read_until( "\n" )
if result != "BEGIN LIST CLIENTS\n" :
raise Exception, result.replace( "\n", "" )
raise PyNUTError( result.replace( "\n", "" ) )
result = self.__srv_handler.read_until( "END LIST CLIENTS\n" )
ups_list = {}