tests:sniffer.py: make sure tlv_struct name is a valid identifier

Some names (ex. 'non-operating_channel ') are not a valid python identifier.
Makes sure TlvStruct name is a valid identifier.

PPM-1161
--
Signed-off-by: Krystyna Oliinyk <k.oliinyk@inango-systems.com>
This commit is contained in:
Krystyna Oliinyk
2021-03-24 17:19:43 +02:00
parent 48233b916f
commit e449dceafd

View File

@@ -9,6 +9,7 @@ import os
import json
from collections import OrderedDict
import subprocess
import re
from opts import debug, err, status
from typing import Callable
@@ -32,6 +33,7 @@ class TlvStruct:
value = [TlvStruct(value, level + 1)]
else:
value = [TlvStruct(v, level + 1) for k, v in value.items()]
name = self.valid_var_name(name)
setattr(self, name, value)
def __repr__(self):
@@ -43,6 +45,8 @@ class TlvStruct:
def __eq__(self, other):
return self._d() == other._d()
def valid_var_name(self, varStr): return re.sub(r'\W|^(?=\d)', '_', varStr)
class Tlv(TlvStruct):
'''Represents an IEEE1905.1 TLV in a Packet.'''