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