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