| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import copy | 5 import copy |
| 6 | 6 |
| 7 import json_parse | 7 import json_parse |
| 8 | 8 |
| 9 def DeleteNodes(item, delete_key): | 9 |
| 10 """Deletes the given nodes in item, recursively, that have |delete_key| as | 10 def DeleteNodes(item, delete_key=None, matcher=None): |
| 11 an attribute. | 11 """Deletes certain nodes in item, recursively. If |delete_key| is set, all |
| 12 dicts with |delete_key| as an attribute are deleted. If a callback is passed |
| 13 as |matcher|, |DeleteNodes| will delete all dicts for which matcher(dict) |
| 14 returns True. |
| 12 """ | 15 """ |
| 13 def HasKey(thing): | 16 assert (delete_key is not None) != (matcher is not None) |
| 14 return json_parse.IsDict(thing) and thing.get(delete_key, False) | 17 |
| 18 def ShouldDelete(thing): |
| 19 return json_parse.IsDict(thing) and ( |
| 20 delete_key is not None and delete_key in thing or |
| 21 matcher is not None and matcher(thing)) |
| 15 | 22 |
| 16 if json_parse.IsDict(item): | 23 if json_parse.IsDict(item): |
| 17 toDelete = [] | 24 toDelete = [] |
| 18 for key, value in item.items(): | 25 for key, value in item.items(): |
| 19 if HasKey(value): | 26 if ShouldDelete(value): |
| 20 toDelete.append(key) | 27 toDelete.append(key) |
| 21 else: | 28 else: |
| 22 DeleteNodes(value, delete_key) | 29 DeleteNodes(value, delete_key, matcher) |
| 23 for key in toDelete: | 30 for key in toDelete: |
| 24 del item[key] | 31 del item[key] |
| 25 elif type(item) == list: | 32 elif type(item) == list: |
| 26 item[:] = [DeleteNodes(thing, delete_key) | 33 item[:] = [DeleteNodes(thing, delete_key, matcher) |
| 27 for thing in item if not HasKey(thing)] | 34 for thing in item if not ShouldDelete(thing)] |
| 28 | 35 |
| 29 return item | 36 return item |
| 30 | 37 |
| 31 | 38 |
| 32 def Load(filename): | 39 def Load(filename): |
| 33 with open(filename, 'r') as handle: | 40 with open(filename, 'r') as handle: |
| 34 schemas = json_parse.Parse(handle.read()) | 41 schemas = json_parse.Parse(handle.read()) |
| 35 return schemas | 42 return schemas |
| 36 | 43 |
| 37 | 44 |
| 38 # A dictionary mapping |filename| to the object resulting from loading the JSON | 45 # A dictionary mapping |filename| to the object resulting from loading the JSON |
| 39 # at |filename|. | 46 # at |filename|. |
| 40 _cache = {} | 47 _cache = {} |
| 41 | 48 |
| 42 | 49 |
| 43 def CachedLoad(filename): | 50 def CachedLoad(filename): |
| 44 """Equivalent to Load(filename), but caches results for subsequent calls""" | 51 """Equivalent to Load(filename), but caches results for subsequent calls""" |
| 45 if filename not in _cache: | 52 if filename not in _cache: |
| 46 _cache[filename] = Load(filename) | 53 _cache[filename] = Load(filename) |
| 47 # Return a copy of the object so that any changes a caller makes won't affect | 54 # Return a copy of the object so that any changes a caller makes won't affect |
| 48 # the next caller. | 55 # the next caller. |
| 49 return copy.deepcopy(_cache[filename]) | 56 return copy.deepcopy(_cache[filename]) |
| 50 | 57 |
| OLD | NEW |