Chromium Code Reviews| 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 16 matching lines...) Expand all Loading... | |
| 27 text = '.'.join(terms[1:]) | 27 text = '.'.join(terms[1:]) |
| 28 href = terms[0] + '.html' + '#type-' + text | 28 href = terms[0] + '.html' + '#type-' + text |
| 29 else: | 29 else: |
| 30 href = namespace_name + '.html' + '#type-' +ref_type | 30 href = namespace_name + '.html' + '#type-' +ref_type |
| 31 text = ref_type | 31 text = ref_type |
| 32 return ({ | 32 return ({ |
| 33 "href": href, | 33 "href": href, |
| 34 "text": text | 34 "text": text |
| 35 }) | 35 }) |
| 36 | 36 |
| 37 def _FormatValue(value): | |
| 38 s = str(value) | |
| 39 return ('<code>%s</code>' % | |
|
not at google - send to devlin
2012/07/09 11:01:20
All HTML should be the concern of the templates, n
cduvall
2012/07/09 17:50:40
Done.
| |
| 40 ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1])) | |
|
not at google - send to devlin
2012/07/09 11:01:20
wow this is voodoo...
| |
| 41 | |
| 37 class HandlebarDictGenerator(object): | 42 class HandlebarDictGenerator(object): |
| 38 """Uses a Model from the JSON Schema Compiler and generates a dict that | 43 """Uses a Model from the JSON Schema Compiler and generates a dict that |
| 39 a Handlebar template can use for a data source. | 44 a Handlebar template can use for a data source. |
| 40 """ | 45 """ |
| 41 def __init__(self, json): | 46 def __init__(self, json): |
| 42 clean_json = copy.deepcopy(json) | 47 clean_json = copy.deepcopy(json) |
| 43 _RemoveNoDocs(clean_json) | 48 _RemoveNoDocs(clean_json) |
| 44 try: | 49 try: |
| 45 self._namespace = model.Namespace(clean_json, clean_json['namespace']) | 50 self._namespace = model.Namespace(clean_json, clean_json['namespace']) |
| 46 except Exception as e: | 51 except Exception as e: |
| 47 logging.info(e) | 52 logging.info(e) |
| 48 | 53 |
| 49 def Generate(self): | 54 def Generate(self): |
| 50 try: | 55 try: |
| 51 return { | 56 return { |
| 52 'name': self._namespace.name, | 57 'name': self._namespace.name, |
| 53 'types': self._GenerateTypes(self._namespace.types), | 58 'types': self._GenerateTypes(self._namespace.types), |
| 54 'functions': self._GenerateFunctions(self._namespace.functions) | 59 'functions': self._GenerateFunctions(self._namespace.functions), |
| 60 'properties': self._GenerateProperties(self._namespace.properties) | |
| 55 } | 61 } |
| 56 except Exception as e: | 62 except Exception as e: |
| 57 logging.info(e) | 63 logging.info(e) |
| 58 | 64 |
| 59 def _GenerateTypes(self, types): | 65 def _GenerateTypes(self, types): |
| 60 types_list = [] | 66 types_list = [] |
| 61 for type_name in types: | 67 for type_name in types: |
| 62 types_list.append(self._GenerateType(types[type_name])) | 68 types_list.append(self._GenerateType(types[type_name])) |
| 63 return types_list | 69 return types_list |
| 64 | 70 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 91 function_dict['parameters'].append(function_dict['callback']) | 97 function_dict['parameters'].append(function_dict['callback']) |
| 92 if len(function_dict['parameters']) > 0: | 98 if len(function_dict['parameters']) > 0: |
| 93 function_dict['parameters'][-1]['last_item'] = True | 99 function_dict['parameters'][-1]['last_item'] = True |
| 94 return function_dict | 100 return function_dict |
| 95 | 101 |
| 96 def _GenerateCallback(self, callback): | 102 def _GenerateCallback(self, callback): |
| 97 if not callback: | 103 if not callback: |
| 98 return {} | 104 return {} |
| 99 callback_dict = { | 105 callback_dict = { |
| 100 'name': 'callback', | 106 'name': 'callback', |
| 107 'description': callback.description, | |
| 101 'simple_type': {'type': 'function'}, | 108 'simple_type': {'type': 'function'}, |
| 102 'optional': callback.optional, | 109 'optional': callback.optional, |
| 103 'parameters': [] | 110 'parameters': [] |
| 104 } | 111 } |
| 105 for param in callback.params: | 112 for param in callback.params: |
| 106 callback_dict['parameters'].append(self._GenerateProperty(param)) | 113 callback_dict['parameters'].append(self._GenerateProperty(param)) |
| 107 if (len(callback_dict['parameters']) > 0): | 114 if (len(callback_dict['parameters']) > 0): |
| 108 callback_dict['parameters'][-1]['last_parameter'] = True | 115 callback_dict['parameters'][-1]['last_parameter'] = True |
| 109 return callback_dict | 116 return callback_dict |
| 110 | 117 |
| 111 def _GenerateProperties(self, properties): | 118 def _GenerateProperties(self, properties): |
| 112 properties_list = [] | 119 properties_list = [] |
| 113 for property_name in properties: | 120 for property_name in properties: |
| 114 properties_list.append(self._GenerateProperty(properties[property_name])) | 121 properties_list.append(self._GenerateProperty(properties[property_name])) |
| 115 return properties_list | 122 return properties_list |
| 116 | 123 |
| 117 def _GenerateProperty(self, property_): | 124 def _GenerateProperty(self, property_): |
| 118 property_dict = { | 125 property_dict = { |
| 119 'name': property_.name, | 126 'name': property_.name, |
| 120 'optional': property_.optional, | 127 'optional': property_.optional, |
| 121 'description': property_.description, | 128 'description': property_.description, |
| 122 'properties': self._GenerateProperties(property_.properties) | 129 'properties': self._GenerateProperties(property_.properties), |
| 130 'functions': self._GenerateFunctions(property_.functions) | |
| 123 } | 131 } |
| 124 self._RenderTypeInformation(property_, property_dict) | 132 self._RenderTypeInformation(property_, property_dict) |
| 133 self._RenderValue(property_.value, property_dict) | |
| 125 return property_dict | 134 return property_dict |
| 126 | 135 |
| 136 def _RenderValue(self, value, dst_dict): | |
| 137 if 'simple_type' in dst_dict and isinstance(value, int): | |
| 138 dst_dict['simple_type']['type'] = _FormatValue(value) | |
|
not at google - send to devlin
2012/07/09 11:01:20
I think just incorporate this into _RenderTypeInfo
cduvall
2012/07/09 17:50:40
Done.
| |
| 139 | |
| 127 def _RenderTypeInformation(self, property_, dst_dict): | 140 def _RenderTypeInformation(self, property_, dst_dict): |
| 128 dst_dict['type'] = property_.type_.name.lower() | 141 dst_dict['type'] = property_.type_.name.lower() |
| 129 if property_.type_ == model.PropertyType.CHOICES: | 142 if property_.type_ == model.PropertyType.CHOICES: |
|
not at google - send to devlin
2012/07/09 11:01:20
here:
if property_.has_value:
if isinstance(pro
cduvall
2012/07/09 17:50:40
Done.
| |
| 130 dst_dict['choices'] = [] | 143 dst_dict['choices'] = [] |
| 131 for choice_name in property_.choices: | 144 for choice_name in property_.choices: |
| 132 dst_dict['choices'].append(self._GenerateProperty( | 145 dst_dict['choices'].append(self._GenerateProperty( |
| 133 property_.choices[choice_name])) | 146 property_.choices[choice_name])) |
| 134 # We keep track of which is last for knowing when to add "or" between | 147 # We keep track of which is last for knowing when to add "or" between |
| 135 # choices in templates. | 148 # choices in templates. |
| 136 if len(dst_dict['choices']) > 0: | 149 if len(dst_dict['choices']) > 0: |
| 137 dst_dict['choices'][-1]['last_choice'] = True | 150 dst_dict['choices'][-1]['last_choice'] = True |
| 138 elif property_.type_ == model.PropertyType.REF: | 151 elif property_.type_ == model.PropertyType.REF: |
| 139 dst_dict['link'] = _GetLinkToRefType(self._namespace.name, | 152 dst_dict['link'] = _GetLinkToRefType(self._namespace.name, |
| 140 property_.ref_type) | 153 property_.ref_type) |
| 141 elif property_.type_ == model.PropertyType.ARRAY: | 154 elif property_.type_ == model.PropertyType.ARRAY: |
| 142 dst_dict['array'] = self._GenerateProperty(property_.item_type) | 155 dst_dict['array'] = self._GenerateProperty(property_.item_type) |
| 143 else: | 156 else: |
| 144 dst_dict['simple_type'] = {'type': dst_dict['type']} | 157 dst_dict['simple_type'] = {'type': dst_dict['type']} |
| OLD | NEW |