@@ -74,11 +74,10 @@ def __del__(self):
7474 if self ._srv_handler :
7575 try :
7676 self ._srv_handler .write ("LOGOUT\n " )
77- except telnetlib .socket .error :
77+ self ._srv_handler .close ()
78+ except (telnetlib .socket .error , AttributeError ):
7879 # The socket is already disconnected.
7980 pass
80- finally :
81- self ._srv_handler .close ()
8281
8382 def __enter__ (self ):
8483 return self
@@ -96,15 +95,15 @@ def _connect(self):
9695
9796 try :
9897 self ._srv_handler = telnetlib .Telnet (self ._host , self ._port ,
99- timeout = self ._timeout )
98+ timeout = self ._timeout )
10099
101- if self ._login != None :
100+ if self ._login is not None :
102101 self ._srv_handler .write ("USERNAME %s\n " % self ._login )
103102 result = self ._srv_handler .read_until ("\n " , self ._timeout )
104103 if not result == "OK\n " :
105104 raise PyNUTError (result .replace ("\n " , "" ))
106105
107- if self ._password != None :
106+ if self ._password is not None :
108107 self ._srv_handler .write ("PASSWORD %s\n " % self ._password )
109108 result = self ._srv_handler .read_until ("\n " , self ._timeout )
110109 if not result == "OK\n " :
@@ -137,7 +136,7 @@ def list_ups(self):
137136 raise PyNUTError (result .replace ("\n " , "" ))
138137
139138 result = self ._srv_handler .read_until ("END LIST UPS\n " ,
140- self ._timeout )
139+ self ._timeout )
141140
142141 ups_dict = {}
143142 for line in result .split ("\n " ):
@@ -161,7 +160,7 @@ def list_vars(self, ups):
161160 raise PyNUTError (result .replace ("\n " , "" ))
162161
163162 result = self ._srv_handler .read_until ("END LIST VAR %s\n " % ups ,
164- self ._timeout )
163+ self ._timeout )
165164 offset = len ("VAR %s " % ups )
166165 end_offset = 0 - (len ("END LIST VAR %s\n " % ups ) + 1 )
167166
@@ -186,7 +185,7 @@ def list_commands(self, ups):
186185 raise PyNUTError (result .replace ("\n " , "" ))
187186
188187 result = self ._srv_handler .read_until ("END LIST CMD %s\n " % ups ,
189- self ._timeout )
188+ self ._timeout )
190189 offset = len ("CMD %s " % ups )
191190 end_offset = 0 - (len ("END LIST CMD %s\n " % ups ) + 1 )
192191
@@ -228,13 +227,13 @@ def list_clients(self, ups=None):
228227 raise PyNUTError (result .replace ("\n " , "" ))
229228
230229 result = self ._srv_handler .read_until ("END LIST CLIENTS\n " ,
231- self ._timeout )
230+ self ._timeout )
232231
233232 clients = {}
234233 for line in result .split ("\n " ):
235234 if line .startswith ("CLIENT" ):
236235 host , ups = line [len ("CLIENT " ):].split (' ' )[:2 ]
237- if not ups in clients :
236+ if ups not in clients :
238237 clients [ups ] = []
239238 clients [ups ].append (host )
240239
@@ -254,7 +253,7 @@ def list_rw_vars(self, ups):
254253 raise PyNUTError (result .replace ("\n " , "" ))
255254
256255 result = self ._srv_handler .read_until ("END LIST RW %s\n " % ups ,
257- self ._timeout )
256+ self ._timeout )
258257 offset = len ("VAR %s" % ups )
259258 end_offset = 0 - (len ("END LIST RW %s\n " % ups ) + 1 )
260259
@@ -278,13 +277,13 @@ def list_enum(self, ups, var):
278277 raise PyNUTError (result .replace ("\n " , "" ))
279278
280279 result = self ._srv_handler .read_until ("END LIST ENUM %s %s\n " % (ups , var ),
281- self ._timeout )
280+ self ._timeout )
282281 offset = len ("ENUM %s %s" % (ups , var ))
283282 end_offset = 0 - (len ("END LIST ENUM %s %s\n " % (ups , var )) + 1 )
284283
285284 try :
286285 return [ c [offset :].split ('"' )[1 ].strip ()
287- for c in result [:end_offset ].split ("\n " ) ]
286+ for c in result [:end_offset ].split ("\n " ) ]
288287 except IndexError :
289288 raise PyNUTError (result .replace ("\n " , "" ))
290289
@@ -301,13 +300,13 @@ def list_range(self, ups, var):
301300 raise PyNUTError (result .replace ("\n " , "" ))
302301
303302 result = self ._srv_handler .read_until ("END LIST RANGE %s %s\n " % (ups , var ),
304- self ._timeout )
303+ self ._timeout )
305304 offset = len ("RANGE %s %s" % (ups , var ))
306305 end_offset = 0 - (len ("END LIST RANGE %s %s\n " % (ups , var )) + 1 )
307306
308307 try :
309308 return [ c [offset :].split ('"' )[1 ].strip ()
310- for c in result [:end_offset ].split ("\n " ) ]
309+ for c in result [:end_offset ].split ("\n " ) ]
311310 except IndexError :
312311 raise PyNUTError (result .replace ("\n " , "" ))
313312
0 commit comments