From e8d177d88f89db216362284533d6e7ed27f9382a Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Fri, 11 Feb 2022 17:08:45 +0100 Subject: [PATCH] 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 --- pySim/tlv.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pySim/tlv.py b/pySim/tlv.py index 71338ab0..dc8cc4fd 100644 --- a/pySim/tlv.py +++ b/pySim/tlv.py @@ -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.