Skip to content

Commit 0e131b6

Browse files
committed
Replace print calls with logging.debug.
1 parent 4d1a640 commit 0e131b6

1 file changed

Lines changed: 20 additions & 27 deletions

File tree

nut2.py

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,14 @@
4848
#
4949

5050
import telnetlib
51+
import logging
5152

5253

5354
__version__ = '2.0.0'
5455
__all__ = ['PyNUTError', 'PyNUTClient']
5556

57+
logging.basicConfig(level=logging.WARNING, format="[%(levelname)s] %(message)s")
58+
5659

5760
class PyNUTError(Exception):
5861
"""Base class for custom exceptions."""
@@ -72,12 +75,13 @@ def __init__(self, host="127.0.0.1", port=3493, login=None, password=None, debug
7275
on console, default to False)
7376
timeout : Timeout used to wait for network response
7477
"""
75-
self._debug = debug
78+
if debug:
79+
# Print DEBUG messages to the console.
80+
logging.getLogger().setLevel(logging.DEBUG)
7681

77-
if self._debug:
78-
print("[DEBUG] Class initialization...")
79-
print("[DEBUG] -> Host = %s (port %s)" % (host, port))
80-
print("[DEBUG] -> Login = '%s' / '%s'" % (login, password))
82+
logging.debug("Class initialization...")
83+
logging.debug(" -> Host = %s (port %s)" % (host, port))
84+
logging.debug(" -> Login = '%s' / '%s'" % (login, password))
8185

8286
self._host = host
8387
self._port = port
@@ -112,8 +116,7 @@ def _connect(self):
112116
If login/pass was specified, the class tries to authenticate.
113117
An error is raised if something goes wrong.
114118
"""
115-
if self._debug:
116-
print("[DEBUG] Connecting to host")
119+
logging.debug("Connecting to host")
117120

118121
self._srv_handler = telnetlib.Telnet(self._host, self._port)
119122

@@ -135,8 +138,7 @@ def list_ups(self):
135138
The result is a dictionary containing 'key->val' pairs of
136139
'UPSName' and 'UPS Description'.
137140
"""
138-
if self._debug:
139-
print("[DEBUG] list_ups from server")
141+
logging.debug("list_ups from server")
140142

141143
self._srv_handler.write("LIST UPS\n")
142144
result = self._srv_handler.read_until("\n")
@@ -159,8 +161,7 @@ def list_vars(self, ups=""):
159161
The result is a dictionary containing 'key->val' pairs of all
160162
available vars.
161163
"""
162-
if self._debug:
163-
print("[DEBUG] list_vars called...")
164+
logging.debug("list_vars called...")
164165

165166
self._srv_handler.write("LIST VAR %s\n" % ups)
166167
result = self._srv_handler.read_until("\n")
@@ -185,8 +186,7 @@ def list_commands(self, ups=""):
185186
The result is a dict object with command name as key and a description
186187
of the command as value.
187188
"""
188-
if self._debug:
189-
print("[DEBUG] list_commands called...")
189+
logging.debug("list_commands called...")
190190

191191
self._srv_handler.write("LIST CMD %s\n" % ups)
192192
result = self._srv_handler.read_until("\n")
@@ -223,8 +223,7 @@ def list_clients(self, ups=None):
223223
The result is a dictionary containing 'key->val' pairs of
224224
'UPSName' and a list of clients.
225225
"""
226-
if self._debug:
227-
print("[DEBUG] list_clients from server")
226+
logging.debug("list_clients from server")
228227

229228
if ups and (ups not in self.list_ups()):
230229
raise PyNUTError("%s is not a valid UPS" % ups)
@@ -256,8 +255,7 @@ def list_rw_vars(self, ups=""):
256255
The result is presented as a dictionary containing 'key->val'
257256
pairs.
258257
"""
259-
if self._debug:
260-
print("[DEBUG] list_vars from '%s'..." % ups)
258+
logging.debug("list_vars from '%s'..." % ups)
261259

262260
self._srv_handler.write("LIST RW %s\n" % ups)
263261
result = self._srv_handler.read_until("\n")
@@ -300,8 +298,7 @@ def run_command(self, ups="", command=""):
300298
Returns OK on success or raises an error.
301299
"""
302300

303-
if self._debug:
304-
print("[DEBUG] run_command called...")
301+
logging.debug("run_command called...")
305302

306303
self._srv_handler.write("INSTCMD %s %s\n" % (ups, command))
307304
result = self._srv_handler.read_until("\n")
@@ -316,16 +313,14 @@ def fsd(self, ups=""):
316313
Returns OK on success or raises an error.
317314
"""
318315

319-
if self._debug:
320-
print("[DEBUG] MASTER called...")
316+
logging.debug("MASTER called...")
321317

322318
self._srv_handler.write("MASTER %s\n" % ups)
323319
result = self._srv_handler.read_until("\n")
324320
if result != "OK MASTER-GRANTED\n":
325321
raise PyNUTError(("Master level function are not available", ""))
326322

327-
if self._debug:
328-
print("[DEBUG] FSD called...")
323+
logging.debug("FSD called...")
329324
self._srv_handler.write("FSD %s\n" % ups)
330325
result = self._srv_handler.read_until("\n")
331326
if result == "OK FSD-SET\n":
@@ -336,17 +331,15 @@ def fsd(self, ups=""):
336331
def help(self):
337332
"""Send HELP command."""
338333

339-
if self._debug:
340-
print("[DEBUG] HELP called...")
334+
logging.debug("HELP called...")
341335

342336
self._srv_handler.write("HELP\n")
343337
return self._srv_handler.read_until("\n")
344338

345339
def ver(self):
346340
"""Send VER command."""
347341

348-
if self._debug:
349-
print("[DEBUG] VER called...")
342+
logging.debug("VER called...")
350343

351344
self._srv_handler.write("VER\n")
352345
return self._srv_handler.read_until("\n")

0 commit comments

Comments
 (0)