tlv: Convert CamelCase class name to snake_case in json

Our hand-written JSON so far is using snake_case identifiers,
while the JSON generated by the pySim.tlv classes use the class
names as keys, which LooksQuiteDifferent.

So let's auto-convert the CamelCase into something that reflects
our existing notion.

Change-Id: Id55929ef03dc48cb668e6ba7e99b6b291680a42f
This commit is contained in:
Harald Welte 2022-02-11 17:08:45 +01:00 committed by laforge
parent 9a2a6691b0
commit e8d177d88f
1 changed files with 5 additions and 1 deletions

View File

@ -31,7 +31,11 @@ from pySim.exceptions import *
import inspect
import abc
import re
def camel_to_snake(name):
name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()
class TlvMeta(abc.ABCMeta):
"""Metaclass which we use to set some class variables at the time of defining a subclass.
@ -148,7 +152,7 @@ class IE(Transcodable, metaclass=TlvMeta):
v = [x.to_dict() for x in self.children]
else:
v = self.decoded
return {type(self).__name__: v}
return {camel_to_snake(type(self).__name__): v}
def from_dict(self, decoded: dict):
"""Set the IE internal decoded representation to data from the argument.