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 | |
14 | 13 |
15 def DeleteNocompileNodes(item): | 14 def DeleteNocompileNodes(item): |
16 def HasNocompile(thing): | 15 def HasNocompile(thing): |
17 return type(thing) == dict and thing.get('nocompile', False) | 16 return type(thing) == dict and thing.get('nocompile', False) |
18 | 17 |
19 if type(item) == dict: | 18 if type(item) == dict: |
20 toDelete = [] | 19 toDelete = [] |
21 for key, value in item.items(): | 20 for key, value in item.items(): |
22 if HasNocompile(value): | 21 if HasNocompile(value): |
23 toDelete.append(key) | 22 toDelete.append(key) |
24 else: | 23 else: |
25 DeleteNocompileNodes(value) | 24 DeleteNocompileNodes(value) |
26 for key in toDelete: | 25 for key in toDelete: |
27 del item[key] | 26 del item[key] |
28 elif type(item) == list: | 27 elif type(item) == list: |
29 item[:] = [DeleteNocompileNodes(thing) | 28 item[:] = [DeleteNocompileNodes(thing) |
30 for thing in item if not HasNocompile(thing)] | 29 for thing in item if not HasNocompile(thing)] |
31 | 30 |
32 return item | 31 return item |
33 | 32 |
34 def Load(filename): | 33 def Load(filename): |
35 with open(filename, 'r') as handle: | 34 with open(filename, 'r') as handle: |
36 schemas = DeleteNocompileNodes( | 35 return DeleteNocompileNodes( |
37 json.loads(json_comment_eater.Nom(handle.read()))) | 36 json.loads(json_comment_eater.Nom(handle.read()))) |
38 schema_util.PrefixSchemasWithNamespace(schemas) | 37 |
39 return schemas | |
40 | 38 |
41 # 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 |
42 # at |filename|. | 40 # at |filename|. |
43 _cache = {} | 41 _cache = {} |
44 | 42 |
45 def CachedLoad(filename): | 43 def CachedLoad(filename): |
46 """Equivalent to Load(filename), but caches results for subsequent calls""" | 44 """Equivalent to Load(filename), but caches results for subsequent calls""" |
47 if filename not in _cache: | 45 if filename not in _cache: |
48 _cache[filename] = Load(filename) | 46 _cache[filename] = Load(filename) |
49 # 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 |
50 # the next caller. | 48 # the next caller. |
51 return copy.deepcopy(_cache[filename]) | 49 return copy.deepcopy(_cache[filename]) |
OLD | NEW |