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 # The text to look for, that means "do not generate" for each language. |
13 def HasNocompile(thing): | 13 nocompile_text = { |
14 return json_parse.IsDict(thing) and thing.get('nocompile', False) | 14 'dart': 'nodart', |
15 'c++': 'nocompile' | |
16 } | |
17 | |
18 def DeleteNocompileNodes(item, lang='c++'): | |
not at google - send to devlin
2013/01/25 18:14:33
how about renaming this to DeleteNodes(item, key)
sashab
2013/01/29 08:27:13
Done; moved this language-specific decision to com
| |
19 def HasNocompile(thing, lang): | |
20 return json_parse.IsDict(thing) and thing.get(nocompile_text[lang], False) | |
15 | 21 |
16 if json_parse.IsDict(item): | 22 if json_parse.IsDict(item): |
17 toDelete = [] | 23 toDelete = [] |
18 for key, value in item.items(): | 24 for key, value in item.items(): |
19 if HasNocompile(value): | 25 if HasNocompile(value, lang): |
20 toDelete.append(key) | 26 toDelete.append(key) |
21 else: | 27 else: |
22 DeleteNocompileNodes(value) | 28 DeleteNocompileNodes(value, lang) |
23 for key in toDelete: | 29 for key in toDelete: |
24 del item[key] | 30 del item[key] |
25 elif type(item) == list: | 31 elif type(item) == list: |
26 item[:] = [DeleteNocompileNodes(thing) | 32 item[:] = [DeleteNocompileNodes(thing, lang) |
27 for thing in item if not HasNocompile(thing)] | 33 for thing in item if not HasNocompile(thing, lang)] |
28 | 34 |
29 return item | 35 return item |
30 | 36 |
31 def Load(filename): | 37 def Load(filename): |
32 with open(filename, 'r') as handle: | 38 with open(filename, 'r') as handle: |
33 schemas = json_parse.Parse(handle.read()) | 39 schemas = json_parse.Parse(handle.read()) |
34 schema_util.PrefixSchemasWithNamespace(schemas) | 40 schema_util.PrefixSchemasWithNamespace(schemas) |
35 return schemas | 41 return schemas |
36 | 42 |
37 # A dictionary mapping |filename| to the object resulting from loading the JSON | 43 # A dictionary mapping |filename| to the object resulting from loading the JSON |
38 # at |filename|. | 44 # at |filename|. |
39 _cache = {} | 45 _cache = {} |
40 | 46 |
41 def CachedLoad(filename): | 47 def CachedLoad(filename): |
42 """Equivalent to Load(filename), but caches results for subsequent calls""" | 48 """Equivalent to Load(filename), but caches results for subsequent calls""" |
43 if filename not in _cache: | 49 if filename not in _cache: |
44 _cache[filename] = Load(filename) | 50 _cache[filename] = Load(filename) |
45 # Return a copy of the object so that any changes a caller makes won't affect | 51 # Return a copy of the object so that any changes a caller makes won't affect |
46 # the next caller. | 52 # the next caller. |
47 return copy.deepcopy(_cache[filename]) | 53 return copy.deepcopy(_cache[filename]) |
OLD | NEW |