Skip to content

Commit ef7d3e2

Browse files
committed
Added support for new commands.
Refactored some old ones too.
1 parent ff1a368 commit ef7d3e2

3 files changed

Lines changed: 115 additions & 4 deletions

File tree

nut2.py

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,17 @@ def list_ups(self):
157157

158158
return ups_dict
159159

160+
def description(self, ups):
161+
"""Returns the description for a given UPS."""
162+
logging.debug("description called...")
163+
164+
self._srv_handler.write("GET UPSDESC %s\n" % ups)
165+
result = self._srv_handler.read_until("\n", self._timeout)
166+
try:
167+
return result.split('"')[1].strip()
168+
except IndexError:
169+
raise PyNUTError(result.replace("\n", ""))
170+
160171
def list_vars(self, ups=""):
161172
"""Get all available vars from the specified UPS.
162173
@@ -275,7 +286,7 @@ def list_rw_vars(self, ups=""):
275286

276287
return rw_vars
277288

278-
def set_var(self, ups="", var="", value=""):
289+
def set_var(self, ups, var, value):
279290
"""Set a variable to the specified value on selected UPS.
280291
281292
The variable must be a writable value (cf list_rw_vars) and you
@@ -288,7 +299,48 @@ def set_var(self, ups="", var="", value=""):
288299
if result != "OK\n":
289300
raise PyNUTError(result.replace("\n", ""))
290301

291-
def run_command(self, ups="", command=""):
302+
def get_var(self, ups, var):
303+
"""Get the value of a variable."""
304+
logging.debug("get_var called...")
305+
306+
self._srv_handler.write("GET VAR %s %s\n" % (ups, var))
307+
result = self._srv_handler.read_until("\n", self._timeout)
308+
try:
309+
# result = 'VAR %s %s "%s"' % (ups, var, value)
310+
return result.split('"')[1].strip()
311+
except IndexError:
312+
raise PyNUTError(result.replace("\n", ""))
313+
314+
# Alias for convenience
315+
def get(self, ups, var):
316+
"""Get the value of a variable (alias for get_var)."""
317+
return self.get_var(ups, var)
318+
319+
def var_description(self, ups, var):
320+
"""Get a variable's description."""
321+
logging.debug("var_description called...")
322+
323+
self._srv_handler.write("GET DESC %s %s\n" % (ups, var))
324+
result = self._srv_handler.read_until("\n", self._timeout)
325+
try:
326+
# result = 'DESC %s %s "%s"' % (ups, var, description)
327+
return result.split('"')[1].strip()
328+
except IndexError:
329+
raise PyNUTError(result.replace("\n", ""))
330+
331+
def command_description(self, ups, command):
332+
"""Get a command's description."""
333+
logging.debug("command_description called...")
334+
335+
self._srv_handler.write("GET CMDDESC %s %s\n" % (ups, command))
336+
result = self._srv_handler.read_until("\n", self._timeout)
337+
try:
338+
# result = 'CMDDESC %s %s "%s"' % (ups, command, description)
339+
return result.split('"')[1].strip()
340+
except IndexError:
341+
raise PyNUTError(result.replace("\n", ""))
342+
343+
def run_command(self, ups, command):
292344
"""Send a command to the specified UPS."""
293345
logging.debug("run_command called...")
294346

@@ -312,6 +364,20 @@ def fsd(self, ups=""):
312364
if result != "OK FSD-SET\n":
313365
raise PyNUTError(result.replace("\n", ""))
314366

367+
def num_logins(self, ups=""):
368+
"""Send GET NUMLOGINS command to get the number of users logged
369+
into a given UPS.
370+
"""
371+
logging.debug("num_logins called on '%s'...", ups)
372+
373+
self._srv_handler.write("GET NUMLOGINS %s\n" % ups)
374+
result = self._srv_handler.read_until("\n", self._timeout)
375+
try:
376+
# result = "NUMLOGINS %s %s\n" % (ups, int(numlogins))
377+
return int(result.split(' ')[2].strip())
378+
except (ValueError, IndexError):
379+
raise PyNUTError(result.replace("\n", ""))
380+
315381
def help(self):
316382
"""Send HELP command."""
317383
logging.debug("HELP called...")

tests/mockserver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def run_command(self):
2929
elif self.command == "VER\n":
3030
return 'Network UPS Tools upsd 2.7.1 - http://www.networkupstools.org/\n'
3131
elif self.command == "GET CMDDESC %s %s\n" % (self.valid, self.valid):
32-
return 'CMDDESC '+self.valid+' '+self.valid+' "Test description"\n'
32+
return 'CMDDESC '+self.valid+' '+self.valid+' '+self.valid_desc+'\n'
3333
elif self.command == "LIST UPS\n" and self.first:
3434
self.first = False
3535
return 'BEGIN LIST UPS\n'

tests/testclient.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ def setUp(self):
2121
self.valid = "test"
2222
self.invalid = "does_not_exist"
2323
self.valid_ups_name = "Test UPS 1"
24-
self.valid_command_desc = "Test description"
24+
self.valid_desc = self.valid_ups_name
25+
self.valid_value = '100'
26+
self.valid_command_desc = self.valid_desc
2527
telnetlib.Telnet = Mock()
2628

2729
def test_init_with_args(self):
@@ -182,3 +184,46 @@ def test_list_clients_none(self):
182184
def test_list_clients_broken(self):
183185
self.assertRaises(PyNUTError, self.broken_client.list_clients,
184186
self.valid)
187+
188+
def test_num_logins(self):
189+
self.assertEquals(self.client.num_logins(self.valid), 1)
190+
191+
def test_num_logins_broken(self):
192+
self.assertRaises(PyNUTError, self.broken_client.num_logins,
193+
self.valid)
194+
195+
def test_description(self):
196+
self.assertEquals(self.client.description(self.valid),
197+
self.valid_desc)
198+
199+
def test_description_broken(self):
200+
self.assertRaises(PyNUTError, self.broken_client.description,
201+
self.valid)
202+
203+
def test_get(self):
204+
self.assertEquals(self.client.get(self.valid, self.valid),
205+
self.valid_value)
206+
207+
def test_get_var_alias_for_get(self):
208+
self.assertEquals(self.client.get(self.valid, self.valid),
209+
self.client.get_var(self.valid, self.valid))
210+
211+
def test_get_broken(self):
212+
self.assertRaises(PyNUTError, self.broken_client.get,
213+
self.valid, self.valid)
214+
215+
def test_var_description(self):
216+
self.assertEquals(self.client.var_description(self.valid,
217+
self.valid), self.valid_desc)
218+
219+
def test_var_description_broken(self):
220+
self.assertRaises(PyNUTError, self.broken_client.var_description,
221+
self.valid, self.valid)
222+
223+
def test_command_description(self):
224+
self.assertEquals(self.client.command_description(self.valid,
225+
self.valid), self.valid_desc)
226+
227+
def test_command_description_broken(self):
228+
self.assertRaises(PyNUTError, self.broken_client.command_description,
229+
self.valid, self.valid)

0 commit comments

Comments
 (0)