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 import os | |
7 import sys | |
8 | 6 |
9 import json_parse | 7 import json_parse |
10 import schema_util | |
11 | 8 |
12 def DeleteNodes(item, delete_key): | 9 def DeleteNodes(item, delete_key): |
13 """Deletes the given nodes in item, recursively, that have |delete_key| as | 10 """Deletes the given nodes in item, recursively, that have |delete_key| as |
14 an attribute. | 11 an attribute. |
15 """ | 12 """ |
16 def HasKey(thing): | 13 def HasKey(thing): |
17 return json_parse.IsDict(thing) and thing.get(delete_key, False) | 14 return json_parse.IsDict(thing) and thing.get(delete_key, False) |
18 | 15 |
19 if json_parse.IsDict(item): | 16 if json_parse.IsDict(item): |
20 toDelete = [] | 17 toDelete = [] |
21 for key, value in item.items(): | 18 for key, value in item.items(): |
22 if HasKey(value): | 19 if HasKey(value): |
23 toDelete.append(key) | 20 toDelete.append(key) |
24 else: | 21 else: |
25 DeleteNodes(value, delete_key) | 22 DeleteNodes(value, delete_key) |
26 for key in toDelete: | 23 for key in toDelete: |
27 del item[key] | 24 del item[key] |
28 elif type(item) == list: | 25 elif type(item) == list: |
29 item[:] = [DeleteNodes(thing, delete_key) | 26 item[:] = [DeleteNodes(thing, delete_key) |
30 for thing in item if not HasKey(thing)] | 27 for thing in item if not HasKey(thing)] |
31 | 28 |
32 return item | 29 return item |
33 | 30 |
| 31 |
34 def Load(filename): | 32 def Load(filename): |
35 with open(filename, 'r') as handle: | 33 with open(filename, 'r') as handle: |
36 schemas = json_parse.Parse(handle.read()) | 34 schemas = json_parse.Parse(handle.read()) |
37 return schemas | 35 return schemas |
38 | 36 |
| 37 |
39 # A dictionary mapping |filename| to the object resulting from loading the JSON | 38 # A dictionary mapping |filename| to the object resulting from loading the JSON |
40 # at |filename|. | 39 # at |filename|. |
41 _cache = {} | 40 _cache = {} |
42 | 41 |
| 42 |
43 def CachedLoad(filename): | 43 def CachedLoad(filename): |
44 """Equivalent to Load(filename), but caches results for subsequent calls""" | 44 """Equivalent to Load(filename), but caches results for subsequent calls""" |
45 if filename not in _cache: | 45 if filename not in _cache: |
46 _cache[filename] = Load(filename) | 46 _cache[filename] = Load(filename) |
47 # Return a copy of the object so that any changes a caller makes won't affect | 47 # Return a copy of the object so that any changes a caller makes won't affect |
48 # the next caller. | 48 # the next caller. |
49 return copy.deepcopy(_cache[filename]) | 49 return copy.deepcopy(_cache[filename]) |
50 | 50 |
OLD | NEW |