Revision d0202de7

b/lib/cli.py
4183 4183
  return not (isinstance(data, (list, dict, tuple)))
4184 4184

  
4185 4185

  
4186
def _GetAlignmentMapping(data):
4187
  """ Returns info about alignment if present in an encoded ordered dictionary.
4188

  
4189
  @type data: list of tuple
4190
  @param data: The encoded ordered dictionary, as defined in
4191
               L{_SerializeGenericInfo}.
4192
  @rtype: dict of any to int
4193
  @return: The dictionary mapping alignment groups to the maximum length of the
4194
           dictionary key found in the group.
4195

  
4196
  """
4197
  alignment_map = {}
4198
  for entry in data:
4199
    if len(entry) > 2:
4200
      group_key = entry[2]
4201
      key_length = len(entry[0])
4202
      if group_key in alignment_map:
4203
        alignment_map[group_key] = max(alignment_map[group_key], key_length)
4204
      else:
4205
        alignment_map[group_key] = key_length
4206

  
4207
  return alignment_map
4208

  
4209

  
4186 4210
def _SerializeGenericInfo(buf, data, level, afterkey=False):
4187 4211
  """Formatting core of L{PrintGenericInfo}.
4188 4212

  
......
4215 4239
        _SerializeGenericInfo(buf, data[key], level + 1, afterkey=True)
4216 4240
  elif isinstance(data, list) and len(data) > 0 and isinstance(data[0], tuple):
4217 4241
    # list of tuples (an ordered dictionary)
4242
    # the tuples may have two or three members - key, value, and alignment group
4243
    # if the alignment group is present, align all values sharing the same group
4218 4244
    if afterkey:
4219 4245
      buf.write("\n")
4220 4246
      doindent = True
4221 4247
    else:
4222 4248
      doindent = False
4223
    for (key, val) in data:
4249

  
4250
    alignment_mapping = _GetAlignmentMapping(data)
4251
    for entry in data:
4252
      key, val = entry[0:2]
4224 4253
      if doindent:
4225 4254
        buf.write(baseind * level)
4226 4255
      else:
4227 4256
        doindent = True
4228 4257
      buf.write(key)
4229 4258
      buf.write(": ")
4259
      if len(entry) > 2:
4260
        max_key_length = alignment_mapping[entry[2]]
4261
        buf.write(" " * (max_key_length - len(key)))
4230 4262
      _SerializeGenericInfo(buf, val, level + 1, afterkey=True)
4231 4263
  elif isinstance(data, tuple) and all(map(_NotAContainer, data)):
4232 4264
    # tuples with simple content are serialized as inline lists
......
4265 4297
      can be:
4266 4298
        - dictionaries, where keys are strings and values are of any of the
4267 4299
          types listed here
4268
        - lists of pairs (key, value), where key is a string and value is of
4269
          any of the types listed here; it's a way to encode ordered
4270
          dictionaries
4300
        - lists of tuples (key, value) or (key, value, alignment_group), where
4301
          key is a string, value is of any of the types listed here, and
4302
          alignment_group can be any hashable value; it's a way to encode
4303
          ordered dictionaries; any entries sharing the same alignment group are
4304
          aligned by appending whitespace before the value as needed
4271 4305
        - lists of any of the types listed here
4272 4306
        - strings
4273 4307

  

Also available in: Unified diff