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 |
11 | 11 |
12 class NoDocError(Exception): | |
13 pass | |
14 | |
12 def _RemoveNoDocs(item): | 15 def _RemoveNoDocs(item): |
13 if type(item) == dict: | 16 if type(item) == dict: |
14 if item.get('nodoc', False): | 17 if item.get('nodoc', False): |
15 return True | 18 return True |
16 for key, value in item.items(): | 19 for key, value in item.items(): |
17 if _RemoveNoDocs(value): | 20 if _RemoveNoDocs(value): |
18 del item[key] | 21 del item[key] |
19 elif type(item) == list: | 22 elif type(item) == list: |
20 for i in item: | 23 for i in item: |
21 if _RemoveNoDocs(i): | 24 if _RemoveNoDocs(i): |
22 item.remove(i) | 25 item.remove(i) |
23 return False | 26 return False |
24 | 27 |
25 def _FormatValue(value): | 28 def _FormatValue(value): |
26 """Inserts commas every three digits for integer values. It is magic. | 29 """Inserts commas every three digits for integer values. It is magic. |
27 """ | 30 """ |
28 s = str(value) | 31 s = str(value) |
29 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) | 32 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) |
30 | 33 |
31 class HandlebarDictGenerator(object): | 34 class HandlebarDictGenerator(object): |
32 """Uses a Model from the JSON Schema Compiler and generates a dict that | 35 """Uses a Model from the JSON Schema Compiler and generates a dict that |
33 a Handlebar template can use for a data source. | 36 a Handlebar template can use for a data source. |
34 """ | 37 """ |
35 def __init__(self, json): | 38 def __init__(self, json): |
36 clean_json = copy.deepcopy(json) | 39 clean_json = copy.deepcopy(json) |
37 _RemoveNoDocs(clean_json) | 40 if _RemoveNoDocs(clean_json): |
41 raise NoDocError() | |
not at google - send to devlin
2012/08/09 05:11:36
I don't think an exception is the answer here. We
cduvall
2012/08/09 17:54:05
Done.
| |
38 try: | 42 try: |
39 self._namespace = model.Namespace(clean_json, clean_json['namespace']) | 43 self._namespace = model.Namespace(clean_json, clean_json['namespace']) |
40 except Exception as e: | 44 except Exception as e: |
not at google - send to devlin
2012/08/09 05:11:36
why do we need to log this error rather than handl
cduvall
2012/08/09 17:54:05
Done.
| |
41 logging.error(e) | 45 logging.error(e) |
42 raise | 46 raise |
43 | 47 |
44 def _StripPrefix(self, name): | 48 def _StripPrefix(self, name): |
45 if name.startswith(self._namespace.name + '.'): | 49 if name.startswith(self._namespace.name + '.'): |
46 return name[len(self._namespace.name + '.'):] | 50 return name[len(self._namespace.name + '.'):] |
47 return name | 51 return name |
48 | 52 |
49 def _FormatDescription(self, description): | 53 def _FormatDescription(self, description): |
50 if description is None or '$ref:' not in description: | 54 if description is None or '$ref:' not in description: |
(...skipping 19 matching lines...) Expand all Loading... | |
70 def Generate(self, samples): | 74 def Generate(self, samples): |
71 try: | 75 try: |
72 return { | 76 return { |
73 'name': self._namespace.name, | 77 'name': self._namespace.name, |
74 'types': map(self._GenerateType, self._namespace.types.values()), | 78 'types': map(self._GenerateType, self._namespace.types.values()), |
75 'functions': self._GenerateFunctions(self._namespace.functions), | 79 'functions': self._GenerateFunctions(self._namespace.functions), |
76 'events': map(self._GenerateEvent, self._namespace.events.values()), | 80 'events': map(self._GenerateEvent, self._namespace.events.values()), |
77 'properties': self._GenerateProperties(self._namespace.properties), | 81 'properties': self._GenerateProperties(self._namespace.properties), |
78 'samples': samples, | 82 'samples': samples, |
79 } | 83 } |
80 except Exception as e: | 84 except Exception as e: |
not at google - send to devlin
2012/08/09 05:11:36
ditto
cduvall
2012/08/09 17:54:05
Done.
| |
81 logging.error(e) | 85 logging.error(e) |
82 raise | 86 raise |
83 | 87 |
84 def _GenerateType(self, type_): | 88 def _GenerateType(self, type_): |
85 type_dict = { | 89 type_dict = { |
86 'name': self._StripPrefix(type_.name), | 90 'name': self._StripPrefix(type_.name), |
87 'description': self._FormatDescription(type_.description), | 91 'description': self._FormatDescription(type_.description), |
88 'properties': self._GenerateProperties(type_.properties), | 92 'properties': self._GenerateProperties(type_.properties), |
89 'functions': self._GenerateFunctions(type_.functions), | 93 'functions': self._GenerateFunctions(type_.functions), |
90 'events': map(self._GenerateEvent, type_.events.values()) | 94 'events': map(self._GenerateEvent, type_.events.values()) |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
177 elif property_.type_ == model.PropertyType.ENUM: | 181 elif property_.type_ == model.PropertyType.ENUM: |
178 dst_dict['enum_values'] = [] | 182 dst_dict['enum_values'] = [] |
179 for enum_value in property_.enum_values: | 183 for enum_value in property_.enum_values: |
180 dst_dict['enum_values'].append({'name': enum_value}) | 184 dst_dict['enum_values'].append({'name': enum_value}) |
181 if len(dst_dict['enum_values']) > 0: | 185 if len(dst_dict['enum_values']) > 0: |
182 dst_dict['enum_values'][-1]['last'] = True | 186 dst_dict['enum_values'][-1]['last'] = True |
183 elif property_.instance_of: | 187 elif property_.instance_of: |
184 dst_dict['simple_type'] = property_.instance_of.lower() | 188 dst_dict['simple_type'] = property_.instance_of.lower() |
185 else: | 189 else: |
186 dst_dict['simple_type'] = property_.type_.name.lower() | 190 dst_dict['simple_type'] = property_.type_.name.lower() |
OLD | NEW |