Chromium Code Reviews| 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 delete in toDelete: | |
|
Yoyo Zhou
2012/04/16 23:21:46
I'd call 'delete' 'key' instead.
not at google - send to devlin
2012/04/17 04:05:45
Done.
| |
| 63 del item[delete] | |
| 64 elif type(item) == list: | |
|
Yoyo Zhou
2012/04/16 23:21:46
You could make this a list comprehension.
item[:]
not at google - send to devlin
2012/04/17 04:05:45
Done.
| |
| 65 i = 0 | |
| 66 while i < len(item): | |
| 67 if HasNocompile(item[i]): | |
| 68 del item[i] | |
| 69 else: | |
| 70 DeleteNocompileNodes(item[i]) | |
| 71 i += 1 | |
| 72 | |
| 73 return item | |
| 74 | |
| 51 def Load(filename): | 75 def Load(filename): |
| 52 with open(filename, 'r') as handle: | 76 with open(filename, 'r') as handle: |
| 53 return json.loads(StripJSONComments(handle.read())) | 77 return DeleteNocompileNodes(json.loads(StripJSONComments(handle.read()))) |
| 54 | |
| 55 | 78 |
| 56 # A dictionary mapping |filename| to the object resulting from loading the JSON | 79 # A dictionary mapping |filename| to the object resulting from loading the JSON |
| 57 # at |filename|. | 80 # at |filename|. |
| 58 _cache = {} | 81 _cache = {} |
| 59 | 82 |
| 60 def CachedLoad(filename): | 83 def CachedLoad(filename): |
| 61 """Equivalent to Load(filename), but caches results for subsequent calls""" | 84 """Equivalent to Load(filename), but caches results for subsequent calls""" |
| 62 if filename not in _cache: | 85 if filename not in _cache: |
| 63 _cache[filename] = Load(filename) | 86 _cache[filename] = Load(filename) |
| 64 # Return a copy of the object so that any changes a caller makes won't affect | 87 # Return a copy of the object so that any changes a caller makes won't affect |
| 65 # the next caller. | 88 # the next caller. |
| 66 return copy.deepcopy(_cache[filename]) | 89 return copy.deepcopy(_cache[filename]) |
| OLD | NEW |