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 from docs_server_utils import GetLinkToRefType | 9 from docs_server_utils import GetLinkToRefType |
10 import third_party.json_schema_compiler.model as model | 10 import third_party.json_schema_compiler.model as model |
(...skipping 14 matching lines...) Expand all Loading... | |
25 def _FormatValue(value): | 25 def _FormatValue(value): |
26 """Inserts commas every three digits for integer values. It is magic. | 26 """Inserts commas every three digits for integer values. It is magic. |
27 """ | 27 """ |
28 s = str(value) | 28 s = str(value) |
29 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) | 29 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) |
30 | 30 |
31 class HandlebarDictGenerator(object): | 31 class HandlebarDictGenerator(object): |
32 """Uses a Model from the JSON Schema Compiler and generates a dict that | 32 """Uses a Model from the JSON Schema Compiler and generates a dict that |
33 a Handlebar template can use for a data source. | 33 a Handlebar template can use for a data source. |
34 """ | 34 """ |
35 def __init__(self, json): | 35 def __init__(self, json, samples): |
36 self._samples = samples | |
not at google - send to devlin
2012/08/01 10:09:19
see example I mentioned
chebert
2012/08/01 18:04:33
Done.
| |
36 clean_json = copy.deepcopy(json) | 37 clean_json = copy.deepcopy(json) |
37 _RemoveNoDocs(clean_json) | 38 _RemoveNoDocs(clean_json) |
38 try: | 39 try: |
39 self._namespace = model.Namespace(clean_json, clean_json['namespace']) | 40 self._namespace = model.Namespace(clean_json, clean_json['namespace']) |
40 except Exception as e: | 41 except Exception as e: |
41 logging.error(e) | 42 logging.error(e) |
42 raise | 43 raise |
43 | 44 |
44 def _StripPrefix(self, name): | 45 def _StripPrefix(self, name): |
45 if name.startswith(self._namespace.name + '.'): | 46 if name.startswith(self._namespace.name + '.'): |
(...skipping 21 matching lines...) Expand all Loading... | |
67 { 'href': ref_dict['href'], 'text': ref_dict['text'], 'rest': rest }) | 68 { 'href': ref_dict['href'], 'text': ref_dict['text'], 'rest': rest }) |
68 return ''.join(formatted_description) | 69 return ''.join(formatted_description) |
69 | 70 |
70 def Generate(self): | 71 def Generate(self): |
71 try: | 72 try: |
72 return { | 73 return { |
73 'name': self._namespace.name, | 74 'name': self._namespace.name, |
74 'types': map(self._GenerateType, self._namespace.types.values()), | 75 'types': map(self._GenerateType, self._namespace.types.values()), |
75 'functions': self._GenerateFunctions(self._namespace.functions), | 76 'functions': self._GenerateFunctions(self._namespace.functions), |
76 'events': map(self._GenerateEvent, self._namespace.events.values()), | 77 'events': map(self._GenerateEvent, self._namespace.events.values()), |
77 'properties': self._GenerateProperties(self._namespace.properties) | 78 'properties': self._GenerateProperties(self._namespace.properties), |
79 'samples': self._FilterSamples(self._samples, self._namespace.name) | |
78 } | 80 } |
79 except Exception as e: | 81 except Exception as e: |
80 logging.error(e) | 82 logging.error(e) |
81 raise | 83 raise |
82 | 84 |
85 def _FilterSamples(self, samples, api_name): | |
86 api_search = '.' + api_name + '.' | |
87 return [sample for sample in samples | |
88 if any(api_search in api['name'] for api in sample['api_calls'])] | |
89 | |
83 def _GenerateType(self, type_): | 90 def _GenerateType(self, type_): |
84 type_dict = { | 91 type_dict = { |
85 'name': self._StripPrefix(type_.name), | 92 'name': self._StripPrefix(type_.name), |
86 'description': self._FormatDescription(type_.description), | 93 'description': self._FormatDescription(type_.description), |
87 'properties': self._GenerateProperties(type_.properties), | 94 'properties': self._GenerateProperties(type_.properties), |
88 'functions': self._GenerateFunctions(type_.functions), | 95 'functions': self._GenerateFunctions(type_.functions), |
89 'events': map(self._GenerateEvent, type_.events.values()) | 96 'events': map(self._GenerateEvent, type_.events.values()) |
90 } | 97 } |
91 self._RenderTypeInformation(type_, type_dict) | 98 self._RenderTypeInformation(type_, type_dict) |
92 return type_dict | 99 return type_dict |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
176 elif property_.type_ == model.PropertyType.ENUM: | 183 elif property_.type_ == model.PropertyType.ENUM: |
177 dst_dict['enum_values'] = [] | 184 dst_dict['enum_values'] = [] |
178 for enum_value in property_.enum_values: | 185 for enum_value in property_.enum_values: |
179 dst_dict['enum_values'].append({'name': enum_value}) | 186 dst_dict['enum_values'].append({'name': enum_value}) |
180 if len(dst_dict['enum_values']) > 0: | 187 if len(dst_dict['enum_values']) > 0: |
181 dst_dict['enum_values'][-1]['last'] = True | 188 dst_dict['enum_values'][-1]['last'] = True |
182 elif property_.instance_of: | 189 elif property_.instance_of: |
183 dst_dict['simple_type'] = property_.instance_of.lower() | 190 dst_dict['simple_type'] = property_.instance_of.lower() |
184 else: | 191 else: |
185 dst_dict['simple_type'] = property_.type_.name.lower() | 192 dst_dict['simple_type'] = property_.type_.name.lower() |
OLD | NEW |