Skip to content

Commit

Permalink
Align style with PEP8 standards (#128)
Browse files Browse the repository at this point in the history
Align style with slightly modified PEP8 standards (extend maximum line length to 120 chars). This will also help in the transition to Python 3, where it is more strict about whitespace.

Done using `autopep8 --in-place --max-line-length 120` and some manual tweaks.
  • Loading branch information
jleveque authored Dec 8, 2020
1 parent 5e1c67e commit 4da0bfc
Show file tree
Hide file tree
Showing 26 changed files with 345 additions and 226 deletions.
80 changes: 47 additions & 33 deletions sonic-chassisd/scripts/chassisd
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ try:
from swsscommon import swsscommon
from sonic_platform_base.module_base import ModuleBase
except ImportError as e:
raise ImportError (str(e) + " - required module not found")
raise ImportError(str(e) + " - required module not found")

#
# Constants ====================================================================
Expand Down Expand Up @@ -59,13 +59,15 @@ INVALID_SLOT = ModuleBase.MODULE_INVALID_SLOT
INVALID_MODULE_INDEX = -1

MODULE_ADMIN_DOWN = 0
MODULE_ADMIN_UP = 1
MODULE_ADMIN_UP = 1

#
# Helper functions =============================================================
#

# try get information from platform API and return a default value if caught NotImplementedError


def try_get(callback, *args, **kwargs):
"""
Handy function to invoke the callback and catch NotImplementedError
Expand All @@ -86,16 +88,18 @@ def try_get(callback, *args, **kwargs):
#
# Module Config Updater ========================================================
#


class ModuleConfigUpdater(logger.Logger):

def __init__(self, log_identifier, chassis):
"""
Constructor for ModuleConfigUpdater
:param chassis: Object representing a platform chassis
"""
super(ModuleConfigUpdater, self).__init__(log_identifier)
"""
Constructor for ModuleConfigUpdater
:param chassis: Object representing a platform chassis
"""
super(ModuleConfigUpdater, self).__init__(log_identifier)

self.chassis = chassis
self.chassis = chassis

def deinit(self):
"""
Expand All @@ -108,8 +112,9 @@ class ModuleConfigUpdater(logger.Logger):
not key.startswith(ModuleBase.MODULE_TYPE_LINE) and \
not key.startswith(ModuleBase.MODULE_TYPE_FABRIC):
self.log_error("Incorrect module-name {}. Should start with {} or {} or {}".format(key,
ModuleBase.MODULE_TYPE_SUPERVISOR, ModuleBase.MODULE_TYPE_LINE,
ModuleBase.MODULE_TYPE_FABRIC))
ModuleBase.MODULE_TYPE_SUPERVISOR,
ModuleBase.MODULE_TYPE_LINE,
ModuleBase.MODULE_TYPE_FABRIC))
return

module_index = try_get(self.chassis.get_module_index, key, default=INVALID_MODULE_INDEX)
Expand All @@ -121,32 +126,33 @@ class ModuleConfigUpdater(logger.Logger):

if (admin_state == MODULE_ADMIN_DOWN) or (admin_state == MODULE_ADMIN_UP):
# Setting the module to administratively up/down state
self.log_info("Changing module {} to admin {} state".format(key,
'DOWN' if admin_state == MODULE_ADMIN_DOWN else 'UP'))
self.log_info("Changing module {} to admin {} state".format(key, 'DOWN' if admin_state == MODULE_ADMIN_DOWN else 'UP'))
try_get(self.chassis.get_module(module_index).set_admin_state, admin_state, default=False)

#
# Module Updater ==============================================================
#


class ModuleUpdater(logger.Logger):

def __init__(self, log_identifier, chassis):
"""
Constructor for ModuleUpdater
:param chassis: Object representing a platform chassis
"""
super(ModuleUpdater, self).__init__(log_identifier)

self.chassis = chassis
self.num_modules = chassis.get_num_modules()
# Connect to STATE_DB and create chassis info tables
state_db = daemon_base.db_connect("STATE_DB")
self.chassis_table = swsscommon.Table(state_db, CHASSIS_INFO_TABLE)
self.module_table = swsscommon.Table(state_db, CHASSIS_MODULE_INFO_TABLE)
self.info_dict_keys = [CHASSIS_MODULE_INFO_NAME_FIELD,
CHASSIS_MODULE_INFO_DESC_FIELD,
CHASSIS_MODULE_INFO_SLOT_FIELD,
CHASSIS_MODULE_INFO_OPERSTATUS_FIELD]
"""
Constructor for ModuleUpdater
:param chassis: Object representing a platform chassis
"""
super(ModuleUpdater, self).__init__(log_identifier)

self.chassis = chassis
self.num_modules = chassis.get_num_modules()
# Connect to STATE_DB and create chassis info tables
state_db = daemon_base.db_connect("STATE_DB")
self.chassis_table = swsscommon.Table(state_db, CHASSIS_INFO_TABLE)
self.module_table = swsscommon.Table(state_db, CHASSIS_MODULE_INFO_TABLE)
self.info_dict_keys = [CHASSIS_MODULE_INFO_NAME_FIELD,
CHASSIS_MODULE_INFO_DESC_FIELD,
CHASSIS_MODULE_INFO_SLOT_FIELD,
CHASSIS_MODULE_INFO_OPERSTATUS_FIELD]

def deinit(self):
"""
Expand Down Expand Up @@ -182,12 +188,14 @@ class ModuleUpdater(logger.Logger):
not key.startswith(ModuleBase.MODULE_TYPE_LINE) and \
not key.startswith(ModuleBase.MODULE_TYPE_FABRIC):
self.log_error("Incorrect module-name {}. Should start with {} or {} or {}".format(key,
ModuleBase.MODULE_TYPE_SUPERVISOR, ModuleBase.MODULE_TYPE_LINE,
ModuleBase.MODULE_TYPE_FABRIC))
ModuleBase.MODULE_TYPE_SUPERVISOR,
ModuleBase.MODULE_TYPE_LINE,
ModuleBase.MODULE_TYPE_FABRIC))
continue

fvs = swsscommon.FieldValuePairs([(CHASSIS_MODULE_INFO_DESC_FIELD, module_info_dict[CHASSIS_MODULE_INFO_DESC_FIELD]),
(CHASSIS_MODULE_INFO_SLOT_FIELD, module_info_dict[CHASSIS_MODULE_INFO_SLOT_FIELD]),
(CHASSIS_MODULE_INFO_SLOT_FIELD,
module_info_dict[CHASSIS_MODULE_INFO_SLOT_FIELD]),
(CHASSIS_MODULE_INFO_OPERSTATUS_FIELD, module_info_dict[CHASSIS_MODULE_INFO_OPERSTATUS_FIELD])])
self.module_table.set(key, fvs)

Expand All @@ -200,7 +208,8 @@ class ModuleUpdater(logger.Logger):
name = try_get(self.chassis.get_module(module_index).get_name)
desc = try_get(self.chassis.get_module(module_index).get_description)
slot = try_get(self.chassis.get_module(module_index).get_slot, default=INVALID_SLOT)
status = try_get(self.chassis.get_module(module_index).get_oper_status, default=ModuleBase.MODULE_STATUS_OFFLINE)
status = try_get(self.chassis.get_module(module_index).get_oper_status,
default=ModuleBase.MODULE_STATUS_OFFLINE)

module_info_dict[CHASSIS_MODULE_INFO_NAME_FIELD] = name
module_info_dict[CHASSIS_MODULE_INFO_DESC_FIELD] = str(desc)
Expand All @@ -212,6 +221,8 @@ class ModuleUpdater(logger.Logger):
#
# Config Manager task ========================================================
#


class ConfigManagerTask(ProcessTaskBase):
def __init__(self):
ProcessTaskBase.__init__(self)
Expand Down Expand Up @@ -256,6 +267,7 @@ class ConfigManagerTask(ProcessTaskBase):
# Daemon =======================================================================
#


class ChassisdDaemon(daemon_base.DaemonBase):
def __init__(self, log_identifier):
super(ChassisdDaemon, self).__init__(log_identifier)
Expand Down Expand Up @@ -309,7 +321,7 @@ class ChassisdDaemon(daemon_base.DaemonBase):
self.log_info("Start daemon main loop")

while not self.stop.wait(CHASSIS_INFO_UPDATE_PERIOD_SECS):
self.module_updater.module_db_update()
self.module_updater.module_db_update()

self.log_info("Stop daemon main loop")

Expand All @@ -325,9 +337,11 @@ class ChassisdDaemon(daemon_base.DaemonBase):
# Main =========================================================================
#


def main():
chassisd = ChassisdDaemon(SYSLOG_IDENTIFIER)
chassisd.run()


if __name__ == '__main__':
main()
4 changes: 2 additions & 2 deletions sonic-chassisd/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
scripts=[
'scripts/chassisd',
],
setup_requires= [
setup_requires=[
'pytest-runner',
'wheel'
],
tests_require = [
tests_require=[
'pytest',
'mock>=2.0.0',
'pytest-cov'
Expand Down
10 changes: 5 additions & 5 deletions sonic-chassisd/tests/mock_module_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ class ModuleBase():

# Possible card types for modular chassis
MODULE_TYPE_SUPERVISOR = "SUPERVISOR"
MODULE_TYPE_LINE = "LINE-CARD"
MODULE_TYPE_FABRIC = "FABRIC-CARD"
MODULE_TYPE_LINE = "LINE-CARD"
MODULE_TYPE_FABRIC = "FABRIC-CARD"

# Possible card status for modular chassis
# Module state is Empty if no module is inserted in the slot
MODULE_STATUS_EMPTY = "Empty"
MODULE_STATUS_EMPTY = "Empty"
# Module state if Offline if powered down. This is also the admin-down state.
MODULE_STATUS_OFFLINE = "Offline"
# Module state is Present when it is powered up, but not fully functional.
MODULE_STATUS_PRESENT = "Present"
# Module state is Present when it is powered up, but entered a fault state.
# Module is not able to go Online.
MODULE_STATUS_FAULT = "Fault"
MODULE_STATUS_FAULT = "Fault"
# Module state is Online when fully operational
MODULE_STATUS_ONLINE = "Online"
MODULE_STATUS_ONLINE = "Online"
1 change: 1 addition & 0 deletions sonic-chassisd/tests/mock_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def set_admin_state(self, up):
def get_admin_state(self):
return self.admin_state


class MockChassis:
def __init__(self):
self.module_list = []
Expand Down
5 changes: 3 additions & 2 deletions sonic-chassisd/tests/mock_swsscommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

class Table:
def __init__(self, db, table_name):
self.table_name = table_name
self.mock_dict = {}
self.table_name = table_name
self.mock_dict = {}

def _del(self, key):
del self.mock_dict[key]
Expand All @@ -19,6 +19,7 @@ def get(self, key):
return self.mock_dict[key]
return None


class FieldValuePairs:
def __init__(self, fvs):
self.fv_dict = dict(fvs)
Expand Down
8 changes: 8 additions & 0 deletions sonic-chassisd/tests/test_chassisd.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
CHASSIS_INFO_KEY_TEMPLATE = 'CHASSIS {}'
CHASSIS_INFO_CARD_NUM_FIELD = 'module_num'


def setup_function():
ModuleUpdater.log_notice = MagicMock()
ModuleUpdater.log_warning = MagicMock()
Expand Down Expand Up @@ -63,6 +64,7 @@ def test_moduleupdater_check_valid_fields():
assert slot == int(fvs[CHASSIS_MODULE_INFO_SLOT_FIELD])
assert status == fvs[CHASSIS_MODULE_INFO_OPERSTATUS_FIELD]


def test_moduleupdater_check_invalid_name():
chassis = MockChassis()
index = 0
Expand All @@ -83,6 +85,7 @@ def test_moduleupdater_check_invalid_name():
fvs = module_updater.module_table.get(name)
assert fvs == None


def test_moduleupdater_check_status_update():
chassis = MockChassis()
index = 0
Expand Down Expand Up @@ -116,6 +119,7 @@ def test_moduleupdater_check_status_update():
print('Updated DB-entry {}'.format(fvs))
assert status == fvs[CHASSIS_MODULE_INFO_OPERSTATUS_FIELD]


def test_moduleupdater_check_deinit():
chassis = MockChassis()
index = 0
Expand All @@ -141,6 +145,7 @@ def test_moduleupdater_check_deinit():
fvs = module_table.get(name)
assert fvs == None


def test_configupdater_check_valid_names():
chassis = MockChassis()
index = 0
Expand All @@ -162,6 +167,7 @@ def test_configupdater_check_valid_names():
# No change since invalid key
assert module.get_admin_state() != admin_state


def test_configupdater_check_valid_index():
chassis = MockChassis()
index = -1
Expand All @@ -183,6 +189,7 @@ def test_configupdater_check_valid_index():
# No change since invalid index
assert module.get_admin_state() != admin_state


def test_configupdater_check_admin_state():
chassis = MockChassis()
index = 0
Expand All @@ -206,6 +213,7 @@ def test_configupdater_check_admin_state():
config_updater.module_config_update(name, admin_state)
assert module.get_admin_state() == admin_state


def test_configupdater_check_num_modules():
chassis = MockChassis()
index = 0
Expand Down
11 changes: 7 additions & 4 deletions sonic-ledd/scripts/ledd
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ try:
from sonic_py_common.interface import backplane_prefix
from swsscommon import swsscommon
except ImportError as e:
raise ImportError (str(e) + " - required module not found")
raise ImportError(str(e) + " - required module not found")

#============================= Constants =============================

VERSION = '1.0'

SYSLOG_IDENTIFIER = "ledd"

USAGE_HELP="""
USAGE_HELP = """
Usage: ledd [options]
Options:
Expand All @@ -37,6 +37,7 @@ SELECT_TIMEOUT = 1000

LEDUTIL_LOAD_ERROR = 1


class DaemonLedd(daemon_base.DaemonBase):

# Run daemon
Expand All @@ -45,8 +46,8 @@ class DaemonLedd(daemon_base.DaemonBase):
if (len(sys.argv) > 1):
try:
(options, remainder) = getopt.getopt(sys.argv[1:],
'hv',
['help', 'version'])
'hv',
['help', 'version'])
except getopt.GetoptError as e:
print(e)
print(USAGE_HELP)
Expand Down Expand Up @@ -118,9 +119,11 @@ class DaemonLedd(daemon_base.DaemonBase):

return 1


def main():
ledd = DaemonLedd(SYSLOG_IDENTIFIER)
ledd.run()


if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion sonic-ledd/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
scripts=[
'scripts/ledd',
],
setup_requires= [
setup_requires=[
'wheel'
],
classifiers=[
Expand Down
1 change: 1 addition & 0 deletions sonic-pcied/scripts/pcied
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,6 @@ def main():
pcied = DaemonPcied(SYSLOG_IDENTIFIER)
pcied.run()


if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion sonic-pcied/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
scripts=[
'scripts/pcied',
],
setup_requires= [
setup_requires=[
'wheel'
],
classifiers=[
Expand Down
Loading

0 comments on commit 4da0bfc

Please sign in to comment.