Ejemplo n.º 1
0
def DeleteNodes(item, delete_key=None, matcher=None):
  """Deletes certain nodes in item, recursively. If |delete_key| is set, all
  dicts with |delete_key| as an attribute are deleted. If a callback is passed
  as |matcher|, |DeleteNodes| will delete all dicts for which matcher(dict)
  returns True.
  """
  assert (delete_key is not None) != (matcher is not None)

  def ShouldDelete(thing):
    return json_parse.IsDict(thing) and (
        delete_key is not None and delete_key in thing or
        matcher is not None and matcher(thing))

  if json_parse.IsDict(item):
    toDelete = []
    for key, value in item.items():
      if ShouldDelete(value):
        toDelete.append(key)
      else:
        DeleteNodes(value, delete_key, matcher)
    for key in toDelete:
      del item[key]
  elif type(item) == list:
    item[:] = [DeleteNodes(thing, delete_key, matcher)
        for thing in item if not ShouldDelete(thing)]

  return item
Ejemplo n.º 2
0
def DeleteNocompileNodes(item):
    def HasNocompile(thing):
        return json_parse.IsDict(thing) and thing.get('nocompile', False)

    if json_parse.IsDict(item):
        toDelete = []
        for key, value in item.items():
            if HasNocompile(value):
                toDelete.append(key)
            else:
                DeleteNocompileNodes(value)
        for key in toDelete:
            del item[key]
    elif type(item) == list:
        item[:] = [
            DeleteNocompileNodes(thing) for thing in item
            if not HasNocompile(thing)
        ]

    return item
Ejemplo n.º 3
0
def DeleteNodes(item, delete_key):
    """Deletes the given nodes in item, recursively, that have |delete_key| as
  an attribute.
  """
    def HasKey(thing):
        return json_parse.IsDict(thing) and thing.get(delete_key, False)

    if json_parse.IsDict(item):
        toDelete = []
        for key, value in item.items():
            if HasKey(value):
                toDelete.append(key)
            else:
                DeleteNodes(value, delete_key)
        for key in toDelete:
            del item[key]
    elif type(item) == list:
        item[:] = [
            DeleteNodes(thing, delete_key) for thing in item
            if not HasKey(thing)
        ]

    return item
Ejemplo n.º 4
0
 def ShouldDelete(thing):
   return json_parse.IsDict(thing) and (
       delete_key is not None and delete_key in thing or
       matcher is not None and matcher(thing))
Ejemplo n.º 5
0
 def HasKey(thing):
     return json_parse.IsDict(thing) and thing.get(delete_key, False)
Ejemplo n.º 6
0
 def HasNocompile(thing):
     return json_parse.IsDict(thing) and thing.get('nocompile', False)