| 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 |
| 11 def _RemoveNoDocs(item): | 11 def _RemoveNoDocs(item): |
| 12 if type(item) == dict: | 12 if type(item) == dict: |
| 13 if item.get('nodoc', False): | 13 if item.get('nodoc', False): |
| 14 return True | 14 return True |
| 15 for key, value in item.items(): | 15 for key, value in item.items(): |
| 16 if _RemoveNoDocs(value): | 16 if _RemoveNoDocs(value): |
| 17 del item[key] | 17 del item[key] |
| 18 elif type(item) == list: | 18 elif type(item) == list: |
| 19 for i in item: | 19 for i in item: |
| 20 if _RemoveNoDocs(i): | 20 if _RemoveNoDocs(i): |
| 21 item.remove(i) | 21 item.remove(i) |
| 22 return False | 22 return False |
| 23 | 23 |
| 24 def _GetLinkToRefType(namespace_name, ref_type): | 24 def _GetLinkToRefType(namespace_name, ref_type): |
| 25 terms = ref_type.split('.') | 25 if ref_type.startswith(namespace_name + '.'): |
| 26 if len(terms) > 1: | 26 type_name = ref_type[len(namespace_name + '.'):] |
| 27 text = '.'.join(terms[1:]) | 27 return { 'href': '#type-' + type_name, 'text': type_name } |
| 28 href = terms[0] + '.html' + '#type-' + text | 28 elif '.' not in ref_type: |
| 29 else: | 29 return { 'href': '#type-' + ref_type, 'text': ref_type } |
| 30 href = namespace_name + '.html' + '#type-' +ref_type | 30 api, type_name = ref_type.rsplit('.', 1) |
| 31 text = ref_type | 31 return { 'href': api + '.html#type-' + type_name, 'text': ref_type } |
| 32 return ({ | |
| 33 "href": href, | |
| 34 "text": text | |
| 35 }) | |
| 36 | 32 |
| 37 def _FormatValue(value): | 33 def _FormatValue(value): |
| 38 """Inserts commas every three digits for integer values. It is magic. | 34 """Inserts commas every three digits for integer values. It is magic. |
| 39 """ | 35 """ |
| 40 s = str(value) | 36 s = str(value) |
| 41 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) | 37 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) |
| 42 | 38 |
| 43 class HandlebarDictGenerator(object): | 39 class HandlebarDictGenerator(object): |
| 44 """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 |
| 45 a Handlebar template can use for a data source. | 41 a Handlebar template can use for a data source. |
| 46 """ | 42 """ |
| 47 def __init__(self, json): | 43 def __init__(self, json): |
| 48 clean_json = copy.deepcopy(json) | 44 clean_json = copy.deepcopy(json) |
| 49 _RemoveNoDocs(clean_json) | 45 _RemoveNoDocs(clean_json) |
| 50 try: | 46 try: |
| 51 self._namespace = model.Namespace(clean_json, clean_json['namespace']) | 47 self._namespace = model.Namespace(clean_json, clean_json['namespace']) |
| 52 except Exception as e: | 48 except Exception as e: |
| 53 logging.info(e) | 49 logging.info(e) |
| 54 | 50 |
| 51 def _StripPrefix(self, name): |
| 52 if name.startswith(self._namespace.name + '.'): |
| 53 return name[len(self._namespace.name + '.'):] |
| 54 return name |
| 55 |
| 56 def _FormatDescription(self, description): |
| 57 if description is None or '$ref:' not in description: |
| 58 return description |
| 59 refs = description.split('$ref:') |
| 60 formatted_description = [refs[0]] |
| 61 for ref in refs[1:]: |
| 62 parts = ref.split(' ', 1) |
| 63 if len(parts) == 1: |
| 64 ref = parts[0] |
| 65 rest = '' |
| 66 else: |
| 67 ref, rest = parts |
| 68 rest = ' ' + rest |
| 69 if not ref[-1].isalnum(): |
| 70 rest = ref[-1] + rest |
| 71 ref = ref[:-1] |
| 72 ref_dict = _GetLinkToRefType(self._namespace.name, ref) |
| 73 formatted_description.append('<a href="%(href)s">%(text)s</a>%(rest)s' % |
| 74 { 'href': ref_dict['href'], 'text': ref_dict['text'], 'rest': rest }) |
| 75 return ''.join(formatted_description) |
| 76 |
| 55 def Generate(self): | 77 def Generate(self): |
| 56 try: | 78 try: |
| 57 return { | 79 return { |
| 58 'name': self._namespace.name, | 80 'name': self._namespace.name, |
| 59 'types': map(self._GenerateType, self._namespace.types.values()), | 81 'types': map(self._GenerateType, self._namespace.types.values()), |
| 60 'functions': self._GenerateFunctions(self._namespace.functions), | 82 'functions': self._GenerateFunctions(self._namespace.functions), |
| 61 'events': map(self._GenerateEvent, self._namespace.events.values()), | 83 'events': map(self._GenerateEvent, self._namespace.events.values()), |
| 62 'properties': self._GenerateProperties(self._namespace.properties) | 84 'properties': self._GenerateProperties(self._namespace.properties) |
| 63 } | 85 } |
| 64 except Exception as e: | 86 except Exception as e: |
| 65 logging.info(e) | 87 logging.info(e) |
| 66 | 88 |
| 67 def _GenerateType(self, type_): | 89 def _GenerateType(self, type_): |
| 68 type_dict = { | 90 type_dict = { |
| 69 'name': type_.name, | 91 'name': self._StripPrefix(type_.name), |
| 70 'description': type_.description, | 92 'description': self._FormatDescription(type_.description), |
| 71 'properties': self._GenerateProperties(type_.properties), | 93 'properties': self._GenerateProperties(type_.properties), |
| 72 'functions': self._GenerateFunctions(type_.functions), | 94 'functions': self._GenerateFunctions(type_.functions), |
| 73 'events': map(self._GenerateEvent, type_.events.values()) | 95 'events': map(self._GenerateEvent, type_.events.values()) |
| 74 } | 96 } |
| 75 self._RenderTypeInformation(type_, type_dict) | 97 self._RenderTypeInformation(type_, type_dict) |
| 76 return type_dict | 98 return type_dict |
| 77 | 99 |
| 78 def _GenerateFunctions(self, functions): | 100 def _GenerateFunctions(self, functions): |
| 79 return map(self._GenerateFunction, functions.values()) | 101 return map(self._GenerateFunction, functions.values()) |
| 80 | 102 |
| 81 def _GenerateFunction(self, function): | 103 def _GenerateFunction(self, function): |
| 82 function_dict = { | 104 function_dict = { |
| 83 'name': function.name, | 105 'name': function.name, |
| 84 'description': function.description, | 106 'description': self._FormatDescription(function.description), |
| 85 'callback': self._GenerateCallback(function.callback), | 107 'callback': self._GenerateCallback(function.callback), |
| 86 'parameters': [], | 108 'parameters': [], |
| 87 'returns': None | 109 'returns': None |
| 88 } | 110 } |
| 89 if function.returns: | 111 if function.returns: |
| 90 function_dict['returns'] = self._GenerateProperty(function.returns) | 112 function_dict['returns'] = self._GenerateProperty(function.returns) |
| 91 for param in function.params: | 113 for param in function.params: |
| 92 function_dict['parameters'].append(self._GenerateProperty(param)) | 114 function_dict['parameters'].append(self._GenerateProperty(param)) |
| 93 if function_dict['callback']: | 115 if function_dict['callback']: |
| 94 function_dict['parameters'].append(function_dict['callback']) | 116 function_dict['parameters'].append(function_dict['callback']) |
| 95 if len(function_dict['parameters']) > 0: | 117 if len(function_dict['parameters']) > 0: |
| 96 function_dict['parameters'][-1]['last'] = True | 118 function_dict['parameters'][-1]['last'] = True |
| 97 return function_dict | 119 return function_dict |
| 98 | 120 |
| 99 def _GenerateEvent(self, event): | 121 def _GenerateEvent(self, event): |
| 100 event_dict = { | 122 event_dict = { |
| 101 'name': event.name, | 123 'name': self._StripPrefix(event.name), |
| 102 'description': event.description, | 124 'description': self._FormatDescription(event.description), |
| 103 'parameters': map(self._GenerateProperty, event.params) | 125 'parameters': map(self._GenerateProperty, event.params) |
| 104 } | 126 } |
| 105 if len(event_dict['parameters']) > 0: | 127 if len(event_dict['parameters']) > 0: |
| 106 event_dict['parameters'][-1]['last'] = True | 128 event_dict['parameters'][-1]['last'] = True |
| 107 return event_dict | 129 return event_dict |
| 108 | 130 |
| 109 def _GenerateCallback(self, callback): | 131 def _GenerateCallback(self, callback): |
| 110 if not callback: | 132 if not callback: |
| 111 return None | 133 return None |
| 112 callback_dict = { | 134 callback_dict = { |
| 113 'name': 'callback', | 135 'name': 'callback', |
| 114 'description': callback.description, | 136 'description': self._FormatDescription(callback.description), |
| 115 'simple_type': {'simple_type': 'function'}, | 137 'simple_type': {'simple_type': 'function'}, |
| 116 'optional': callback.optional, | 138 'optional': callback.optional, |
| 117 'parameters': [] | 139 'parameters': [] |
| 118 } | 140 } |
| 119 for param in callback.params: | 141 for param in callback.params: |
| 120 callback_dict['parameters'].append(self._GenerateProperty(param)) | 142 callback_dict['parameters'].append(self._GenerateProperty(param)) |
| 121 if (len(callback_dict['parameters']) > 0): | 143 if (len(callback_dict['parameters']) > 0): |
| 122 callback_dict['parameters'][-1]['last'] = True | 144 callback_dict['parameters'][-1]['last'] = True |
| 123 return callback_dict | 145 return callback_dict |
| 124 | 146 |
| 125 def _GenerateProperties(self, properties): | 147 def _GenerateProperties(self, properties): |
| 126 return map(self._GenerateProperty, properties.values()) | 148 return map(self._GenerateProperty, properties.values()) |
| 127 | 149 |
| 128 def _GenerateProperty(self, property_): | 150 def _GenerateProperty(self, property_): |
| 129 property_dict = { | 151 property_dict = { |
| 130 'name': property_.name, | 152 'name': self._StripPrefix(property_.name), |
| 131 'optional': property_.optional, | 153 'optional': property_.optional, |
| 132 'description': property_.description, | 154 'description': self._FormatDescription(property_.description), |
| 133 'properties': self._GenerateProperties(property_.properties), | 155 'properties': self._GenerateProperties(property_.properties), |
| 134 'functions': self._GenerateFunctions(property_.functions) | 156 'functions': self._GenerateFunctions(property_.functions) |
| 135 } | 157 } |
| 136 if property_.has_value: | 158 if property_.has_value: |
| 137 if isinstance(property_.value, int): | 159 if isinstance(property_.value, int): |
| 138 property_dict['value'] = _FormatValue(property_.value) | 160 property_dict['value'] = _FormatValue(property_.value) |
| 139 else: | 161 else: |
| 140 property_dict['value'] = property_.value | 162 property_dict['value'] = property_.value |
| 141 else: | 163 else: |
| 142 self._RenderTypeInformation(property_, property_dict) | 164 self._RenderTypeInformation(property_, property_dict) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 160 elif property_.type_ == model.PropertyType.ENUM: | 182 elif property_.type_ == model.PropertyType.ENUM: |
| 161 dst_dict['enum_values'] = [] | 183 dst_dict['enum_values'] = [] |
| 162 for enum_value in property_.enum_values: | 184 for enum_value in property_.enum_values: |
| 163 dst_dict['enum_values'].append({'name': enum_value}) | 185 dst_dict['enum_values'].append({'name': enum_value}) |
| 164 if len(dst_dict['enum_values']) > 0: | 186 if len(dst_dict['enum_values']) > 0: |
| 165 dst_dict['enum_values'][-1]['last'] = True | 187 dst_dict['enum_values'][-1]['last'] = True |
| 166 elif property_.instance_of: | 188 elif property_.instance_of: |
| 167 dst_dict['simple_type'] = property_.instance_of.lower() | 189 dst_dict['simple_type'] = property_.instance_of.lower() |
| 168 else: | 190 else: |
| 169 dst_dict['simple_type'] = property_.type_.name.lower() | 191 dst_dict['simple_type'] = property_.type_.name.lower() |
| OLD | NEW |