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

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

Issue 10830252: Extensions Docs Server: Uniform handling of file not found errors (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 4 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
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 logging
7 import os 6 import os
8 7
9 from docs_server_utils import GetLinkToRefType 8 from docs_server_utils import GetLinkToRefType
10 import third_party.json_schema_compiler.model as model 9 import third_party.json_schema_compiler.model as model
11 10
12 def _RemoveNoDocs(item): 11 def _RemoveNoDocs(item):
13 if type(item) == dict: 12 if type(item) == dict:
14 if item.get('nodoc', False): 13 if item.get('nodoc', False):
15 return True 14 return True
16 for key, value in item.items(): 15 for key, value in item.items():
(...skipping 11 matching lines...) Expand all
28 s = str(value) 27 s = str(value)
29 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) 28 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1])
30 29
31 class HandlebarDictGenerator(object): 30 class HandlebarDictGenerator(object):
32 """Uses a Model from the JSON Schema Compiler and generates a dict that 31 """Uses a Model from the JSON Schema Compiler and generates a dict that
33 a Handlebar template can use for a data source. 32 a Handlebar template can use for a data source.
34 """ 33 """
35 def __init__(self, json): 34 def __init__(self, json):
36 clean_json = copy.deepcopy(json) 35 clean_json = copy.deepcopy(json)
37 _RemoveNoDocs(clean_json) 36 _RemoveNoDocs(clean_json)
38 try: 37 self._namespace = model.Namespace(clean_json, clean_json['namespace'])
39 self._namespace = model.Namespace(clean_json, clean_json['namespace'])
40 except Exception as e:
41 logging.error(e)
42 raise
43 38
44 def _StripPrefix(self, name): 39 def _StripPrefix(self, name):
45 if name.startswith(self._namespace.name + '.'): 40 if name.startswith(self._namespace.name + '.'):
46 return name[len(self._namespace.name + '.'):] 41 return name[len(self._namespace.name + '.'):]
47 return name 42 return name
48 43
49 def _FormatDescription(self, description): 44 def _FormatDescription(self, description):
50 if description is None or '$ref:' not in description: 45 if description is None or '$ref:' not in description:
51 return description 46 return description
52 refs = description.split('$ref:') 47 refs = description.split('$ref:')
53 formatted_description = [refs[0]] 48 formatted_description = [refs[0]]
54 for ref in refs[1:]: 49 for ref in refs[1:]:
55 parts = ref.split(' ', 1) 50 parts = ref.split(' ', 1)
56 if len(parts) == 1: 51 if len(parts) == 1:
57 ref = parts[0] 52 ref = parts[0]
58 rest = '' 53 rest = ''
59 else: 54 else:
60 ref, rest = parts 55 ref, rest = parts
61 rest = ' ' + rest 56 rest = ' ' + rest
62 if not ref[-1].isalnum(): 57 if not ref[-1].isalnum():
63 rest = ref[-1] + rest 58 rest = ref[-1] + rest
64 ref = ref[:-1] 59 ref = ref[:-1]
65 ref_dict = GetLinkToRefType(self._namespace.name, ref) 60 ref_dict = GetLinkToRefType(self._namespace.name, ref)
66 formatted_description.append('<a href="%(href)s">%(text)s</a>%(rest)s' % 61 formatted_description.append('<a href="%(href)s">%(text)s</a>%(rest)s' %
67 { 'href': ref_dict['href'], 'text': ref_dict['text'], 'rest': rest }) 62 { 'href': ref_dict['href'], 'text': ref_dict['text'], 'rest': rest })
68 return ''.join(formatted_description) 63 return ''.join(formatted_description)
69 64
70 def Generate(self, samples): 65 def Generate(self, samples):
71 try: 66 return {
72 return { 67 'name': self._namespace.name,
73 'name': self._namespace.name, 68 'types': map(self._GenerateType, self._namespace.types.values()),
74 'types': map(self._GenerateType, self._namespace.types.values()), 69 'functions': self._GenerateFunctions(self._namespace.functions),
75 'functions': self._GenerateFunctions(self._namespace.functions), 70 'events': map(self._GenerateEvent, self._namespace.events.values()),
76 'events': map(self._GenerateEvent, self._namespace.events.values()), 71 'properties': self._GenerateProperties(self._namespace.properties),
77 'properties': self._GenerateProperties(self._namespace.properties), 72 'samples': samples,
78 'samples': samples, 73 }
79 }
80 except Exception as e:
81 logging.error(e)
82 raise
83 74
84 def _GenerateType(self, type_): 75 def _GenerateType(self, type_):
85 type_dict = { 76 type_dict = {
86 'name': self._StripPrefix(type_.name), 77 'name': self._StripPrefix(type_.name),
87 'description': self._FormatDescription(type_.description), 78 'description': self._FormatDescription(type_.description),
88 'properties': self._GenerateProperties(type_.properties), 79 'properties': self._GenerateProperties(type_.properties),
89 'functions': self._GenerateFunctions(type_.functions), 80 'functions': self._GenerateFunctions(type_.functions),
90 'events': map(self._GenerateEvent, type_.events.values()) 81 'events': map(self._GenerateEvent, type_.events.values())
91 } 82 }
92 self._RenderTypeInformation(type_, type_dict) 83 self._RenderTypeInformation(type_, type_dict)
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 elif property_.type_ == model.PropertyType.ENUM: 168 elif property_.type_ == model.PropertyType.ENUM:
178 dst_dict['enum_values'] = [] 169 dst_dict['enum_values'] = []
179 for enum_value in property_.enum_values: 170 for enum_value in property_.enum_values:
180 dst_dict['enum_values'].append({'name': enum_value}) 171 dst_dict['enum_values'].append({'name': enum_value})
181 if len(dst_dict['enum_values']) > 0: 172 if len(dst_dict['enum_values']) > 0:
182 dst_dict['enum_values'][-1]['last'] = True 173 dst_dict['enum_values'][-1]['last'] = True
183 elif property_.instance_of: 174 elif property_.instance_of:
184 dst_dict['simple_type'] = property_.instance_of.lower() 175 dst_dict['simple_type'] = property_.instance_of.lower()
185 else: 176 else:
186 dst_dict['simple_type'] = property_.type_.name.lower() 177 dst_dict['simple_type'] = property_.type_.name.lower()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698