Skip to content

Commit

Permalink
Added compatibility layer.
Browse files Browse the repository at this point in the history
lextm committed Oct 13, 2024
1 parent 4c71d45 commit 7e708dc
Showing 21 changed files with 446 additions and 61 deletions.
23 changes: 23 additions & 0 deletions pysnmp/carrier/asyncio/dgram/base.py
Original file line number Diff line number Diff line change
@@ -74,6 +74,7 @@
import sys
import traceback
from socket import socket
import warnings

from pysnmp import debug
from pysnmp.carrier import error
@@ -215,3 +216,25 @@ def normalize_address(
if not isinstance(transportAddress, self.ADDRESS_TYPE):
transportAddress = self.ADDRESS_TYPE(transportAddress)
return transportAddress

# Old to new attribute mapping
deprecated_attributes = {
"openClientMode": "open_client_mode",
"openServerMode": "open_server_mode",
"closeTransport": "close_transport",
"sendMessage": "send_message",
"normalizeAddr": "normalize_address",
}

def __getattr__(self, attr: str):
if new_attr := self.deprecated_attributes.get(attr):
warnings.warn(
f"{attr} is deprecated. Please use {new_attr} instead.",
DeprecationWarning,
stacklevel=2,
)
return getattr(self, new_attr)

raise AttributeError(
f"class '{self.__class__.__name__}' has no attribute '{attr}'"
)
23 changes: 23 additions & 0 deletions pysnmp/carrier/asyncio/dispatch.py
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@
import traceback
from time import time
from typing import Tuple
import warnings

from pysnmp.carrier.base import AbstractTransport, AbstractTransportDispatcher
from pysnmp.error import PySnmpError
@@ -101,3 +102,25 @@ def unregister_transport(self, tDomain: Tuple[int, ...]):
if self.__transport_count == 0 and not self.loopingcall.done():
self.loopingcall.cancel()
self.loopingcall = None

# compatibility with legacy code
# Old to new attribute mapping
deprecated_attributes = {
"jobStarted": "job_started",
"jobFinished": "job_finished",
"runDispatcher": "run_dispatcher",
"registerTransport": "register_transport",
"closeDispatcher": "close_dispatcher",
}

def __getattr__(self, attr: str):
if new_attr := self.deprecated_attributes.get(attr):
warnings.warn(
f"{attr} is deprecated. Please use {new_attr} instead.",
DeprecationWarning,
stacklevel=2,
)
return getattr(self, new_attr)
raise AttributeError(
f"'{self.__class__.__name__}' object has no attribute '{attr}'"
)
12 changes: 12 additions & 0 deletions pysnmp/entity/config.py
Original file line number Diff line number Diff line change
@@ -44,6 +44,18 @@
"usmKeyTypeLocalized": "USM_KEY_TYPE_LOCALIZED",
"authServices": "AUTH_SERVICES",
"privServices": "PRIV_SERVICES",
"addV1System": "add_v1_system",
"delV1System": "delete_v1_system",
"addV3User": "add_v3_user",
"delV3User": "delete_v3_user",
"addTransport": "add_transport",
"delTransport": "delete_transport",
"addTargetParams": "add_target_parameters",
"delTargetParams": "delete_target_parameters",
"addTargetAddr": "add_target_address",
"delTargetAddr": "delete_target_address",
"addVacmUser": "add_vacm_user",
"delVacmUser": "delete_vacm_user",
}


22 changes: 22 additions & 0 deletions pysnmp/entity/engine.py
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
import sys
import tempfile
from typing import Any, Dict
import warnings


from pyasn1.type import univ
@@ -279,3 +280,24 @@ def delete_user_context(self, arg):
del self.cache["__%s" % arg]
except KeyError:
pass

# compatibility with legacy code
# Old to new attribute mapping
deprecated_attributes = {
"transportDispatcher": "transport_dispatcher",
"openDispatcher": "open_dispatcher",
"closeDispatcher": "close_dispatcher",
"msgAndPduDsp": "message_dispatcher",
}

def __getattr__(self, attr: str):
if new_attr := self.deprecated_attributes.get(attr):
warnings.warn(
f"{attr} is deprecated. Please use {new_attr} instead.",
DeprecationWarning,
stacklevel=2,
)
return getattr(self, new_attr)
raise AttributeError(
f"'{self.__class__.__name__}' object has no attribute '{attr}'"
)
19 changes: 19 additions & 0 deletions pysnmp/entity/rfc3413/context.py
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
# Copyright (c) 2005-2020, Ilya Etingof <etingof@gmail.com>
# License: https://www.pysnmp.com/pysnmp/license.html
#
import warnings
from pyasn1.type import univ
from pysnmp import debug, error
from pysnmp.entity.engine import SnmpEngine
@@ -69,3 +70,21 @@ def get_mib_instrum(self, contextName: bytes = b"") -> MibInstrumController:
f"getMibInstrum: contextName {contextName!r}, mibInstum {self.context_names[contextName]!r}"
)
return self.context_names[contextName]

# compatibility with legacy code
# Old to new attribute mapping
deprecated_attributes = {
"getMibInstrum": "get_mib_instrum",
}

def __getattr__(self, attr: str):
if new_attr := self.deprecated_attributes.get(attr):
warnings.warn(
f"{attr} is deprecated. Please use {new_attr} instead.",
DeprecationWarning,
stacklevel=2,
)
return getattr(self, new_attr)
raise AttributeError(
f"'{self.__class__.__name__}' object has no attribute '{attr}'"
)
44 changes: 29 additions & 15 deletions pysnmp/hlapi/v1arch/asyncio/cmdgen.py
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
#
import asyncio
from typing import AsyncGenerator
import warnings

from pysnmp.hlapi import varbinds
from pysnmp.hlapi.transport import AbstractTransportTarget
@@ -43,7 +44,7 @@ async def get_cmd(
authData: CommunityData,
transportTarget: AbstractTransportTarget,
*varBinds: ObjectType,
**options
**options,
) -> "tuple[errind.ErrorIndication | None, Integer32 | str | int | None, Integer32 | int | None, tuple[ObjectType, ...]]":
r"""Creates a generator to perform SNMP GET query.
@@ -174,7 +175,7 @@ async def set_cmd(
authData: CommunityData,
transportTarget: AbstractTransportTarget,
*varBinds: ObjectType,
**options
**options,
) -> "tuple[errind.ErrorIndication | None, Integer32 | str | int | None, Integer32 | int | None, tuple[ObjectType, ...]]":
r"""Creates a generator to perform SNMP SET query.
@@ -305,7 +306,7 @@ async def next_cmd(
authData: CommunityData,
transportTarget: AbstractTransportTarget,
*varBinds: ObjectType,
**options
**options,
) -> "tuple[errind.ErrorIndication | None, Integer32 | str | int | None, Integer32 | int | None, tuple[ObjectType, ...]]":
r"""Creates a generator to perform SNMP GETNEXT query.
@@ -455,7 +456,7 @@ async def bulk_cmd(
nonRepeaters: int,
maxRepetitions: int,
*varBinds: ObjectType,
**options
**options,
) -> "tuple[errind.ErrorIndication | None, Integer32 | str | int | None, Integer32 | int | None, tuple[ObjectType, ...]]":
r"""Creates a generator to perform SNMP GETBULK query.
@@ -627,7 +628,7 @@ async def walk_cmd(
authData: CommunityData,
transportTarget: AbstractTransportTarget,
varBind: ObjectType,
**options
**options,
) -> AsyncGenerator[
"tuple[errind.ErrorIndication | None, Integer32 | str | int | None, Integer32 | int | None, tuple[ObjectType, ...]]",
None,
@@ -738,7 +739,7 @@ async def walk_cmd(
authData,
transportTarget,
(varBind[0], Null("")), # type: ignore
**dict(lookupMib=options.get("lookupMib", True))
**dict(lookupMib=options.get("lookupMib", True)),
)
if (
ignoreNonIncreasingOid
@@ -810,7 +811,7 @@ async def bulk_walk_cmd(
nonRepeaters: int,
maxRepetitions: int,
varBind: ObjectType,
**options
**options,
) -> AsyncGenerator[
"tuple[errind.ErrorIndication | None, Integer32 | str | int | None, Integer32 | int | None, tuple[ObjectType, ...]]",
None,
@@ -943,7 +944,7 @@ async def bulk_walk_cmd(
nonRepeaters,
maxRepetitions,
*[ObjectType(varBinds[-1][0], Null(""))],
**dict(lookupMib=options.get("lookupMib", True))
**dict(lookupMib=options.get("lookupMib", True)),
)

if (
@@ -1029,10 +1030,23 @@ async def bulk_walk_cmd(


# Compatibility API
getCmd = get_cmd # noqa: N816
setCmd = set_cmd # noqa: N816
nextCmd = next_cmd # noqa: N816
bulkCmd = bulk_cmd # noqa: N816
walkCmd = walk_cmd # noqa: N816
bulkWalkCmd = bulk_walk_cmd # noqa: N816
isEndOfMib = is_end_of_mib # noqa: N816
deprecated_attributes = {
"getCmd": "get_cmd",
"setCmd": "set_cmd",
"nextCmd": "next_cmd",
"bulkCmd": "bulk_cmd",
"walkCmd": "walk_cmd",
"bulkWalkCmd": "bulk_walk_cmd",
"isEndOfMib": "is_end_of_mib",
}


def __getattr__(attr: str):
if new_attr := deprecated_attributes.get(attr):
warnings.warn(
f"{attr} is deprecated. Please use {new_attr} instead.",
DeprecationWarning,
stacklevel=2,
)
return globals()[new_attr]
raise AttributeError(f"module '{__name__}' has no attribute '{attr}'")
18 changes: 16 additions & 2 deletions pysnmp/hlapi/v1arch/asyncio/ntforg.py
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
# License: https://www.pysnmp.com/pysnmp/license.html
#
import asyncio
import warnings

from pysnmp.hlapi.transport import AbstractTransportTarget
from pysnmp.hlapi.v1arch.asyncio.auth import CommunityData
@@ -27,7 +28,7 @@ async def send_notification(
transportTarget: AbstractTransportTarget,
notifyType: str,
*varBinds: NotificationType,
**options
**options,
) -> "tuple[errind.ErrorIndication, Integer32 | int, Integer32 | int, tuple[ObjectType, ...]]":
r"""Creates a generator to send SNMP notification.
@@ -256,4 +257,17 @@ def __trap_function(future):


# Compatibility API
sendNotification = send_notification # noqa: N816
deprecated_attributes = {
"sendNotification": "send_notification",
}


def __getattr__(attr: str):
if new_attr := deprecated_attributes.get(attr):
warnings.warn(
f"{attr} is deprecated. Please use {new_attr} instead.",
DeprecationWarning,
stacklevel=2,
)
return globals()[new_attr]
raise AttributeError(f"module '{__name__}' has no attribute '{attr}'")
21 changes: 21 additions & 0 deletions pysnmp/hlapi/v3arch/asyncio/auth.py
Original file line number Diff line number Diff line change
@@ -476,6 +476,27 @@ def clone(
privKeyType is None and self.privKeyType or USM_KEY_TYPE_PASSPHRASE,
)

# Compatibility API
deprecated_attributes = {
"authKey": "authentication_key",
"privKey": "privacy_key",
"authProtocol": "authentication_protocol",
"privProtocol": "privacy_protocol",
}

def __getattr__(self, attr: str):
if new_attr := self.deprecated_attributes.get(attr):
warnings.warn(
f"{attr} is deprecated. Please use {new_attr} instead.",
DeprecationWarning,
stacklevel=2,
)
return getattr(self, new_attr)

raise AttributeError(
f"'{self.__class__.__name__}' object has no attribute '{attr}'"
)


# Backward-compatible protocol IDs

Loading

0 comments on commit 7e708dc

Please sign in to comment.