Skip to content

Commit

Permalink
Fix bug with get_nist_urls fun assignment. Rename object factory.
Browse files Browse the repository at this point in the history
  • Loading branch information
progala committed Aug 10, 2023
1 parent acd43e4 commit 5e5084d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/user/include_jinja_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
15 changes: 5 additions & 10 deletions netutils/platform_mapper.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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)

0 comments on commit 5e5084d

Please sign in to comment.