Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1387)

Side by Side Diff: chrome/common/extensions/docs/server2/handlebar_dict_generator.py

Issue 11018003: Extensions Docs Server: Server code for showing properties of properties (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/common/extensions/docs/server2/handlebar_dict_generator_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 os 6 import os
7 7
8 from docs_server_utils import GetLinkToRefType 8 from docs_server_utils import GetLinkToRefType
9 import third_party.json_schema_compiler.model as model 9 import third_party.json_schema_compiler.model as model
10 10
11 def _RemoveNoDocs(item): 11 def _RemoveNoDocs(item):
12 if type(item) == dict: 12 if type(item) == dict:
13 if item.get('nodoc', False): 13 if item.get('nodoc', False):
14 return True 14 return True
15 for key, value in item.items(): 15 for key, value in item.items():
16 if _RemoveNoDocs(value): 16 if _RemoveNoDocs(value):
17 del item[key] 17 del item[key]
18 elif type(item) == list: 18 elif type(item) == list:
19 for i in item: 19 for i in item:
20 if _RemoveNoDocs(i): 20 if _RemoveNoDocs(i):
21 item.remove(i) 21 item.remove(i)
22 return False 22 return False
23 23
24 def _CreateId(name, parent, prefix):
not at google - send to devlin 2012/10/03 01:07:26 It would be a bit nicer to have parent be an optio
cduvall 2012/10/05 01:13:26 Done.
25 if parent:
not at google - send to devlin 2012/10/03 01:07:26 "is not None" or whatever?
cduvall 2012/10/05 01:13:26 Done.
26 return '-'.join([prefix, parent, name])
27 return '-'.join([prefix, name])
28
24 def _FormatValue(value): 29 def _FormatValue(value):
25 """Inserts commas every three digits for integer values. It is magic. 30 """Inserts commas every three digits for integer values. It is magic.
26 """ 31 """
27 s = str(value) 32 s = str(value)
28 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) 33 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1])
29 34
30 class HandlebarDictGenerator(object): 35 class HandlebarDictGenerator(object):
31 """Uses a Model from the JSON Schema Compiler and generates a dict that 36 """Uses a Model from the JSON Schema Compiler and generates a dict that
32 a Handlebar template can use for a data source. 37 a Handlebar template can use for a data source.
33 """ 38 """
(...skipping 28 matching lines...) Expand all
62 ref_dict = GetLinkToRefType(self._namespace.name, ref) 67 ref_dict = GetLinkToRefType(self._namespace.name, ref)
63 formatted_description.append('<a href="%(href)s">%(text)s</a>%(rest)s' % 68 formatted_description.append('<a href="%(href)s">%(text)s</a>%(rest)s' %
64 { 'href': ref_dict['href'], 'text': ref_dict['text'], 'rest': rest }) 69 { 'href': ref_dict['href'], 'text': ref_dict['text'], 'rest': rest })
65 return ''.join(formatted_description) 70 return ''.join(formatted_description)
66 71
67 def Generate(self): 72 def Generate(self):
68 if self._namespace is None: 73 if self._namespace is None:
69 return {} 74 return {}
70 return { 75 return {
71 'name': self._namespace.name, 76 'name': self._namespace.name,
72 'types': map(self._GenerateType, self._namespace.types.values()), 77 'types': [self._GenerateType(t) for t in self._namespace.types.values()
73 'functions': self._GenerateFunctions(self._namespace.functions), 78 if t.type_ != model.PropertyType.ADDITIONAL_PROPERTIES],
74 'events': map(self._GenerateEvent, self._namespace.events.values()), 79 'functions': self._GenerateFunctions(self._namespace.functions, None),
75 'properties': self._GenerateProperties(self._namespace.properties) 80 'events': self._GenerateEvents(self._namespace.events, None),
81 'properties': self._GenerateProperties(self._namespace.properties, None)
76 } 82 }
77 83
78 def _GenerateType(self, type_): 84 def _GenerateType(self, type_):
85 name = self._StripPrefix(type_.name)
79 type_dict = { 86 type_dict = {
80 'name': self._StripPrefix(type_.name), 87 'name': name,
81 'description': self._FormatDescription(type_.description), 88 'description': self._FormatDescription(type_.description),
82 'properties': self._GenerateProperties(type_.properties), 89 'properties': self._GenerateProperties(type_.properties, name),
83 'functions': self._GenerateFunctions(type_.functions), 90 'functions': self._GenerateFunctions(type_.functions, name),
84 'events': map(self._GenerateEvent, type_.events.values()) 91 'events': self._GenerateEvents(type_.events, name),
92 'id': _CreateId(name, None, 'type')
85 } 93 }
86 self._RenderTypeInformation(type_, type_dict) 94 self._RenderTypeInformation(type_, type_dict)
87 return type_dict 95 return type_dict
88 96
89 def _GenerateFunctions(self, functions): 97 def _GenerateFunctions(self, functions, parent):
not at google - send to devlin 2012/10/03 01:07:26 Again, make parent explicitly optional, though you
cduvall 2012/10/05 01:13:26 Done.
90 return map(self._GenerateFunction, functions.values()) 98 return [self._GenerateFunction(f, parent) for f in functions.values()]
91 99
92 def _GenerateFunction(self, function): 100 def _GenerateFunction(self, function, parent):
93 function_dict = { 101 function_dict = {
94 'name': function.name, 102 'name': function.name,
95 'description': self._FormatDescription(function.description), 103 'description': self._FormatDescription(function.description),
96 'callback': self._GenerateCallback(function.callback), 104 'callback': self._GenerateCallback(function.callback),
97 'parameters': [], 105 'parameters': [],
98 'returns': None 106 'returns': None,
107 'parent': parent,
108 'id': _CreateId(function.name, parent, 'method')
99 } 109 }
100 if function.returns: 110 if function.returns:
101 function_dict['returns'] = self._GenerateProperty(function.returns) 111 function_dict['returns'] = self._GenerateProperty(function.returns)
102 for param in function.params: 112 for param in function.params:
103 function_dict['parameters'].append(self._GenerateProperty(param)) 113 function_dict['parameters'].append(self._GenerateProperty(param))
104 if function_dict['callback']: 114 if function_dict['callback']:
105 function_dict['parameters'].append(function_dict['callback']) 115 function_dict['parameters'].append(function_dict['callback'])
106 if len(function_dict['parameters']) > 0: 116 if len(function_dict['parameters']) > 0:
107 function_dict['parameters'][-1]['last'] = True 117 function_dict['parameters'][-1]['last'] = True
108 return function_dict 118 return function_dict
109 119
110 def _GenerateEvent(self, event): 120 def _GenerateEvents(self, events, parent):
121 return [self._GenerateEvent(e, parent) for e in events.values()]
122
123 def _GenerateEvent(self, event, parent):
124 name = self._StripPrefix(event.name)
111 event_dict = { 125 event_dict = {
112 'name': self._StripPrefix(event.name), 126 'name': name,
113 'description': self._FormatDescription(event.description), 127 'description': self._FormatDescription(event.description),
114 'parameters': map(self._GenerateProperty, event.params), 128 'parameters': map(self._GenerateProperty, event.params),
115 'callback': self._GenerateCallback(event.callback), 129 'callback': self._GenerateCallback(event.callback),
116 'conditions': [GetLinkToRefType(self._namespace.name, c) 130 'conditions': [GetLinkToRefType(self._namespace.name, c)
117 for c in event.conditions], 131 for c in event.conditions],
118 'actions': [GetLinkToRefType(self._namespace.name, a) 132 'actions': [GetLinkToRefType(self._namespace.name, a)
119 for a in event.actions], 133 for a in event.actions],
120 'filters': map(self._GenerateProperty, event.filters), 134 'filters': map(self._GenerateProperty, event.filters),
121 'supportsRules': event.supports_rules 135 'supportsRules': event.supports_rules,
136 'parent': parent,
137 'id': _CreateId(name, parent, 'event')
122 } 138 }
123 if event_dict['callback']: 139 if event_dict['callback']:
124 event_dict['parameters'].append(event_dict['callback']) 140 event_dict['parameters'].append(event_dict['callback'])
125 if len(event_dict['parameters']) > 0: 141 if len(event_dict['parameters']) > 0:
126 event_dict['parameters'][-1]['last'] = True 142 event_dict['parameters'][-1]['last'] = True
127 return event_dict 143 return event_dict
128 144
129 def _GenerateCallback(self, callback): 145 def _GenerateCallback(self, callback):
130 if not callback: 146 if not callback:
131 return None 147 return None
132 callback_dict = { 148 callback_dict = {
133 'name': callback.name, 149 'name': callback.name,
134 'description': self._FormatDescription(callback.description), 150 'description': self._FormatDescription(callback.description),
135 'simple_type': {'simple_type': 'function'}, 151 'simple_type': {'simple_type': 'function'},
136 'optional': callback.optional, 152 'optional': callback.optional,
137 'parameters': [] 153 'parameters': []
138 } 154 }
139 for param in callback.params: 155 for param in callback.params:
140 callback_dict['parameters'].append(self._GenerateProperty(param)) 156 callback_dict['parameters'].append(self._GenerateProperty(param))
141 if (len(callback_dict['parameters']) > 0): 157 if (len(callback_dict['parameters']) > 0):
142 callback_dict['parameters'][-1]['last'] = True 158 callback_dict['parameters'][-1]['last'] = True
143 return callback_dict 159 return callback_dict
144 160
145 def _GenerateProperties(self, properties): 161 def _GenerateProperties(self, properties, parent):
146 return map(self._GenerateProperty, properties.values()) 162 return [self._GenerateProperty(v, parent) for v in properties.values()
163 if v.type_ != model.PropertyType.ADDITIONAL_PROPERTIES]
147 164
148 def _GenerateProperty(self, property_): 165 def _GenerateProperty(self, property_, parent=None):
166 name = self._StripPrefix(property_.name)
149 property_dict = { 167 property_dict = {
150 'name': self._StripPrefix(property_.name), 168 'name': name,
151 'optional': property_.optional, 169 'optional': property_.optional,
152 'description': self._FormatDescription(property_.description), 170 'description': self._FormatDescription(property_.description),
153 'properties': self._GenerateProperties(property_.properties), 171 'properties': self._GenerateProperties(property_.properties, name),
154 'functions': self._GenerateFunctions(property_.functions) 172 'functions': self._GenerateFunctions(property_.functions, name),
not at google - send to devlin 2012/10/03 01:07:26 It's confusing to me that "parent" is actually "na
cduvall 2012/10/05 01:13:26 Done. I named this 'parent_name' instead, and adde
173 'parent': parent,
174 'id': _CreateId(name, parent, 'property')
155 } 175 }
156 if property_.has_value: 176 if property_.has_value:
157 if isinstance(property_.value, int): 177 if isinstance(property_.value, int):
158 property_dict['value'] = _FormatValue(property_.value) 178 property_dict['value'] = _FormatValue(property_.value)
159 else: 179 else:
160 property_dict['value'] = property_.value 180 property_dict['value'] = property_.value
161 else: 181 else:
162 self._RenderTypeInformation(property_, property_dict) 182 self._RenderTypeInformation(property_, property_dict)
163 return property_dict 183 return property_dict
164 184
165 def _RenderTypeInformation(self, property_, dst_dict): 185 def _RenderTypeInformation(self, property_, dst_dict):
166 if property_.type_ == model.PropertyType.CHOICES: 186 if property_.type_ == model.PropertyType.CHOICES:
167 dst_dict['choices'] = map(self._GenerateProperty, 187 dst_dict['choices'] = map(self._GenerateProperty,
168 property_.choices.values()) 188 property_.choices.values())
169 # We keep track of which is last for knowing when to add "or" between 189 # We keep track of which is last for knowing when to add "or" between
170 # choices in templates. 190 # choices in templates.
171 if len(dst_dict['choices']) > 0: 191 if len(dst_dict['choices']) > 0:
172 dst_dict['choices'][-1]['last'] = True 192 dst_dict['choices'][-1]['last'] = True
173 elif property_.type_ == model.PropertyType.ADDITIONAL_PROPERTIES:
174 dst_dict['additional_properties'] = True
175 elif property_.type_ == model.PropertyType.REF: 193 elif property_.type_ == model.PropertyType.REF:
176 dst_dict['link'] = GetLinkToRefType(self._namespace.name, 194 dst_dict['link'] = GetLinkToRefType(self._namespace.name,
177 property_.ref_type) 195 property_.ref_type)
178 elif property_.type_ == model.PropertyType.ARRAY: 196 elif property_.type_ == model.PropertyType.ARRAY:
179 dst_dict['array'] = self._GenerateProperty(property_.item_type) 197 dst_dict['array'] = self._GenerateProperty(property_.item_type, None)
180 elif property_.type_ == model.PropertyType.ENUM: 198 elif property_.type_ == model.PropertyType.ENUM:
181 dst_dict['enum_values'] = [] 199 dst_dict['enum_values'] = []
182 for enum_value in property_.enum_values: 200 for enum_value in property_.enum_values:
183 dst_dict['enum_values'].append({'name': enum_value}) 201 dst_dict['enum_values'].append({'name': enum_value})
184 if len(dst_dict['enum_values']) > 0: 202 if len(dst_dict['enum_values']) > 0:
185 dst_dict['enum_values'][-1]['last'] = True 203 dst_dict['enum_values'][-1]['last'] = True
186 elif property_.instance_of is not None: 204 elif property_.instance_of is not None:
187 dst_dict['simple_type'] = property_.instance_of.lower() 205 dst_dict['simple_type'] = property_.instance_of.lower()
188 else: 206 else:
189 dst_dict['simple_type'] = property_.type_.name.lower() 207 dst_dict['simple_type'] = property_.type_.name.lower()
OLDNEW
« no previous file with comments | « no previous file | chrome/common/extensions/docs/server2/handlebar_dict_generator_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698