daemon: added class variable type hinting to core.services.coreservices

This commit is contained in:
Blake Harnden 2020-06-13 22:25:38 -07:00
parent 91f1f7f004
commit d94bae6b42

View file

@ -10,7 +10,7 @@ services.
import enum import enum
import logging import logging
import time import time
from typing import TYPE_CHECKING, Iterable, List, Tuple, Type from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Set, Tuple, Type
from core import utils from core import utils
from core.constants import which from core.constants import which
@ -36,14 +36,15 @@ class ServiceMode(enum.Enum):
class ServiceDependencies: class ServiceDependencies:
""" """
Can generate boot paths for services, based on their dependencies. Will validate Can generate boot paths for services, based on their dependencies. Will validate
that all services will be booted and that all dependencies exist within the services provided. that all services will be booted and that all dependencies exist within the services
provided.
""" """
def __init__(self, services: List[Type["CoreService"]]) -> None: def __init__(self, services: List["CoreService"]) -> None:
# helpers to check validity # helpers to check validity
self.dependents = {} self.dependents: Dict[str, Set[str]] = {}
self.booted = set() self.booted: Set[str] = set()
self.node_services = {} self.node_services: Dict[str, "CoreService"] = {}
for service in services: for service in services:
self.node_services[service.name] = service self.node_services[service.name] = service
for dependency in service.dependencies: for dependency in service.dependencies:
@ -51,9 +52,9 @@ class ServiceDependencies:
dependents.add(service.name) dependents.add(service.name)
# used to find paths # used to find paths
self.path = [] self.path: List["CoreService"] = []
self.visited = set() self.visited: Set[str] = set()
self.visiting = set() self.visiting: Set[str] = set()
def boot_paths(self) -> List[List["CoreService"]]: def boot_paths(self) -> List[List["CoreService"]]:
""" """
@ -131,7 +132,7 @@ class ServiceDependencies:
class ServiceShim: class ServiceShim:
keys = [ keys: List[str] = [
"dirs", "dirs",
"files", "files",
"startidx", "startidx",
@ -241,10 +242,10 @@ class ServiceManager:
Manages services available for CORE nodes to use. Manages services available for CORE nodes to use.
""" """
services = {} services: Dict[str, Type["CoreService"]] = {}
@classmethod @classmethod
def add(cls, service: "CoreService") -> None: def add(cls, service: Type["CoreService"]) -> None:
""" """
Add a service to manager. Add a service to manager.
@ -314,8 +315,8 @@ class CoreServices:
custom service configuration. A CoreService is not a Configurable. custom service configuration. A CoreService is not a Configurable.
""" """
name = "services" name: str = "services"
config_type = RegisterTlvs.UTILITY config_type: RegisterTlvs = RegisterTlvs.UTILITY
def __init__(self, session: "Session") -> None: def __init__(self, session: "Session") -> None:
""" """
@ -323,17 +324,17 @@ class CoreServices:
:param session: session this manager is tied to :param session: session this manager is tied to
""" """
self.session = session self.session: "Session" = session
# dict of default services tuples, key is node type # dict of default services tuples, key is node type
self.default_services = { self.default_services: Dict[str, List[str]] = {
"mdr": ("zebra", "OSPFv3MDR", "IPForward"), "mdr": ["zebra", "OSPFv3MDR", "IPForward"],
"PC": ("DefaultRoute",), "PC": ["DefaultRoute"],
"prouter": (), "prouter": [],
"router": ("zebra", "OSPFv2", "OSPFv3", "IPForward"), "router": ["zebra", "OSPFv2", "OSPFv3", "IPForward"],
"host": ("DefaultRoute", "SSH"), "host": ["DefaultRoute", "SSH"],
} }
# dict of node ids to dict of custom services by name # dict of node ids to dict of custom services by name
self.custom_services = {} self.custom_services: Dict[int, Dict[str, "CoreService"]] = {}
def reset(self) -> None: def reset(self) -> None:
""" """
@ -425,7 +426,7 @@ class CoreServices:
continue continue
node.services.append(service) node.services.append(service)
def all_configs(self) -> List[Tuple[int, Type["CoreService"]]]: def all_configs(self) -> List[Tuple[int, "CoreService"]]:
""" """
Return (node_id, service) tuples for all stored configs. Used when reconnecting Return (node_id, service) tuples for all stored configs. Used when reconnecting
to a session or opening XML. to a session or opening XML.
@ -808,50 +809,50 @@ class CoreService:
""" """
# service name should not include spaces # service name should not include spaces
name = None name: Optional[str] = None
# executables that must exist for service to run # executables that must exist for service to run
executables = () executables: Tuple[str, ...] = ()
# sets service requirements that must be started prior to this service starting # sets service requirements that must be started prior to this service starting
dependencies = () dependencies: Tuple[str, ...] = ()
# group string allows grouping services together # group string allows grouping services together
group = None group: Optional[str] = None
# private, per-node directories required by this service # private, per-node directories required by this service
dirs = () dirs: Tuple[str, ...] = ()
# config files written by this service # config files written by this service
configs = () configs: Tuple[str, ...] = ()
# config file data # config file data
config_data = {} config_data: Dict[str, str] = {}
# list of startup commands # list of startup commands
startup = () startup: Tuple[str, ...] = ()
# list of shutdown commands # list of shutdown commands
shutdown = () shutdown: Tuple[str, ...] = ()
# list of validate commands # list of validate commands
validate = () validate: Tuple[str, ...] = ()
# validation mode, used to determine startup success # validation mode, used to determine startup success
validation_mode = ServiceMode.NON_BLOCKING validation_mode: ServiceMode = ServiceMode.NON_BLOCKING
# time to wait in seconds for determining if service started successfully # time to wait in seconds for determining if service started successfully
validation_timer = 5 validation_timer: int = 5
# validation period in seconds, how frequent validation is attempted # validation period in seconds, how frequent validation is attempted
validation_period = 0.5 validation_period: float = 0.5
# metadata associated with this service # metadata associated with this service
meta = None meta: Optional[str] = None
# custom configuration text # custom configuration text
custom = False custom: bool = False
custom_needed = False custom_needed: bool = False
def __init__(self) -> None: def __init__(self) -> None:
""" """
@ -859,8 +860,8 @@ class CoreService:
against their config. Services are instantiated when a custom against their config. Services are instantiated when a custom
configuration is used to override their default parameters. configuration is used to override their default parameters.
""" """
self.custom = True self.custom: bool = True
self.config_data = self.__class__.config_data.copy() self.config_data: Dict[str, str] = self.__class__.config_data.copy()
@classmethod @classmethod
def on_load(cls) -> None: def on_load(cls) -> None: