Skip to content

Commit d77b8c4

Browse files
committed
Added even more methods.
1 parent 29468c6 commit d77b8c4

3 files changed

Lines changed: 101 additions & 15 deletions

File tree

nut2.py

Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ def _connect(self):
108108
if not result == "OK\n":
109109
raise PyNUTError(result.replace("\n", ""))
110110

111+
def description(self, ups):
112+
"""Returns the description for a given UPS."""
113+
logging.debug("description called...")
114+
115+
self._srv_handler.write("GET UPSDESC %s\n" % ups)
116+
result = self._srv_handler.read_until("\n", self._timeout)
117+
try:
118+
return result.split('"')[1].strip()
119+
except IndexError:
120+
raise PyNUTError(result.replace("\n", ""))
121+
111122
def list_ups(self):
112123
"""Returns the list of available UPS from the NUT server.
113124
@@ -132,17 +143,6 @@ def list_ups(self):
132143

133144
return ups_dict
134145

135-
def description(self, ups):
136-
"""Returns the description for a given UPS."""
137-
logging.debug("description called...")
138-
139-
self._srv_handler.write("GET UPSDESC %s\n" % ups)
140-
result = self._srv_handler.read_until("\n", self._timeout)
141-
try:
142-
return result.split('"')[1].strip()
143-
except IndexError:
144-
raise PyNUTError(result.replace("\n", ""))
145-
146146
def list_vars(self, ups):
147147
"""Get all available vars from the specified UPS.
148148
@@ -261,6 +261,52 @@ def list_rw_vars(self, ups):
261261

262262
return rw_vars
263263

264+
def list_enum(self, ups, var):
265+
"""Get a list of valid values for an enum variable.
266+
267+
The result is presented as a list.
268+
"""
269+
logging.debug("list_enum from '%s'...", ups)
270+
271+
self._srv_handler.write("LIST ENUM %s %s\n" % (ups, var))
272+
result = self._srv_handler.read_until("\n", self._timeout)
273+
if result != "BEGIN LIST ENUM %s %s\n" % (ups, var):
274+
raise PyNUTError(result.replace("\n", ""))
275+
276+
result = self._srv_handler.read_until("END LIST ENUM %s %s\n" % (ups, var),
277+
self._timeout)
278+
offset = len("ENUM %s %s" % (ups, var))
279+
end_offset = 0 - (len("END LIST ENUM %s %s\n" % (ups, var)) + 1)
280+
281+
try:
282+
return [ c[offset:].split('"')[1].strip()
283+
for c in result[:end_offset].split("\n") ]
284+
except IndexError:
285+
raise PyNUTError(result.replace("\n", ""))
286+
287+
def list_range(self, ups, var):
288+
"""Get a list of valid values for an range variable.
289+
290+
The result is presented as a list.
291+
"""
292+
logging.debug("list_range from '%s'...", ups)
293+
294+
self._srv_handler.write("LIST RANGE %s %s\n" % (ups, var))
295+
result = self._srv_handler.read_until("\n", self._timeout)
296+
if result != "BEGIN LIST RANGE %s %s\n" % (ups, var):
297+
raise PyNUTError(result.replace("\n", ""))
298+
299+
result = self._srv_handler.read_until("END LIST RANGE %s %s\n" % (ups, var),
300+
self._timeout)
301+
offset = len("RANGE %s %s" % (ups, var))
302+
end_offset = 0 - (len("END LIST RANGE %s %s\n" % (ups, var)) + 1)
303+
304+
try:
305+
return [ c[offset:].split('"')[1].strip()
306+
for c in result[:end_offset].split("\n") ]
307+
except IndexError:
308+
raise PyNUTError(result.replace("\n", ""))
309+
264310
def set_var(self, ups, var, value):
265311
"""Set a variable to the specified value on selected UPS.
266312
@@ -281,7 +327,7 @@ def get_var(self, ups, var):
281327
self._srv_handler.write("GET VAR %s %s\n" % (ups, var))
282328
result = self._srv_handler.read_until("\n", self._timeout)
283329
try:
284-
# result = 'VAR %s %s "%s"' % (ups, var, value)
330+
# result = 'VAR %s %s "%s"\n' % (ups, var, value)
285331
return result.split('"')[1].strip()
286332
except IndexError:
287333
raise PyNUTError(result.replace("\n", ""))
@@ -298,11 +344,27 @@ def var_description(self, ups, var):
298344
self._srv_handler.write("GET DESC %s %s\n" % (ups, var))
299345
result = self._srv_handler.read_until("\n", self._timeout)
300346
try:
301-
# result = 'DESC %s %s "%s"' % (ups, var, description)
347+
# result = 'DESC %s %s "%s"\n' % (ups, var, description)
302348
return result.split('"')[1].strip()
303349
except IndexError:
304350
raise PyNUTError(result.replace("\n", ""))
305351

352+
def var_type(self, ups, var):
353+
"""Get a variable's type."""
354+
logging.debug("var_type called...")
355+
356+
self._srv_handler.write("GET TYPE %s %s\n" % (ups, var))
357+
result = self._srv_handler.read_until("\n", self._timeout)
358+
try:
359+
# result = 'TYPE %s %s %s\n' % (ups, var, type)
360+
type_ = ' '.join(result.split(' ')[3:]).strip()
361+
# Ensure the response was valid.
362+
assert(len(type_) > 0)
363+
assert(result.startswith("TYPE"))
364+
return type_
365+
except AssertionError:
366+
raise PyNUTError(result.replace("\n", ""))
367+
306368
def command_description(self, ups, command):
307369
"""Get a command's description."""
308370
logging.debug("command_description called...")

tests/mockserver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ def run_command(self):
6565
return 'ERR INVALID-ARGUMENT\n'
6666
elif self.command == "LIST ENUM %s %s\n" % (self.valid, self.valid) and self.first:
6767
self.first = False
68-
return 'BEGIN LIST ENUM %s %s\n' (self.valid, self.valid)
68+
return 'BEGIN LIST ENUM %s %s\n' % (self.valid, self.valid)
6969
elif self.command == "LIST ENUM %s %s\n" % (self.valid, self.valid):
7070
return 'ENUM %s %s %s\nEND LIST ENUM %s %s\n' % (self.valid,
7171
self.valid, self.valid_desc, self.valid, self.valid)
7272
elif self.command == "LIST RANGE %s %s\n" % (self.valid, self.valid) and self.first:
7373
self.first = False
74-
return 'BEGIN LIST RANGE %s %s\n' (self.valid, self.valid)
74+
return 'BEGIN LIST RANGE %s %s\n' % (self.valid, self.valid)
7575
elif self.command == "LIST RANGE %s %s\n" % (self.valid, self.valid):
7676
return 'RANGE %s %s %s %s\nEND LIST RANGE %s %s\n' % (self.valid,
7777
self.valid, self.valid_desc, self.valid_desc, self.valid, self.valid)

tests/testclient.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,27 @@ def test_command_description(self):
227227
def test_command_description_broken(self):
228228
self.assertRaises(PyNUTError, self.broken_client.command_description,
229229
self.valid, self.valid)
230+
231+
def test_list_enum(self):
232+
self.assertEquals(self.client.list_enum(self.valid, self.valid),
233+
[self.valid_desc])
234+
235+
def test_list_enum_broken(self):
236+
self.assertRaises(PyNUTError, self.broken_client.list_enum,
237+
self.valid, self.valid)
238+
239+
def test_list_range(self):
240+
self.assertEquals(self.client.list_range(self.valid, self.valid),
241+
[self.valid_desc])
242+
243+
def test_list_range_broken(self):
244+
self.assertRaises(PyNUTError, self.broken_client.list_range,
245+
self.valid, self.valid)
246+
247+
def test_var_type(self):
248+
self.assertEquals(self.client.var_type(self.valid, self.valid),
249+
"RW STRING:3")
250+
251+
def test_var_type_broken(self):
252+
self.assertRaises(PyNUTError, self.broken_client.var_type,
253+
self.valid, self.valid)

0 commit comments

Comments
 (0)