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, samples): | 35 def __init__(self, json): |
36 self._samples = samples | |
37 clean_json = copy.deepcopy(json) | 36 clean_json = copy.deepcopy(json) |
38 _RemoveNoDocs(clean_json) | 37 _RemoveNoDocs(clean_json) |
39 try: | 38 try: |
40 self._namespace = model.Namespace(clean_json, clean_json['namespace']) | 39 self._namespace = model.Namespace(clean_json, clean_json['namespace']) |
41 except Exception as e: | 40 except Exception as e: |
42 logging.error(e) | 41 logging.error(e) |
43 raise | 42 raise |
44 | 43 |
45 def _StripPrefix(self, name): | 44 def _StripPrefix(self, name): |
46 if name.startswith(self._namespace.name + '.'): | 45 if name.startswith(self._namespace.name + '.'): |
(...skipping 14 matching lines...) Expand all Loading... |
61 ref, rest = parts | 60 ref, rest = parts |
62 rest = ' ' + rest | 61 rest = ' ' + rest |
63 if not ref[-1].isalnum(): | 62 if not ref[-1].isalnum(): |
64 rest = ref[-1] + rest | 63 rest = ref[-1] + rest |
65 ref = ref[:-1] | 64 ref = ref[:-1] |
66 ref_dict = GetLinkToRefType(self._namespace.name, ref) | 65 ref_dict = GetLinkToRefType(self._namespace.name, ref) |
67 formatted_description.append('<a href="%(href)s">%(text)s</a>%(rest)s' % | 66 formatted_description.append('<a href="%(href)s">%(text)s</a>%(rest)s' % |
68 { 'href': ref_dict['href'], 'text': ref_dict['text'], 'rest': rest }) | 67 { 'href': ref_dict['href'], 'text': ref_dict['text'], 'rest': rest }) |
69 return ''.join(formatted_description) | 68 return ''.join(formatted_description) |
70 | 69 |
71 def Generate(self): | 70 def Generate(self, samples): |
72 try: | 71 return { |
73 return { | 72 'name': self._namespace.name, |
74 'name': self._namespace.name, | 73 'types': map(self._GenerateType, self._namespace.types.values()), |
75 'types': map(self._GenerateType, self._namespace.types.values()), | 74 'functions': self._GenerateFunctions(self._namespace.functions), |
76 'functions': self._GenerateFunctions(self._namespace.functions), | 75 'events': map(self._GenerateEvent, self._namespace.events.values()), |
77 'events': map(self._GenerateEvent, self._namespace.events.values()), | 76 'properties': self._GenerateProperties(self._namespace.properties), |
78 'properties': self._GenerateProperties(self._namespace.properties), | 77 'samples': self._FilterSamples(samples), |
79 'samples': self._FilterSamples(self._samples, self._namespace.name) | 78 } |
80 } | |
81 except Exception as e: | |
82 logging.error(e) | |
83 raise | |
84 | 79 |
85 def _FilterSamples(self, samples, api_name): | 80 def _FilterSamples(self, samples): |
86 api_search = '.' + api_name + '.' | 81 api_search = '.' + self._namespace.name + '.' |
87 return [sample for sample in samples | 82 return [sample for sample in samples |
88 if any(api_search in api['name'] for api in sample['api_calls'])] | 83 if any(api_search in api['name'] for api in sample['api_calls'])] |
89 | 84 |
90 def _GenerateType(self, type_): | 85 def _GenerateType(self, type_): |
91 type_dict = { | 86 type_dict = { |
92 'name': self._StripPrefix(type_.name), | 87 'name': self._StripPrefix(type_.name), |
93 'description': self._FormatDescription(type_.description), | 88 'description': self._FormatDescription(type_.description), |
94 'properties': self._GenerateProperties(type_.properties), | 89 'properties': self._GenerateProperties(type_.properties), |
95 'functions': self._GenerateFunctions(type_.functions), | 90 'functions': self._GenerateFunctions(type_.functions), |
96 'events': map(self._GenerateEvent, type_.events.values()) | 91 'events': map(self._GenerateEvent, type_.events.values()) |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 elif property_.type_ == model.PropertyType.ENUM: | 178 elif property_.type_ == model.PropertyType.ENUM: |
184 dst_dict['enum_values'] = [] | 179 dst_dict['enum_values'] = [] |
185 for enum_value in property_.enum_values: | 180 for enum_value in property_.enum_values: |
186 dst_dict['enum_values'].append({'name': enum_value}) | 181 dst_dict['enum_values'].append({'name': enum_value}) |
187 if len(dst_dict['enum_values']) > 0: | 182 if len(dst_dict['enum_values']) > 0: |
188 dst_dict['enum_values'][-1]['last'] = True | 183 dst_dict['enum_values'][-1]['last'] = True |
189 elif property_.instance_of: | 184 elif property_.instance_of: |
190 dst_dict['simple_type'] = property_.instance_of.lower() | 185 dst_dict['simple_type'] = property_.instance_of.lower() |
191 else: | 186 else: |
192 dst_dict['simple_type'] = property_.type_.name.lower() | 187 dst_dict['simple_type'] = property_.type_.name.lower() |
OLD | NEW |