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