Skip to content

Commit af36fc1

Browse files
committed
Use the timeout value on every read.
1 parent 86601da commit af36fc1

1 file changed

Lines changed: 23 additions & 18 deletions

File tree

nut2.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,12 @@ def list_ups(self):
142142
logging.debug("list_ups from server")
143143

144144
self._srv_handler.write("LIST UPS\n")
145-
result = self._srv_handler.read_until("\n")
145+
result = self._srv_handler.read_until("\n", self._timeout)
146146
if result != "BEGIN LIST UPS\n":
147147
raise PyNUTError(result.replace("\n", ""))
148148

149-
result = self._srv_handler.read_until("END LIST UPS\n")
149+
result = self._srv_handler.read_until("END LIST UPS\n",
150+
self._timeout)
150151

151152
ups_dict = {}
152153
for line in result.split("\n"):
@@ -165,11 +166,12 @@ def list_vars(self, ups=""):
165166
logging.debug("list_vars called...")
166167

167168
self._srv_handler.write("LIST VAR %s\n" % ups)
168-
result = self._srv_handler.read_until("\n")
169+
result = self._srv_handler.read_until("\n", self._timeout)
169170
if result != "BEGIN LIST VAR %s\n" % ups:
170171
raise PyNUTError(result.replace("\n", ""))
171172

172-
result = self._srv_handler.read_until("END LIST VAR %s\n" % ups)
173+
result = self._srv_handler.read_until("END LIST VAR %s\n" % ups,
174+
self._timeout)
173175
offset = len("VAR %s " % ups)
174176
end_offset = 0 - (len("END LIST VAR %s\n" % ups) + 1)
175177

@@ -189,11 +191,12 @@ def list_commands(self, ups=""):
189191
logging.debug("list_commands called...")
190192

191193
self._srv_handler.write("LIST CMD %s\n" % ups)
192-
result = self._srv_handler.read_until("\n")
194+
result = self._srv_handler.read_until("\n", self._timeout)
193195
if result != "BEGIN LIST CMD %s\n" % ups:
194196
raise PyNUTError(result.replace("\n", ""))
195197

196-
result = self._srv_handler.read_until("END LIST CMD %s\n" % ups)
198+
result = self._srv_handler.read_until("END LIST CMD %s\n" % ups,
199+
self._timeout)
197200
offset = len("CMD %s " % ups)
198201
end_offset = 0 - (len("END LIST CMD %s\n" % ups) + 1)
199202

@@ -204,7 +207,7 @@ def list_commands(self, ups=""):
204207
# For each var we try to get the available description
205208
try:
206209
self._srv_handler.write("GET CMDDESC %s %s\n" % (ups, command))
207-
temp = self._srv_handler.read_until("\n")
210+
temp = self._srv_handler.read_until("\n", self._timeout)
208211
if temp.startswith("CMDDESC"):
209212
desc_offset = len("CMDDESC %s %s " % (ups, command))
210213
commands[command] = temp[desc_offset:-1].split('"')[1]
@@ -230,11 +233,12 @@ def list_clients(self, ups=None):
230233
self._srv_handler.write("LIST CLIENTS %s\n" % ups)
231234
else:
232235
self._srv_handler.write("LIST CLIENTS\n")
233-
result = self._srv_handler.read_until("\n")
236+
result = self._srv_handler.read_until("\n", self._timeout)
234237
if result != "BEGIN LIST CLIENTS\n":
235238
raise PyNUTError(result.replace("\n", ""))
236239

237-
result = self._srv_handler.read_until("END LIST CLIENTS\n")
240+
result = self._srv_handler.read_until("END LIST CLIENTS\n",
241+
self._timeout)
238242

239243
clients = {}
240244
for line in result.split("\n"):
@@ -255,11 +259,12 @@ def list_rw_vars(self, ups=""):
255259
logging.debug("list_vars from '%s'...", ups)
256260

257261
self._srv_handler.write("LIST RW %s\n" % ups)
258-
result = self._srv_handler.read_until("\n")
262+
result = self._srv_handler.read_until("\n", self._timeout)
259263
if result != "BEGIN LIST RW %s\n" % ups:
260264
raise PyNUTError(result.replace("\n", ""))
261265

262-
result = self._srv_handler.read_until("END LIST RW %s\n" % ups)
266+
result = self._srv_handler.read_until("END LIST RW %s\n" % ups,
267+
self._timeout)
263268
offset = len("VAR %s" % ups)
264269
end_offset = 0 - (len("END LIST RW %s\n" % ups) + 1)
265270

@@ -279,7 +284,7 @@ def set_var(self, ups="", var="", value=""):
279284
logging.debug("set_var '%s' from '%s' to '%s'", var, ups, value)
280285

281286
self._srv_handler.write("SET VAR %s %s %s\n" % (ups, var, value))
282-
result = self._srv_handler.read_until("\n")
287+
result = self._srv_handler.read_until("\n", self._timeout)
283288
if result != "OK\n":
284289
raise PyNUTError(result.replace("\n", ""))
285290

@@ -288,22 +293,22 @@ def run_command(self, ups="", command=""):
288293
logging.debug("run_command called...")
289294

290295
self._srv_handler.write("INSTCMD %s %s\n" % (ups, command))
291-
result = self._srv_handler.read_until("\n")
296+
result = self._srv_handler.read_until("\n", self._timeout)
292297
if result != "OK\n":
293298
raise PyNUTError(result.replace("\n", ""))
294299

295300
def fsd(self, ups=""):
296-
"""Send FSD command."""
301+
"""Send MASTER and FSD commands."""
297302
logging.debug("MASTER called...")
298303

299304
self._srv_handler.write("MASTER %s\n" % ups)
300-
result = self._srv_handler.read_until("\n")
305+
result = self._srv_handler.read_until("\n", self._timeout)
301306
if result != "OK MASTER-GRANTED\n":
302307
raise PyNUTError(("Master level function are not available", ""))
303308

304309
logging.debug("FSD called...")
305310
self._srv_handler.write("FSD %s\n" % ups)
306-
result = self._srv_handler.read_until("\n")
311+
result = self._srv_handler.read_until("\n", self._timeout)
307312
if result != "OK FSD-SET\n":
308313
raise PyNUTError(result.replace("\n", ""))
309314

@@ -312,11 +317,11 @@ def help(self):
312317
logging.debug("HELP called...")
313318

314319
self._srv_handler.write("HELP\n")
315-
return self._srv_handler.read_until("\n")
320+
return self._srv_handler.read_until("\n", self._timeout)
316321

317322
def ver(self):
318323
"""Send VER command."""
319324
logging.debug("VER called...")
320325

321326
self._srv_handler.write("VER\n")
322-
return self._srv_handler.read_until("\n")
327+
return self._srv_handler.read_until("\n", self._timeout)

0 commit comments

Comments
 (0)