laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/27168 )
Change subject: tlv: Convert CamleCase class name to snake_case in json ......................................................................
tlv: Convert CamleCase 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 --- M pySim/tlv.py 1 file changed, 5 insertions(+), 1 deletion(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/68/27168/1
diff --git a/pySim/tlv.py b/pySim/tlv.py index 71338ab..dc8cc4f 100644 --- a/pySim/tlv.py +++ b/pySim/tlv.py @@ -31,7 +31,11 @@
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 @@ 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.