From 5e5084d60e2b4e781cd9213fd40a306b64411d1e Mon Sep 17 00:00:00 2001 From: Przemek Rogala Date: Thu, 10 Aug 2023 11:14:49 +0100 Subject: [PATCH] Fix bug with get_nist_urls fun assignment. Rename object factory. --- docs/user/include_jinja_list.md | 2 +- netutils/platform_mapper.py | 15 +++++---------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/docs/user/include_jinja_list.md b/docs/user/include_jinja_list.md index 68827c04..9e95a3ac 100644 --- a/docs/user/include_jinja_list.md +++ b/docs/user/include_jinja_list.md @@ -59,7 +59,7 @@ | encrypt_type7 | netutils.password.encrypt_type7 | | get_hash_salt | netutils.password.get_hash_salt | | tcp_ping | netutils.ping.tcp_ping | -| create_platform_object | netutils.platform_mapper.create_platform_object | +| os_platform_object_builder | netutils.platform_mapper.os_platform_object_builder | | longest_prefix_match | netutils.route.longest_prefix_match | | uptime_seconds_to_string | netutils.time.uptime_seconds_to_string | | uptime_string_to_seconds | netutils.time.uptime_string_to_seconds | diff --git a/netutils/platform_mapper.py b/netutils/platform_mapper.py index 968d8a73..d8e7ae71 100644 --- a/netutils/platform_mapper.py +++ b/netutils/platform_mapper.py @@ -1,6 +1,7 @@ """Platform Mappers.""" # 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 abc import dataclasses import typing as t @@ -31,18 +32,16 @@ } -@dataclasses.dataclass -class OsPlatform: +class OsPlatform(metaclass=abc.ABCMeta): """Base class for dynamically generated vendor specific platform data classes.""" - @property def asdict(self) -> t.Dict[str, t.Any]: """Returns dictionary representation of the class attributes.""" return dataclasses.asdict(self) + @abc.abstractmethod 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: str) -> t.Any: """Return value of the attribute matching provided name or None if no attribute is found.""" @@ -86,15 +85,11 @@ def os_platform_object_builder(vendor: str, platform: str, version: str) -> obje if version_parser: field_values.update(version_parser(version)) - 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 # type: ignore + get_nist_urls_func = get_nist_url_funcs.get(vendor, {}).get(platform, None) or get_nist_url_funcs["default"] platform_cls = dataclasses.make_dataclass( - cls_name=class_name, - fields=class_fields, - bases=(OsPlatform,), + cls_name=class_name, fields=class_fields, bases=(OsPlatform,), namespace={"get_nist_urls": get_nist_urls_func} ) return platform_cls(**field_values)