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 logging |
7 import os | 7 import os |
8 | 8 |
9 import third_party.json_schema_compiler.model as model | 9 import third_party.json_schema_compiler.model as model |
10 | 10 |
(...skipping 28 matching lines...) Expand all Loading... |
39 class HandlebarDictGenerator(object): | 39 class HandlebarDictGenerator(object): |
40 """Uses a Model from the JSON Schema Compiler and generates a dict that | 40 """Uses a Model from the JSON Schema Compiler and generates a dict that |
41 a Handlebar template can use for a data source. | 41 a Handlebar template can use for a data source. |
42 """ | 42 """ |
43 def __init__(self, json): | 43 def __init__(self, json): |
44 clean_json = copy.deepcopy(json) | 44 clean_json = copy.deepcopy(json) |
45 _RemoveNoDocs(clean_json) | 45 _RemoveNoDocs(clean_json) |
46 try: | 46 try: |
47 self._namespace = model.Namespace(clean_json, clean_json['namespace']) | 47 self._namespace = model.Namespace(clean_json, clean_json['namespace']) |
48 except Exception as e: | 48 except Exception as e: |
49 logging.info(e) | 49 logging.error(e) |
| 50 raise |
50 | 51 |
51 def _StripPrefix(self, name): | 52 def _StripPrefix(self, name): |
52 if name.startswith(self._namespace.name + '.'): | 53 if name.startswith(self._namespace.name + '.'): |
53 return name[len(self._namespace.name + '.'):] | 54 return name[len(self._namespace.name + '.'):] |
54 return name | 55 return name |
55 | 56 |
56 def _FormatDescription(self, description): | 57 def _FormatDescription(self, description): |
57 if description is None or '$ref:' not in description: | 58 if description is None or '$ref:' not in description: |
58 return description | 59 return description |
59 refs = description.split('$ref:') | 60 refs = description.split('$ref:') |
(...skipping 17 matching lines...) Expand all Loading... |
77 def Generate(self): | 78 def Generate(self): |
78 try: | 79 try: |
79 return { | 80 return { |
80 'name': self._namespace.name, | 81 'name': self._namespace.name, |
81 'types': map(self._GenerateType, self._namespace.types.values()), | 82 'types': map(self._GenerateType, self._namespace.types.values()), |
82 'functions': self._GenerateFunctions(self._namespace.functions), | 83 'functions': self._GenerateFunctions(self._namespace.functions), |
83 'events': map(self._GenerateEvent, self._namespace.events.values()), | 84 'events': map(self._GenerateEvent, self._namespace.events.values()), |
84 'properties': self._GenerateProperties(self._namespace.properties) | 85 'properties': self._GenerateProperties(self._namespace.properties) |
85 } | 86 } |
86 except Exception as e: | 87 except Exception as e: |
87 logging.info(e) | 88 logging.error(e) |
| 89 raise |
88 | 90 |
89 def _GenerateType(self, type_): | 91 def _GenerateType(self, type_): |
90 type_dict = { | 92 type_dict = { |
91 'name': self._StripPrefix(type_.name), | 93 'name': self._StripPrefix(type_.name), |
92 'description': self._FormatDescription(type_.description), | 94 'description': self._FormatDescription(type_.description), |
93 'properties': self._GenerateProperties(type_.properties), | 95 'properties': self._GenerateProperties(type_.properties), |
94 'functions': self._GenerateFunctions(type_.functions), | 96 'functions': self._GenerateFunctions(type_.functions), |
95 'events': map(self._GenerateEvent, type_.events.values()) | 97 'events': map(self._GenerateEvent, type_.events.values()) |
96 } | 98 } |
97 self._RenderTypeInformation(type_, type_dict) | 99 self._RenderTypeInformation(type_, type_dict) |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 elif property_.type_ == model.PropertyType.ENUM: | 184 elif property_.type_ == model.PropertyType.ENUM: |
183 dst_dict['enum_values'] = [] | 185 dst_dict['enum_values'] = [] |
184 for enum_value in property_.enum_values: | 186 for enum_value in property_.enum_values: |
185 dst_dict['enum_values'].append({'name': enum_value}) | 187 dst_dict['enum_values'].append({'name': enum_value}) |
186 if len(dst_dict['enum_values']) > 0: | 188 if len(dst_dict['enum_values']) > 0: |
187 dst_dict['enum_values'][-1]['last'] = True | 189 dst_dict['enum_values'][-1]['last'] = True |
188 elif property_.instance_of: | 190 elif property_.instance_of: |
189 dst_dict['simple_type'] = property_.instance_of.lower() | 191 dst_dict['simple_type'] = property_.instance_of.lower() |
190 else: | 192 else: |
191 dst_dict['simple_type'] = property_.type_.name.lower() | 193 dst_dict['simple_type'] = property_.type_.name.lower() |
OLD | NEW |