Skip to content

Commit

Permalink
Removed pyasn1.compat dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
lextm committed Sep 11, 2024
1 parent c4a5869 commit 6a4c429
Show file tree
Hide file tree
Showing 19 changed files with 77 additions and 94 deletions.
5 changes: 3 additions & 2 deletions pysnmp/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
# License: https://www.pysnmp.com/pysnmp/license.html
#
import logging
from pyasn1.compat.octets import octs2ints


from pysnmp import error
from pysnmp import __version__

Expand Down Expand Up @@ -129,6 +130,6 @@ def hexdump(octets):
return " ".join(
[
"{}{:02X}".format(n % 16 == 0 and ("\n%.5d: " % n) or "", x)
for n, x in zip(range(len(octets)), octs2ints(octets))
for n, x in zip(range(len(octets)), octets)
]
)
31 changes: 15 additions & 16 deletions pysnmp/entity/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# Copyright (c) 2005-2020, Ilya Etingof <[email protected]>
# License: https://www.pysnmp.com/pysnmp/license.html
#
from pyasn1.compat.octets import null
from pysnmp.carrier.asyncio.dgram import udp, udp6, unix
from pysnmp.carrier.base import AbstractTransport
from pysnmp.entity.engine import SnmpEngine
Expand Down Expand Up @@ -117,7 +116,7 @@ def addV1System(
contextEngineId = snmpEngineID.syntax.clone(contextEngineId)

if contextName is None:
contextName = null
contextName = b""

securityName = securityName is not None and securityName or communityIndex

Expand Down Expand Up @@ -258,7 +257,7 @@ def addV3User(
masterAuthKey = localAuthKey = authKey

if authKeyType < usmKeyTypeMaster: # pass phrase is given
masterAuthKey = authServices[authProtocol].hashPassphrase(authKey or null)
masterAuthKey = authServices[authProtocol].hashPassphrase(authKey or b"")

if authKeyType < usmKeyTypeLocalized: # pass phrase or master key is given
localAuthKey = authServices[authProtocol].localizeKey(
Expand All @@ -275,7 +274,7 @@ def addV3User(

if privKeyType < usmKeyTypeMaster: # pass phrase is given
masterPrivKey = privServices[privProtocol].hashPassphrase(
authProtocol, privKey or null
authProtocol, privKey or b""
)

if privKeyType < usmKeyTypeLocalized: # pass phrase or master key is given
Expand Down Expand Up @@ -468,7 +467,7 @@ def addTargetAddr(
params: str,
timeout: "float | None" = None,
retryCount: "int | None" = None,
tagList=null,
tagList=b"",
sourceAddress=None,
):
mibBuilder = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder
Expand Down Expand Up @@ -776,7 +775,7 @@ def addVacmUser(
readSubTree=(),
writeSubTree=(),
notifySubTree=(),
contextName=null,
contextName=b"",
):
(groupName, securityLevel, readView, writeView, notifyView) = __cookVacmUserInfo(
snmpEngine, securityModel, securityName, securityLevel
Expand All @@ -795,11 +794,11 @@ def addVacmUser(
notifyView,
)
if readSubTree:
addVacmView(snmpEngine, readView, "included", readSubTree, null)
addVacmView(snmpEngine, readView, "included", readSubTree, b"")
if writeSubTree:
addVacmView(snmpEngine, writeView, "included", writeSubTree, null)
addVacmView(snmpEngine, writeView, "included", writeSubTree, b"")
if notifySubTree:
addVacmView(snmpEngine, notifyView, "included", notifySubTree, null)
addVacmView(snmpEngine, notifyView, "included", notifySubTree, b"")


def delVacmUser(
Expand All @@ -810,7 +809,7 @@ def delVacmUser(
readSubTree=(),
writeSubTree=(),
notifySubTree=(),
contextName=null,
contextName=b"",
):
(groupName, securityLevel, readView, writeView, notifyView) = __cookVacmUserInfo(
snmpEngine, securityModel, securityName, securityLevel
Expand All @@ -830,7 +829,7 @@ def delVacmUser(


def addRoUser(
snmpEngine, securityModel, securityName, securityLevel, subTree, contextName=null
snmpEngine, securityModel, securityName, securityLevel, subTree, contextName=b""
):
addVacmUser(
snmpEngine,
Expand All @@ -843,7 +842,7 @@ def addRoUser(


def delRoUser(
snmpEngine, securityModel, securityName, securityLevel, subTree, contextName=null
snmpEngine, securityModel, securityName, securityLevel, subTree, contextName=b""
):
delVacmUser(
snmpEngine,
Expand All @@ -856,7 +855,7 @@ def delRoUser(


def addRwUser(
snmpEngine, securityModel, securityName, securityLevel, subTree, contextName=null
snmpEngine, securityModel, securityName, securityLevel, subTree, contextName=b""
):
addVacmUser(
snmpEngine,
Expand All @@ -870,7 +869,7 @@ def addRwUser(


def delRwUser(
snmpEngine, securityModel, securityName, securityLevel, subTree, contextName=null
snmpEngine, securityModel, securityName, securityLevel, subTree, contextName=b""
):
delVacmUser(
snmpEngine,
Expand All @@ -884,7 +883,7 @@ def delRwUser(


def addTrapUser(
snmpEngine, securityModel, securityName, securityLevel, subTree, contextName=null
snmpEngine, securityModel, securityName, securityLevel, subTree, contextName=b""
):
addVacmUser(
snmpEngine,
Expand All @@ -899,7 +898,7 @@ def addTrapUser(


def delTrapUser(
snmpEngine, securityModel, securityName, securityLevel, subTree, contextName=null
snmpEngine, securityModel, securityName, securityLevel, subTree, contextName=b""
):
delVacmUser(
snmpEngine,
Expand Down
3 changes: 1 addition & 2 deletions pysnmp/entity/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import sys
import tempfile
from typing import Any
from pyasn1.compat.octets import str2octs
from pysnmp.carrier.base import AbstractTransportAddress, AbstractTransportDispatcher
from pysnmp.proto.rfc1902 import OctetString
from pysnmp.proto.rfc3412 import MsgAndPduDispatcher
Expand Down Expand Up @@ -157,7 +156,7 @@ def __init__(

try:
fd, fn = tempfile.mkstemp(dir=persistentPath)
os.write(fd, str2octs(snmpEngineBoots.syntax.prettyPrint()))
os.write(fd, snmpEngineBoots.syntax.prettyPrint().encode("iso-8859-1"))
os.close(fd)
shutil.move(fn, f)
except Exception:
Expand Down
7 changes: 3 additions & 4 deletions pysnmp/entity/rfc3413/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# License: https://www.pysnmp.com/pysnmp/license.html
#
from pyasn1.type import univ
from pyasn1.compat.octets import null
from pysnmp import error
from pysnmp import debug

Expand All @@ -26,7 +25,7 @@ def __init__(self, snmpEngine, contextEngineId=None):
f'SnmpContext: contextEngineId "{self.contextEngineId!r}"'
)
self.contextNames = {
null: snmpEngine.msgAndPduDsp.mibInstrumController # Default name
b"": snmpEngine.msgAndPduDsp.mibInstrumController # Default name
}

def registerContextName(self, contextName, mibInstrum=None):
Expand All @@ -37,7 +36,7 @@ def registerContextName(self, contextName, mibInstrum=None):
f"registerContextName: registered contextName {contextName!r}, mibInstrum {mibInstrum!r}"
)
if mibInstrum is None:
self.contextNames[contextName] = self.contextNames[null]
self.contextNames[contextName] = self.contextNames[b""]
else:
self.contextNames[contextName] = mibInstrum

Expand All @@ -49,7 +48,7 @@ def unregisterContextName(self, contextName):
)
del self.contextNames[contextName]

def getMibInstrum(self, contextName=null):
def getMibInstrum(self, contextName=b""):
contextName = univ.OctetString(contextName).asOctets()
if contextName not in self.contextNames:
debug.logger & debug.flagIns and debug.logger(
Expand Down
3 changes: 1 addition & 2 deletions pysnmp/entity/rfc3413/ntforg.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# License: https://www.pysnmp.com/pysnmp/license.html
#
import sys
from pyasn1.compat.octets import null
from pysnmp.entity.rfc3413 import config
from pysnmp.proto.proxy import rfc2576
from pysnmp.proto import rfc3411
Expand Down Expand Up @@ -564,7 +563,7 @@ def _sendNotification(
additionalVarBinds=(),
cbFun=None,
cbCtx=None,
contextName=null,
contextName=b"",
instanceIndex=None,
):
if self.snmpContext is None:
Expand Down
5 changes: 2 additions & 3 deletions pysnmp/entity/rfc3413/ntfrcv.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# License: https://www.pysnmp.com/pysnmp/license.html
#
import sys
from pyasn1.compat.octets import null
from pysnmp.proto import rfc3411, error
from pysnmp.proto.api import v1, v2c # backend is always SMIv2 compliant
from pysnmp.proto.proxy import rfc2576
Expand All @@ -22,7 +21,7 @@ class NotificationReceiver:

def __init__(self, snmpEngine, cbFun, cbCtx=None):
snmpEngine.msgAndPduDsp.registerContextEngineId(
null, self.pduTypes, self.processPdu # '' is a wildcard
b"", self.pduTypes, self.processPdu # '' is a wildcard
)

self.__snmpTrapCommunity = ""
Expand All @@ -38,7 +37,7 @@ def storeSnmpTrapCommunity(snmpEngine, execpoint, variables, cbCtx):
)

def close(self, snmpEngine):
snmpEngine.msgAndPduDsp.unregisterContextEngineId(null, self.pduTypes)
snmpEngine.msgAndPduDsp.unregisterContextEngineId(b"", self.pduTypes)
self.__cbFun = self.__cbCtx = None

def processPdu(
Expand Down
9 changes: 4 additions & 5 deletions pysnmp/entity/rfc3413/oneliner/cmdgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from pysnmp.hlapi.asyncio import sync
from pysnmp.hlapi.varbinds import *
from pysnmp.hlapi.lcd import *
from pyasn1.compat.octets import null
from pyasn1.type import univ

__all__ = ["CommandGenerator", "MibVariable"]
Expand All @@ -37,7 +36,7 @@ def getCmd(self, authData, transportTarget, *varNames, **kwargs):
self.snmpEngine,
authData,
transportTarget,
ContextData(kwargs.get("contextEngineId"), kwargs.get("contextName", null)),
ContextData(kwargs.get("contextEngineId"), kwargs.get("contextName", b"")),
*[(x, self._null) for x in varNames],
**kwargs
)
Expand All @@ -51,7 +50,7 @@ def setCmd(self, authData, transportTarget, *varBinds, **kwargs):
self.snmpEngine,
authData,
transportTarget,
ContextData(kwargs.get("contextEngineId"), kwargs.get("contextName", null)),
ContextData(kwargs.get("contextEngineId"), kwargs.get("contextName", b"")),
*varBinds,
**kwargs
)
Expand All @@ -69,7 +68,7 @@ def nextCmd(self, authData, transportTarget, *varNames, **kwargs):
self.snmpEngine,
authData,
transportTarget,
ContextData(kwargs.get("contextEngineId"), kwargs.get("contextName", null)),
ContextData(kwargs.get("contextEngineId"), kwargs.get("contextName", b"")),
*[(x, self._null) for x in varNames],
**kwargs
):
Expand Down Expand Up @@ -101,7 +100,7 @@ def bulkCmd(
self.snmpEngine,
authData,
transportTarget,
ContextData(kwargs.get("contextEngineId"), kwargs.get("contextName", null)),
ContextData(kwargs.get("contextEngineId"), kwargs.get("contextName", b"")),
nonRepeaters,
maxRepetitions,
*[(x, self._null) for x in varNames],
Expand Down
3 changes: 1 addition & 2 deletions pysnmp/entity/rfc3413/oneliner/ntforg.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from pysnmp.hlapi.asyncio import sync
from pysnmp.hlapi.varbinds import *
from pysnmp.hlapi.lcd import *
from pyasn1.compat.octets import null
from pysnmp.entity import config
from pysnmp.entity.rfc3413 import context

Expand Down Expand Up @@ -82,7 +81,7 @@ def sendNotification(
self.snmpEngine,
authData,
transportTarget,
ContextData(kwargs.get("contextEngineId"), kwargs.get("contextName", null)),
ContextData(kwargs.get("contextEngineId"), kwargs.get("contextName", b"")),
notifyType,
notificationType.addVarBinds(*varBinds),
**kwargs
Expand Down
8 changes: 4 additions & 4 deletions pysnmp/hlapi/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
from pysnmp.entity import config
from pysnmp import error
from pyasn1.compat.octets import null


__all__ = [
"CommunityData",
Expand Down Expand Up @@ -113,8 +113,8 @@ class instance.
mpModel = 1 # Default is SMIv2
securityModel = mpModel + 1
securityLevel = "noAuthNoPriv"
contextName = null
tag = null
contextName = b""
tag = b""

def __init__(
self,
Expand Down Expand Up @@ -381,7 +381,7 @@ class instance.
securityLevel = "noAuthNoPriv"
securityModel = 3
mpModel = 3
contextName = null
contextName = b""

def __init__(
self,
Expand Down
4 changes: 1 addition & 3 deletions pysnmp/hlapi/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
# Copyright (c) 2005-2020, Ilya Etingof <[email protected]>
# License: https://www.pysnmp.com/pysnmp/license.html
#
from pyasn1.compat.octets import null

__all__ = ["ContextData"]


Expand Down Expand Up @@ -46,7 +44,7 @@ class ContextData:
"""

def __init__(self, contextEngineId=None, contextName=null):
def __init__(self, contextEngineId=None, contextName=b""):
self.contextEngineId = contextEngineId
self.contextName = contextName

Expand Down
8 changes: 3 additions & 5 deletions pysnmp/hlapi/lcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from pysnmp.entity.engine import SnmpEngine
from pysnmp.hlapi.auth import *

from pyasn1.compat.octets import null

from pysnmp.hlapi.transport import AbstractTransportTarget

__all__ = ["CommandGeneratorLcdConfigurator", "NotificationOriginatorLcdConfigurator"]
Expand Down Expand Up @@ -39,7 +37,7 @@ class CommandGeneratorLcdConfigurator(AbstractLcdConfigurator):
cacheKeys = ["auth", "parm", "tran", "addr"]

def configure(
self, snmpEngine, authData, transportTarget, contextName=null, **options
self, snmpEngine, authData, transportTarget, contextName=b"", **options
):
cache = self._getCache(snmpEngine)
if isinstance(authData, CommunityData):
Expand Down Expand Up @@ -128,7 +126,7 @@ def configure(

return addrName, paramsName

def unconfigure(self, snmpEngine, authData=None, contextName=null, **options):
def unconfigure(self, snmpEngine, authData=None, contextName=b"", **options):
cache = self._getCache(snmpEngine)
if authData:
if isinstance(authData, CommunityData):
Expand Down Expand Up @@ -264,7 +262,7 @@ def configure(

return notifyName

def unconfigure(self, snmpEngine, authData=None, contextName=null, **options):
def unconfigure(self, snmpEngine, authData=None, contextName=b"", **options):
cache = self._getCache(snmpEngine)
if authData:
authDataKey = (
Expand Down
Loading

0 comments on commit 6a4c429

Please sign in to comment.