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 json | 6 import json |
7 | 7 |
8 def StripJSONComments(stream): | 8 def StripJSONComments(stream): |
9 """Strips //-style comments from a stream of JSON. Allows un-escaped // | 9 """Strips //-style comments from a stream of JSON. Allows un-escaped // |
10 inside string values. | 10 inside string values. |
(...skipping 30 matching lines...) Expand all Loading... |
41 else: | 41 else: |
42 if last_char == '/' and not inside_string: | 42 if last_char == '/' and not inside_string: |
43 result += '/' | 43 result += '/' |
44 if char == '"': | 44 if char == '"': |
45 inside_string = not inside_string | 45 inside_string = not inside_string |
46 last_char = char | 46 last_char = char |
47 result += char | 47 result += char |
48 | 48 |
49 return result | 49 return result |
50 | 50 |
| 51 def DeleteNocompileNodes(item): |
| 52 def HasNocompile(thing): |
| 53 return type(thing) == dict and thing.get('nocompile', False) |
| 54 |
| 55 if type(item) == dict: |
| 56 toDelete = [] |
| 57 for key, value in item.items(): |
| 58 if HasNocompile(value): |
| 59 toDelete.append(key) |
| 60 else: |
| 61 DeleteNocompileNodes(value) |
| 62 for key in toDelete: |
| 63 del item[key] |
| 64 elif type(item) == list: |
| 65 item[:] = [DeleteNocompileNodes(thing) |
| 66 for thing in item if not HasNocompile(thing)] |
| 67 |
| 68 return item |
| 69 |
51 def Load(filename): | 70 def Load(filename): |
52 with open(filename, 'r') as handle: | 71 with open(filename, 'r') as handle: |
53 return json.loads(StripJSONComments(handle.read())) | 72 return DeleteNocompileNodes(json.loads(StripJSONComments(handle.read()))) |
54 | 73 |
55 | 74 |
56 # A dictionary mapping |filename| to the object resulting from loading the JSON | 75 # A dictionary mapping |filename| to the object resulting from loading the JSON |
57 # at |filename|. | 76 # at |filename|. |
58 _cache = {} | 77 _cache = {} |
59 | 78 |
60 def CachedLoad(filename): | 79 def CachedLoad(filename): |
61 """Equivalent to Load(filename), but caches results for subsequent calls""" | 80 """Equivalent to Load(filename), but caches results for subsequent calls""" |
62 if filename not in _cache: | 81 if filename not in _cache: |
63 _cache[filename] = Load(filename) | 82 _cache[filename] = Load(filename) |
64 # Return a copy of the object so that any changes a caller makes won't affect | 83 # Return a copy of the object so that any changes a caller makes won't affect |
65 # the next caller. | 84 # the next caller. |
66 return copy.deepcopy(_cache[filename]) | 85 return copy.deepcopy(_cache[filename]) |
OLD | NEW |