@@ -66,14 +66,15 @@ class PyNUTClient(object):
6666 def __init__ (self , host = "127.0.0.1" , port = 3493 , login = None , password = None , debug = False , timeout = 5 , connect = True ):
6767 """Class initialization method.
6868
69- host : Host to connect (defaults to 127.0.0.1)
70- port : Port where NUT listens for connections (defaults to 3493)
69+ host : Host to connect (defaults to 127.0.0.1).
70+ port : Port where NUT listens for connections (defaults to 3493).
7171 login : Login used to connect to NUT server (defaults to None
72- for no authentication)
73- password : Password used when using authentication (defaults to None)
72+ for no authentication).
73+ password : Password used when using authentication (defaults to None).
7474 debug : Boolean, put class in debug mode (prints everything
75- on console, default to False)
76- timeout : Timeout used to wait for network response
75+ on console, defaults to False).
76+ timeout : Timeout used to wait for network response (defaults
77+ to 5 seconds).
7778 """
7879 if debug :
7980 # Print DEBUG messages to the console.
@@ -123,13 +124,13 @@ def _connect(self):
123124 if self ._login != None :
124125 self ._srv_handler .write ("USERNAME %s\n " % self ._login )
125126 result = self ._srv_handler .read_until ("\n " , self ._timeout )
126- if result [: 2 ] != "OK" :
127+ if not result . startswith ( "OK" ) :
127128 raise PyNUTError (result .replace ("\n " , "" ))
128129
129130 if self ._password != None :
130131 self ._srv_handler .write ("PASSWORD %s\n " % self ._password )
131132 result = self ._srv_handler .read_until ("\n " , self ._timeout )
132- if result [: 2 ] != "OK" :
133+ if not result . startswith ( "OK" ) :
133134 raise PyNUTError (result .replace ("\n " , "" ))
134135
135136 def list_ups (self ):
@@ -146,12 +147,12 @@ def list_ups(self):
146147 raise PyNUTError (result .replace ("\n " , "" ))
147148
148149 result = self ._srv_handler .read_until ("END LIST UPS\n " )
149- ups_dict = {}
150150
151+ ups_dict = {}
151152 for line in result .split ("\n " ):
152- if line [: 3 ] == "UPS" :
153- ups , desc = line [4 : - 1 ].split ('"' )
154- ups_dict [ups .replace ( " " , "" )] = desc
153+ if line . startswith ( "UPS" ) :
154+ ups , desc = line [len ( "UPS " ): - len ( '"' ) ].split ('"' )[: 2 ]
155+ ups_dict [ups .strip ( )] = desc . strip ()
155156
156157 return ups_dict
157158
@@ -168,15 +169,14 @@ def list_vars(self, ups=""):
168169 if result != "BEGIN LIST VAR %s\n " % ups :
169170 raise PyNUTError (result .replace ("\n " , "" ))
170171
171- ups_vars = {}
172172 result = self ._srv_handler .read_until ("END LIST VAR %s\n " % ups )
173173 offset = len ("VAR %s " % ups )
174174 end_offset = 0 - (len ("END LIST VAR %s\n " % ups ) + 1 )
175175
176+ ups_vars = {}
176177 for current in result [:end_offset ].split ("\n " ):
177- var = current [offset :].split ('"' )[0 ].replace (" " , "" )
178- data = current [offset :].split ('"' )[1 ]
179- ups_vars [var ] = data
178+ var , data = current [offset :].split ('"' )[:2 ]
179+ ups_vars [var .strip ()] = data
180180
181181 return ups_vars
182182
0 commit comments