Skip to content

Commit

Permalink
Merge pull request #6 from cesbit/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
joente authored Feb 6, 2023
2 parents 93b75e6 + cfaac01 commit 7866562
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions asyncsnmplib/mib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
from .syntax_funs import SYNTAX_FUNS


ENUM_UNKNOWN = 'unknown'
ENUM_UNKNOWN = None

FLAGS_SEPERATOR = ','


def on_octet_string(value: bytes) -> str:
"""
used as a fallback for OCTET STRING when no formatter is found/defined
"""
try:
return value.decode('utf-8')
except Exception:
return


def on_oid_map(oid: Tuple[int]) -> str:
if not isinstance(oid, tuple):
# some devices don't follow mib's syntax
Expand All @@ -23,7 +33,7 @@ def on_value_map(value: int, map_: dict) -> str:
return map_.get(value, ENUM_UNKNOWN)


def on_value_map_b(value: str, map_: dict) -> str:
def on_value_map_b(value: bytes, map_: dict) -> str:
return FLAGS_SEPERATOR.join(
v for k, v in map_.items() if value[k // 8] & (1 << k % 8))

Expand All @@ -32,7 +42,7 @@ def on_syntax(syntax: dict, value: Union[int, str]):
if syntax['tp'] == 'CUSTOM':
return SYNTAX_FUNS[syntax['func']](value)
elif syntax['tp'] == 'OCTET STRING':
return value.decode('ascii', 'ignore')
return on_octet_string(value)
elif syntax['tp'] == 'OBJECT IDENTIFIER':
return on_oid_map(value)
elif syntax['tp'] == 'BITS':
Expand Down

0 comments on commit 7866562

Please sign in to comment.