aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@osmocom.org>2022-02-11 15:44:28 +0100
committerHarald Welte <laforge@osmocom.org>2022-02-11 17:13:01 +0100
commit9a2a6691b09baae28bd81c2e25425217793b1dbb (patch)
tree99e196b708581ff386aca2926cf5dbbf2218a6cd
parent425038ffbc05c467e6019489aaaa9d86055a7a2b (diff)
tlv: Function for flattening the list-of-dict output of TLV decoder
Before: { "FcpTemplate": [ { "FileDescriptor": { "shareable": true, "file_type": "df", "structure": "no_info_given" } }, { "FileIdentifier": "3f00" }, { "ProprietaryInformation": [ { "UiccCharacteristics": "71" }, { "AvailableMemory": 123052 } ] }, { "LifeCycleStatusInteger": "operational_activated" }, { "SecurityAttribReferenced": { "ef_arr_file_id": "2f06", "ef_arr_record_nr": 2 } }, { "PinStatusTemplate_DO": [ { "PS_DO": "40" }, { "KeyReference": 1 }, { "KeyReference": 129 } ] }, { "TotalFileSize": 187809 } ] } After: { "FcpTemplate": { "FileDescriptor": { "shareable": true, "file_type": "df", "structure": "no_info_given" }, "FileIdentifier": "3f00", "ProprietaryInformation": { "UiccCharacteristics": "71", "AvailableMemory": 123052 }, "LifeCycleStatusInteger": "operational_activated", "SecurityAttribReferenced": { "ef_arr_file_id": "2f06", "ef_arr_record_nr": 2 }, "PinStatusTemplate_DO": { "PS_DO": "40", "KeyReference": 129 }, "TotalFileSize": 187809 } } Change-Id: Ia5ad8f1d3b0d47ebdb1856b0feaba120bad3eef9
-rw-r--r--pySim/tlv.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/pySim/tlv.py b/pySim/tlv.py
index ba1aa9f..71338ab 100644
--- a/pySim/tlv.py
+++ b/pySim/tlv.py
@@ -404,3 +404,30 @@ class TLV_IE_Collection(metaclass=TlvCollectionMeta):
def to_tlv(self):
return self.to_bytes()
+
+
+def flatten_dict_lists(inp):
+ """hierarchically flatten each list-of-dicts into a single dict. This is useful to
+ make the output of hierarchical TLV decoder structures flatter and more easy to read."""
+ def are_all_elements_dict(l):
+ for e in l:
+ if not isinstance(e, dict):
+ return False
+ return True
+
+ if isinstance(inp, list):
+ if are_all_elements_dict(inp):
+ # flatten into one shared dict
+ newdict = {}
+ for e in inp:
+ key = list(e.keys())[0]
+ newdict[key] = e[key]
+ inp = newdict
+ # process result as any native dict
+ return {k:flatten_dict_lists(inp[k]) for k in inp.keys()}
+ else:
+ return [flatten_dict_lists(x) for x in inp]
+ elif isinstance(inp, dict):
+ return {k:flatten_dict_lists(inp[k]) for k in inp.keys()}
+ else:
+ return inp