add docstrings to the router classes

This commit is contained in:
Bastian Schroll 2019-03-04 20:10:49 +01:00
parent 54676f9f06
commit 05166cfdca
3 changed files with 37 additions and 1 deletions

View file

@ -17,14 +17,22 @@
class Route: class Route:
"""!Class for single routing points"""
def __init__(self, name, callback): def __init__(self, name, callback):
"""!Create a instance of an route point
@param name: name of the route point
@param callback: instance of the callback function
"""
self._name = name self._name = name
self._callback = callback self._callback = callback
@property @property
def name(self): def name(self):
"""!Property to get the route point name"""
return self._name return self._name
@property @property
def callback(self): def callback(self):
"""!Porperty to get the callback function instance"""
return self._callback return self._callback

View file

@ -21,16 +21,29 @@ logging.debug("- %s loaded", __name__)
class Router: class Router:
"""!Class for the Router"""
def __init__(self, name): def __init__(self, name):
"""!Create a new router
@param name: name of the router"""
self._name = name self._name = name
self._routeList = [] self._routeList = []
logging.debug("[%s] new router", self._name) logging.debug("[%s] new router", self._name)
def addRoute(self, route): def addRoute(self, route):
"""!Adds a route point to the router
@param route: instance of the Route class
"""
logging.debug("[%s] add route: %s", self._name, route.name) logging.debug("[%s] add route: %s", self._name, route.name)
self._routeList.append(route) self._routeList.append(route)
def runRouter(self, bwPacket): def runRouter(self, bwPacket):
"""!Run the router
@param bwPacket: instance of Packet class
@return a instance of Packet class
"""
logging.debug("[%s] started", self._name) logging.debug("[%s] started", self._name)
for routeObject in self._routeList: for routeObject in self._routeList:
logging.debug("[%s] -> run route: %s", self._name, routeObject) logging.debug("[%s] -> run route: %s", self._name, routeObject)
@ -50,8 +63,10 @@ class Router:
@property @property
def name(self): def name(self):
"""!Property to get the name of the router"""
return self._name return self._name
@property @property
def routeList(self): def routeList(self):
"""!Property to get a list of all route points of this router"""
return self._routeList return self._routeList

View file

@ -15,7 +15,6 @@
@description: Class for the BOSWatch packet router manager class @description: Class for the BOSWatch packet router manager class
""" """
# todo think about implement threading for routers and the plugin calls (THREAD SAFETY!!!) # todo think about implement threading for routers and the plugin calls (THREAD SAFETY!!!)
import logging import logging
import importlib import importlib
@ -27,15 +26,24 @@ logging.debug("- %s loaded", __name__)
class RouterManager: class RouterManager:
"""!Class to manage all routers"""
def __init__(self): def __init__(self):
"""!Create new router"""
self._routerDict = {} self._routerDict = {}
def __del__(self): def __del__(self):
"""!Destroy the internal routerDict
All routers and route point instances will be destroyed too
Also destroys all instances from modules or plugins"""
# destroy all routers (also destroys all instances of modules/plugins) # destroy all routers (also destroys all instances of modules/plugins)
del self._routerDict del self._routerDict
# if there is an error, router list would be empty (see tmp variable) # if there is an error, router list would be empty (see tmp variable)
def buildRouter(self, config): def buildRouter(self, config):
"""!Initialize Routers from given config file
@param config: instance of ConfigYaml class
@return True or False"""
self._routerDict = {} # all routers and instances of modules/plugins would be destroyed self._routerDict = {} # all routers and instances of modules/plugins would be destroyed
routerDict_tmp = {} routerDict_tmp = {}
logging.debug("build routers") logging.debug("build routers")
@ -91,6 +99,10 @@ class RouterManager:
return True return True
def runRouter(self, routerRunList, bwPacket): def runRouter(self, routerRunList, bwPacket):
"""!Run given Routers
@param routerRunList: string or list of router names in string form
@param bwPacket: instance of Packet class"""
if type(routerRunList) is str: # convert single string name to list if type(routerRunList) is str: # convert single string name to list
routerRunList = [routerRunList] routerRunList = [routerRunList]
@ -99,6 +111,7 @@ class RouterManager:
self._routerDict[routerName].runRouter(bwPacket) self._routerDict[routerName].runRouter(bwPacket)
def _showRouterRoute(self): def _showRouterRoute(self):
"""!Show the routes of all routers"""
for name, routerObject in self._routerDict.items(): for name, routerObject in self._routerDict.items():
logging.debug("Route for %s", name) logging.debug("Route for %s", name)
counter = 0 counter = 0