xref: /aosp_15_r20/external/cronet/android/tools/license/metadata_dictionary.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1class MetadataDictionary(dict):
2  """
3  This is a very simple class that prints out a textproto using a dictionary.
4  Realistically, we should not be re-inventing the wheel as we are doing here
5  and we should be using protobuf instead.
6
7  TODO(b/360322121): Use protobuf generated classes instead of this.
8  """
9
10  def __init__(self, field_name):
11    super().__init__()
12    self.field_name = field_name
13
14  def _as_string(self, dict_items, width=2, depth=1):
15    str = self.field_name + " {\n"
16    for (key, value) in dict_items:
17      if not isinstance(value, MetadataDictionary):
18        str += (" " * width * depth) + f"{key}: {value}\n"
19      else:
20        str += (" " * width * depth) + value._as_string(value.items(), width,
21                                                        depth + 1)
22    str += (" " * width * (depth - 1)) + "}\n"
23    return str
24
25  def __repr__(self):
26    return self._as_string(self.items())
27