Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class decorator to dynamically add new classes and supported device_type #3274

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions netmiko/ssh_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,41 @@
telnet_platforms_str = "\n" + telnet_platforms_str


def register_as(platform_name: str):
"""
Decorator to register a class as a platform

Ex:
from netmiko.ssh_dispatcher import register_as
from netmiko.base_connection import BaseConnection

@register_as('my_driver')
class MyDriver(BaseConnection):
# custom code and method overrides
pass
"""
def register(cls: Type["BaseConnection"]) -> Type["BaseConnection"]:
global CLASS_MAPPER, platforms, platforms_base, platforms_str, telnet_platforms, telnet_platforms_str
CLASS_MAPPER[platform_name] = cls
platforms = list(CLASS_MAPPER.keys())
platforms.sort()
platforms_base = list(CLASS_MAPPER_BASE.keys())
platforms_base.sort()
platforms_str = "\n".join(platforms_base)
platforms_str = "\n" + platforms_str

scp_platforms = list(FILE_TRANSFER_MAP.keys())
scp_platforms.sort()
scp_platforms_str = "\n".join(scp_platforms)
scp_platforms_str = "\n" + scp_platforms_str

telnet_platforms = [x for x in platforms if "telnet" in x]
telnet_platforms_str = "\n".join(telnet_platforms)
telnet_platforms_str = "\n" + telnet_platforms_str
return cls
return register


def ConnectHandler(*args: Any, **kwargs: Any) -> "BaseConnection":
"""Factory function selects the proper class and creates object based on device_type."""
device_type = kwargs["device_type"]
Expand Down
Loading