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