diff options
author | Harald Welte <laforge@osmocom.org> | 2022-02-11 17:08:45 +0100 |
---|---|---|
committer | laforge <laforge@osmocom.org> | 2022-02-12 08:33:37 +0000 |
commit | e8d177d88f89db216362284533d6e7ed27f9382a (patch) | |
tree | 00509ba32025a69e032d8da63a825c544a1ebb80 | |
parent | 9a2a6691b09baae28bd81c2e25425217793b1dbb (diff) |
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
-rw-r--r-- | pySim/tlv.py | 6 |
1 files changed, 5 insertions, 1 deletions
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 @@ 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. |