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