Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
T
tango
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Jira issues
Open Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LOFAR2.0
tango
Commits
c1ad15c1
Commit
c1ad15c1
authored
Aug 24, 2022
by
Auke Klazema
Browse files
Options
Downloads
Patches
Plain Diff
L2SS-873
: Refactor convertion using isinstance checks
parent
09f68ca3
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!398
Resolve L2SS-873
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
tangostationcontrol/tangostationcontrol/clients/snmp_client.py
+41
-29
41 additions, 29 deletions
...stationcontrol/tangostationcontrol/clients/snmp_client.py
with
41 additions
and
29 deletions
tangostationcontrol/tangostationcontrol/clients/snmp_client.py
+
41
−
29
View file @
c1ad15c1
...
...
@@ -183,52 +183,64 @@ class snmp_attribute:
"""
get all the values in a list, make sure to convert specific types that dont want to play nicely
"""
vals
=
[]
val
ue
s
=
[]
varBinds
=
var_binds
[
0
]
for
var_bind
in
varBinds
:
value
=
self
.
_convert_var_bind
(
var_bind
)
vals
.
append
(
value
)
value
=
self
.
_convert_var_bind
_value
(
var_bind
[
1
]
)
val
ue
s
.
append
(
value
)
if
self
.
is_scalar
:
vals
=
vals
[
0
]
val
ue
s
=
val
ue
s
[
0
]
return
vals
return
val
ue
s
def
_convert_var_bind
(
self
,
va
r_bind
):
# Some MIB's used custom types, some dont. Custom types are merely wrapped base types.
varbind_types
=
var_bind
[
1
].
__class__
.
__bases__
+
(
type
(
var_bind
[
1
]),
)
def
_convert_var_bind
_value
(
self
,
va
lue
):
def
is_an_hlapi_integer
(
value
):
return
isinstance
(
value
,
(
hlapi
.
Integer32
,
hlapi
.
Integer
)
)
snmp_type
=
None
def
is_an_hlapi_number_type
(
value
):
return
isinstance
(
value
,
(
hlapi
.
TimeTicks
,
hlapi
.
Counter32
,
hlapi
.
Gauge32
,
hlapi
.
Integer32
,
hlapi
.
Integer
))
# find if one of the base types is present.
for
i
in
varbind_types
:
if
i
in
snmp_to_numpy_dict
.
keys
():
snmp_type
=
i
def
is_an_hlapi_string_type
(
value
):
return
isinstance
(
value
,
(
hlapi
.
OctetString
,
hlapi
.
ObjectIdentity
))
if
snmp_type
is
None
:
r
aise
TypeError
(
f
"
Error: did not find a valid snmp type. Got:
{
varbind_types
}
, expected one of:
'
{
snmp_to_numpy_dict
.
keys
()
}
'"
)
def
needs_conversion_from_integer_to_str
(
value
)
:
r
eturn
is_an_hlapi_integer
(
value
)
and
self
.
dtype
==
str
if
snmp_type
is
hlapi
.
IpAddress
:
# IpAddress values get printed as their raw value but in hex (7F 20 20 01 for 127.0.0.1 for example)
value
=
var_bind
[
1
].
prettyPrint
()
def
needs_conversion_from_ipaddress_to_str
(
value
):
return
isinstance
(
value
,
hlapi
.
IpAddress
)
and
self
.
dtype
==
str
elif
(
snmp_type
is
hlapi
.
Integer32
or
snmp_type
is
hlapi
.
Integer
)
and
self
.
dtype
==
str
:
# Integers can have 'named values', Where a value can be translated to a specific name. A dict basically
# Example: {1: "other", 2: "invalid", 3: "dynamic", 4: "static",}
def
needs_conversion_from_number_to_int64
(
value
):
return
is_an_hlapi_number_type
(
value
)
and
self
.
dtype
==
numpy
.
int64
if
var_bind
[
1
].
namedValues
==
{}:
# An empty dict {} means no namedValue's are present.
value
=
snmp_to_numpy_dict
[
snmp_type
](
var_bind
[
1
])
def
needs_conversion_from_string_to_str
(
value
):
return
is_an_hlapi_string_type
(
value
)
and
self
.
dtype
==
str
def
convert_integer_to_str
(
value
):
if
value
.
namedValues
:
result
=
value
.
prettyPrint
()
else
:
# append the named values string instead of the raw number.
value
=
var_bind
[
1
].
prettyPrint
()
result
=
numpy
.
int64
(
value
)
return
result
if
needs_conversion_from_ipaddress_to_str
(
value
):
result
=
value
.
prettyPrint
()
elif
needs_conversion_from_integer_to_str
(
value
):
result
=
convert_integer_to_str
(
value
)
elif
needs_conversion_from_number_to_int64
(
value
):
result
=
numpy
.
int64
(
value
)
*
self
.
scaling_factor
elif
needs_conversion_from_string_to_str
(
value
):
result
=
str
(
value
)
else
:
# convert from the funky pysnmp types to numpy types and then append
value
=
snmp_to_numpy_dict
[
snmp_type
](
var_bind
[
1
])
*
self
.
scaling_factor
raise
TypeError
(
f
"
Error: did not find a valid snmp type. Got:
{
type
(
value
)
}
, expected one of:
'
{
snmp_to_numpy_dict
.
keys
()
}
'"
)
return
result
return
value
class
mib_loader
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment