Skip to content

Commit 8c401bc

Browse files
committed
docstrings and some code consistency fixes
1 parent 3bae9fa commit 8c401bc

1 file changed

Lines changed: 89 additions & 18 deletions

File tree

UPSmodules/UPSmodule.py

Lines changed: 89 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ def __init__(self):
322322
# Read and check the UPS list.
323323
def read_ups_list(self):
324324
"""Reads the config.json file which contains parameters for UPSs to be used by utility.
325-
326325
:return: boolean True if no problems reading list
327326
"""
328327
if not os.path.isfile(env.ut_const.UPS_LIST_JSON_FILE):
@@ -334,7 +333,6 @@ def read_ups_list(self):
334333

335334
def check_ups_list(self, quiet=True):
336335
"""Check the list of UPS for compatibility, accessibility, and responsiveness.
337-
338336
:param quiet: flag to specify if method should print results or return quietly
339337
:type quiet: bool
340338
:return:
@@ -429,35 +427,52 @@ def list_valid_ups_types(self):
429427

430428
# Methods to set daemon and active UPS.
431429
def set_daemon_ups(self):
430+
""" Set the active ups to the daemon ups.
431+
:return:
432+
"""
432433
for k, v in self.ups_list.items():
433434
if v['daemon']:
434435
self.active_ups = v
435436
return True
436437
return False
437438

438439
def set_active_ups(self, ups_item):
440+
""" Sets the active ups to the specified ups.
441+
:param ups_item: The target ups
442+
:type ups_item: dict
443+
:return:
444+
"""
439445
self.active_ups = ups_item
440446
# End of methods to set daemon and active UPS.
441447

442448
# Set of methods to return parameters for target UPS.
443449
def get_ups_parameter_value(self, param_name, tups):
450+
""" Get ups parameter value for parameter name from target UPS or active UPS if not specified
451+
:param param_name:
452+
:type param_name: str
453+
:param tups:
454+
:type tups: dict
455+
:return:
456+
"""
444457
if not tups:
445458
tups = self.active_ups
446459
if param_name in tups.keys():
447460
return tups[param_name]
448461
else:
449462
return None
450463

451-
# TODO test this and use where command name is needed.
452464
def get_mib_commands(self, tups=None):
465+
""" Get the list of MIB commands for the target UPS.
466+
:param tups:
467+
:type tups: dict
468+
:return: List of MIB commands for target UPS
469+
"""
453470
if not tups:
454471
tups = self.active_ups
455472
return tups['mib_commands']
456473

457-
# TODO test this and use where command name is needed.
458474
def get_mib_name(self, mib_cmd, tups=None):
459475
"""Get the mib command name.
460-
461476
:param mib_cmd: string representing mib command
462477
:param tups: target UPS, active UPS if missing
463478
:return: string of mib command name
@@ -471,43 +486,72 @@ def get_mib_name(self, mib_cmd, tups=None):
471486

472487
def get_mib_name_for_type(self, mib_cmd, ups_type):
473488
"""Get mib command name for a given UPS type.
474-
475489
:param mib_cmd:
476490
:param ups_type:
477491
:return: string of mib command name
478492
"""
479493
return self.all_mib_cmds[ups_type][mib_cmd]['name']
480494

481495
def ups_uuid(self, tups=None):
496+
""" Get the uuid value for the target UPS or active UPS if target is None.
497+
:param tups: The target ups dictionary from list or None.
498+
:type tups: dict
499+
:return: The uuid as an int.
500+
"""
482501
if not tups:
483502
tups = self.active_ups
484503
return tups['uuid']
485504

486505
def ups_name(self, tups=None):
506+
""" Get the name value for the target UPS or active UPS if target is None.
507+
:param tups: The target ups dictionary from list or None.
508+
:type tups: dict
509+
:return: The name as an str.
510+
"""
487511
if not tups:
488512
tups = self.active_ups
489513
return tups['display_name']
490514

491515
def ups_type(self, tups=None):
516+
""" Get the type value for the target UPS or active UPS if target is None.
517+
:param tups: The target ups dictionary from list or None.
518+
:type tups: dict
519+
:return: The ups_type as an str.
520+
"""
492521
if not tups:
493522
tups = self.active_ups
494523
return tups['ups_type']
495524

496525
def ups_ip(self, tups=None):
526+
""" Get the IP address value for the target UPS or active UPS if target is None.
527+
:param tups: The target ups dictionary from list or None.
528+
:type tups: dict
529+
:return: The IP address as an str.
530+
"""
497531
if not tups:
498532
tups = self.active_ups
499533
return tups['ups_IP']
500534
# End of set of methods to return parameters for target UPS.
501535

502536
# Set of methods to check if UPS is valid.
503537
def check_ip(self, tups=None):
538+
""" check the IP address value for the target UPS or active UPS if target is None.
539+
:param tups: The target ups dictionary from list or None.
540+
:type tups: dict
541+
:return: bool True if the given IP address is pingable, else False
542+
"""
504543
if not tups:
505544
tups = self.active_ups
506545
if not os.system('ping -c 1 {} > /dev/null'.format(tups['ups_IP'])):
507546
return True
508547
return False
509548

510549
def check_snmp(self, tups=None):
550+
""" check if the IP address for the target UPS or active UPS if target is None, responds to snmp command.
551+
:param tups: The target ups dictionary from list or None.
552+
:type tups: dict
553+
:return: bool True if the given IP address responds, else False
554+
"""
511555
if not tups:
512556
tups = self.active_ups
513557
cmd_str = 'snmpget -v2c -c {} {} {}'.format(tups['snmp_community'], tups['ups_IP'], 'iso.3.6.1.2.1.1.1.0')
@@ -520,24 +564,44 @@ def check_snmp(self, tups=None):
520564
return False
521565
return True
522566

523-
def is_compatible(self, v=None):
524-
if not v:
525-
v = self.active_ups
526-
return v['compatible']
567+
def is_compatible(self, tups=None):
568+
""" check if target UPS or active UPS if target is None, is compatible.
569+
:param tups: The target ups dictionary from list or None.
570+
:type tups: dict
571+
:return: bool True if compatible
572+
"""
573+
if not tups:
574+
tups = self.active_ups
575+
return tups['compatible']
527576

528-
def is_responsive(self, v=None):
529-
if not v:
530-
v = self.active_ups
531-
return v['responsive']
577+
def is_responsive(self, tups=None):
578+
""" check if target UPS or active UPS if target is None, is responsive.
579+
:param tups: The target ups dictionary from list or None.
580+
:type tups: dict
581+
:return: bool True if responsive
582+
"""
583+
if not tups:
584+
tups = self.active_ups
585+
return tups['responsive']
532586

533-
def is_accessible(self, v=None):
534-
if not v:
535-
v = self.active_ups
536-
return v['accessible']
587+
def is_accessible(self, tups=None):
588+
""" check if target UPS or active UPS if target is None, is accessible.
589+
:param tups: The target ups dictionary from list or None.
590+
:type tups: dict
591+
:return: bool True if accessible
592+
"""
593+
if not tups:
594+
tups = self.active_ups
595+
return tups['accessible']
537596
# End of set of methods to check if UPS is valid.
538597

539598
# Commands to read from UPS using snmp protocol.
540599
def get_monitor_mib_commands(self, cmd_type='dynamic'):
600+
""" Get the specified list of monitor mib commands for the active UPS.
601+
:param cmd_type: The target type of monitor commands
602+
:type cmd_type: str
603+
:return: list of relevant mib commands
604+
"""
541605
if cmd_type == 'all':
542606
return_list = []
543607
for cmd_type in ['static', 'dynamic']:
@@ -548,6 +612,13 @@ def get_monitor_mib_commands(self, cmd_type='dynamic'):
548612
return self.monitor_mib_cmds[cmd_type]
549613

550614
def read_all_ups_list_items(self, command_list, errups=True):
615+
""" Get the specified list of monitor mib commands for all UPSs.
616+
:param command_list: A list of mib commands to be read from the active UPS
617+
:type command_list: list
618+
:param errups: Flag to indicate if error UPS should be included.
619+
:type errups: bool
620+
:return: dict of results from the reading of all commands from all UPSs.
621+
"""
551622
results = {}
552623
for ups_name, ups_item in self.get_ups_list().items():
553624
if not errups:

0 commit comments

Comments
 (0)