Skip to content

Commit

Permalink
Address linting issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
progala committed Aug 8, 2023
1 parent 841070e commit acd43e4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
10 changes: 7 additions & 3 deletions netutils/nist.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Functions building NIST URLs from the os platform values."""
import re
import typing as t


def get_nist_urls_juniper_junos(os_platform_data: dict, api_key: str) -> t.List[str]:
def get_nist_urls_juniper_junos(os_platform_data: t.Dict[str, t.Any], api_key: str) -> t.List[str]:
"""Create a list of possible NIST Url strings for JuniperPlatform.
Args:
Expand Down Expand Up @@ -79,7 +80,7 @@ def get_nist_urls_juniper_junos(os_platform_data: dict, api_key: str) -> t.List[
raise []


def get_nist_urls_default(os_platform_data: dict, api_key: str) -> t.List[str]:
def get_nist_urls_default(os_platform_data: t.Dict[str, t.Any], api_key: str) -> t.List[str]:
r"""Create a list of possible NIST Url strings.
Child models with NIST URL customizations need their own "get_nist_urls" method.
Expand Down Expand Up @@ -107,4 +108,7 @@ def get_nist_urls_default(os_platform_data: dict, api_key: str) -> t.List[str]:
return nist_urls


get_nist_url_funcs = {"default": get_nist_urls_default, "juniper": {"junos": get_nist_urls_juniper_junos}}
get_nist_url_funcs: t.Dict[str, t.Any] = {
"default": get_nist_urls_default,
"juniper": {"junos": get_nist_urls_juniper_junos},
}
19 changes: 10 additions & 9 deletions netutils/os_version_parser.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Functions used for parsing os platform version."""
import re
import typing as t


def juniper_junos_version_parser(version: str) -> t.Dict:
def juniper_junos_version_parser(version: str) -> t.Dict[str, t.Any]:
"""Parses JunOS Version into usable bits matching JunOS Standars.
Args:
Expand All @@ -16,7 +17,7 @@ def juniper_junos_version_parser(version: str) -> t.Dict:
"""
# Use regex to group the main, minor, type and build into useable pieces
# re_main_minor_type_build = re.search(r"^(\d+)\.(\d+)([xXrRsS])?(\d+)?", split_version[0])
re_main_minor_type_build = re.compile(
re_main_minor_type_build: re.Pattern[str] = re.compile(
r"""
^
(?P<main>\d+) # main train
Expand All @@ -27,18 +28,18 @@ def juniper_junos_version_parser(version: str) -> t.Dict:
""",
re.VERBOSE,
)
re_service_build_respin = re.compile(
re_service_build_respin: re.Pattern[str] = re.compile(
r"""
(?P<service>[sSdD])? # service (optional)
(?P<service_build>\d+)? # service build (optional)
\.?
\.?
(?P<service_respin>\d+)? # service respin (optional)
""",
re.VERBOSE,
)
# Set empty params for service pieces and complete them if a second indice exists from the version split
# Define isservice, isfrs, isspecial, ismaintenance
parsed_version = {
parsed_version: t.Dict[str, t.Any] = {
"isservice": False,
"ismaintenance": False,
"isfrs": False,
Expand All @@ -52,14 +53,14 @@ def juniper_junos_version_parser(version: str) -> t.Dict:
version_core_part, *version_service_part = re.split("-|:", version)

# Parse out junos into sections that can be used for logic
parsed_version.update(re_main_minor_type_build.search(version_core_part).groupdict())
parsed_version.update(re_main_minor_type_build.search(version_core_part).groupdict()) # type:ignore

if version_service_part:
parsed_version.update(re_service_build_respin.search(version_service_part[0]).groupdict())
parsed_version.update(re_service_build_respin.search(version_service_part[0]).groupdict()) # type:ignore
if parsed_version.get("service", "").lower() == "s":
parsed_version["isservice"] = True
# Juniper looks at the D in special releases like it's the R in normal releases; Use it as the frs identifier
elif parsed_version.get("service").lower() == "d" and (
elif parsed_version.get("service").lower() == "d" and ( # type:ignore
parsed_version.get("service_build") is None or int(parsed_version.get("service_build", 1)) <= 1
):
parsed_version["isfrs"] = True
Expand All @@ -73,7 +74,7 @@ def juniper_junos_version_parser(version: str) -> t.Dict:
parsed_version["isservice"] = True

if parsed_version["type"].lower() == "r" and (
parsed_version.get("build") is None or int(parsed_version.get("build")) <= 1
parsed_version.get("build") is None or int(parsed_version.get("build")) <= 1 # type:ignore
):
parsed_version["isfrs"] = True
elif parsed_version["type"].lower() == "r":
Expand Down
26 changes: 17 additions & 9 deletions netutils/platform_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
# The intent of this script is to take a given platform, determine the format, and reformat it for another purpose
# An example of this is a platform being formatted for NIST Database Query
import dataclasses
import typing as t

from netutils.nist import get_nist_url_funcs
from netutils.os_version_parser import os_version_parsers

PLATFORM_FIELDS = {
PLATFORM_FIELDS: t.Dict[str, t.Any] = {
"default": [
("vendor", str),
("os_type", str),
Expand All @@ -32,20 +33,27 @@

@dataclasses.dataclass
class OsPlatform:
"""Base class for dynamically generated vendor specific platform data classes."""

@property
def asdict(self):
def asdict(self) -> t.Dict[str, t.Any]:
"""Returns dictionary representation of the class attributes."""
return dataclasses.asdict(self)

def get_nist_urls(self, api_key):
return self.get_nist_urls_fn(api_key)
def get_nist_urls(self, api_key: str) -> t.List[str]:
"""Returns list of NIST URLs for the platform."""
return self.get_nist_urls_fn(api_key) # type: ignore

def get(self, key):
return self.__getitem__(key)
def get(self, key: str) -> t.Any:
"""Return value of the attribute matching provided name or None if no attribute is found."""
return getattr(self, key, None)

def keys(self):
def keys(self) -> t.KeysView[t.Any]:
"""Return attributes and their values as dict keys."""
return self.__annotations__.keys()

def __getitem__(self, key):
def __getitem__(self, key: str) -> t.Any:
"""Allow retrieving attributes using subscript notation."""
return getattr(self, key)


Expand Down Expand Up @@ -81,7 +89,7 @@ def os_platform_object_builder(vendor: str, platform: str, version: str) -> obje
base_class = OsPlatform
class_name = f"{vendor.capitalize()}{platform.capitalize()}"
get_nist_urls_fn = get_nist_url_funcs.get(vendor, {}).get(platform, None) or get_nist_url_funcs["default"]
base_class.get_nist_urls_fn = get_nist_urls_fn
base_class.get_nist_urls_fn = get_nist_urls_fn # type: ignore

platform_cls = dataclasses.make_dataclass(
cls_name=class_name,
Expand Down

0 comments on commit acd43e4

Please sign in to comment.