Skip to content

Commit 8476758

Browse files
committed
Refactored list_commands.
1 parent 949c3b8 commit 8476758

1 file changed

Lines changed: 10 additions & 12 deletions

File tree

nut2.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -193,29 +193,27 @@ def list_commands(self, ups=""):
193193
if result != "BEGIN LIST CMD %s\n" % ups:
194194
raise PyNUTError(result.replace("\n", ""))
195195

196-
ups_cmds = {}
197196
result = self._srv_handler.read_until("END LIST CMD %s\n" % ups)
198197
offset = len("CMD %s " % ups)
199198
end_offset = 0 - (len("END LIST CMD %s\n" % ups) + 1)
200199

200+
commands = {}
201201
for current in result[:end_offset].split("\n"):
202-
var = current[offset:].split('"')[0].replace(" ", "")
202+
command = current[offset:].split('"')[0].strip()
203203

204204
# For each var we try to get the available description
205205
try:
206-
self._srv_handler.write("GET CMDDESC %s %s\n" % (ups, var))
206+
self._srv_handler.write("GET CMDDESC %s %s\n" % (ups, command))
207207
temp = self._srv_handler.read_until("\n")
208-
if temp[:7] != "CMDDESC":
209-
raise PyNUTError
208+
if temp.startswith("CMDDESC"):
209+
desc_offset = len("CMDDESC %s %s " % (ups, command))
210+
commands[command] = temp[desc_offset:-1].split('"')[1]
210211
else:
211-
off = len("CMDDESC %s %s " % (ups, var))
212-
desc = temp[off:-1].split('"')[1]
213-
except PyNUTError:
214-
desc = var
212+
commands[command] = command
213+
except IndexError:
214+
commands[command] = command
215215

216-
ups_cmds[var] = desc
217-
218-
return ups_cmds
216+
return commands
219217

220218
def list_clients(self, ups=None):
221219
"""Returns the list of connected clients from the NUT server.

0 commit comments

Comments
 (0)