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 logging |
6 import os | 7 import os |
7 | 8 |
8 import third_party.json_schema_compiler.model as model | 9 import third_party.json_schema_compiler.model as model |
9 | 10 |
10 def _RemoveNoDocs(item): | 11 def _RemoveNoDocs(item): |
11 if type(item) == dict: | 12 if type(item) == dict: |
12 if item.get('nodoc', False): | 13 if item.get('nodoc', False): |
13 return True | 14 return True |
14 for key, value in item.items(): | 15 for key, value in item.items(): |
15 if _RemoveNoDocs(value): | 16 if _RemoveNoDocs(value): |
(...skipping 17 matching lines...) Expand all Loading... |
33 "text": text | 34 "text": text |
34 }) | 35 }) |
35 | 36 |
36 class HandlebarDictGenerator(object): | 37 class HandlebarDictGenerator(object): |
37 """Uses a Model from the JSON Schema Compiler and generates a dict that | 38 """Uses a Model from the JSON Schema Compiler and generates a dict that |
38 a Handlebar template can use for a data source. | 39 a Handlebar template can use for a data source. |
39 """ | 40 """ |
40 def __init__(self, json): | 41 def __init__(self, json): |
41 clean_json = copy.deepcopy(json) | 42 clean_json = copy.deepcopy(json) |
42 _RemoveNoDocs(clean_json) | 43 _RemoveNoDocs(clean_json) |
43 self._namespace = model.Namespace(clean_json, clean_json['namespace']) | 44 try: |
| 45 self._namespace = model.Namespace(clean_json, clean_json['namespace']) |
| 46 except Exception as e: |
| 47 logging.info(e) |
44 | 48 |
45 def Generate(self): | 49 def Generate(self): |
46 return { | 50 try: |
47 'name': self._namespace.name, | 51 return { |
48 'types': self._GenerateTypes(self._namespace.types), | 52 'name': self._namespace.name, |
49 'functions': self._GenerateFunctions(self._namespace.functions) | 53 'types': self._GenerateTypes(self._namespace.types), |
50 } | 54 'functions': self._GenerateFunctions(self._namespace.functions) |
| 55 } |
| 56 except Exception as e: |
| 57 logging.info(e) |
51 | 58 |
52 def _GenerateTypes(self, types): | 59 def _GenerateTypes(self, types): |
53 types_list = [] | 60 types_list = [] |
54 for type_name in types: | 61 for type_name in types: |
55 types_list.append(self._GenerateType(types[type_name])) | 62 types_list.append(self._GenerateType(types[type_name])) |
56 return types_list | 63 return types_list |
57 | 64 |
58 def _GenerateType(self, type_): | 65 def _GenerateType(self, type_): |
59 type_dict = { | 66 type_dict = { |
60 'name': type_.name, | 67 'name': type_.name, |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 # choices in templates. | 135 # choices in templates. |
129 if len(dst_dict['choices']) > 0: | 136 if len(dst_dict['choices']) > 0: |
130 dst_dict['choices'][-1]['last_choice'] = True | 137 dst_dict['choices'][-1]['last_choice'] = True |
131 elif property_.type_ == model.PropertyType.REF: | 138 elif property_.type_ == model.PropertyType.REF: |
132 dst_dict['link'] = _GetLinkToRefType(self._namespace.name, | 139 dst_dict['link'] = _GetLinkToRefType(self._namespace.name, |
133 property_.ref_type) | 140 property_.ref_type) |
134 elif property_.type_ == model.PropertyType.ARRAY: | 141 elif property_.type_ == model.PropertyType.ARRAY: |
135 dst_dict['array'] = self._GenerateProperty(property_.item_type) | 142 dst_dict['array'] = self._GenerateProperty(property_.item_type) |
136 else: | 143 else: |
137 dst_dict['simple_type'] = {'type': dst_dict['type']} | 144 dst_dict['simple_type'] = {'type': dst_dict['type']} |
OLD | NEW |