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

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

Issue 11315018: Extensions Docs Server: Generalize $ref's to work for any schema node (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 1 month 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 os 5 import os
6 6
7 from file_system import FileNotFoundError
8
7 def FormatKey(key): 9 def FormatKey(key):
8 """Normalize a key by making sure it has a .html extension, and convert any 10 """Normalize a key by making sure it has a .html extension, and convert any
9 '.'s to '_'s. 11 '.'s to '_'s.
10 """ 12 """
11 if key.endswith('.html'): 13 if key.endswith('.html'):
12 key = key[:-5] 14 key = key[:-len('.html')]
13 safe_key = key.replace('.', '_') 15 safe_key = key.replace('.', '_')
14 return safe_key + '.html' 16 return '%s.html' % safe_key
15 17
16 def SanitizeAPIName(name, api_path=''): 18 def SanitizeAPIName(name, api_path=''):
17 """Sanitizes API filenames that are in subdirectories. 19 """Sanitizes API filenames that are in subdirectories.
18 """ 20 """
19 filename = os.path.splitext(name)[0][len(api_path):].replace(os.sep, '_') 21 filename = os.path.splitext(name)[0][len(api_path):].replace(os.sep, '_')
20 if 'experimental' in filename: 22 if 'experimental' in filename:
21 filename = 'experimental_' + filename.replace('experimental_', '') 23 filename = 'experimental_' + filename.replace('experimental_', '')
22 return filename 24 return filename
23 25
24 def GetLinkToRefType(namespace_name, ref_type): 26 def _FindInAPI(node_name, api):
not at google - send to devlin 2012/11/01 22:00:49 Find what in API? And what does it return? I was s
cduvall 2012/11/02 01:53:53 Done.
27 node_name = node_name.split('.')[0]
28 for type_ in api.get('types', []):
29 if type_['name'] == node_name:
not at google - send to devlin 2012/11/01 22:00:49 except the property is "id".
cduvall 2012/11/02 01:53:53 This has been processed by JSCModel, so all nodes
not at google - send to devlin 2012/11/02 17:26:47 Ah, of course.
30 return 'type'
31 for function in api.get('functions', []):
32 if function['name'] == node_name:
33 return 'method'
34 for event in api.get('events', []):
35 if event['name'] == node_name:
not at google - send to devlin 2012/11/01 22:00:49 how about for name in ['functions', 'events']:
cduvall 2012/11/02 01:53:53 Done.
36 return 'event'
37 for property_ in api.get('properties', []):
38 if property_['name'] == node_name:
39 return 'property'
not at google - send to devlin 2012/11/01 22:00:49 ... except "properties" is a dictionary not a list
cduvall 2012/11/02 01:53:53 This has been processed by JSCModel, so it's a lis
40 return None
not at google - send to devlin 2012/11/01 22:00:49 And also, we need to search deeply through nodes.
not at google - send to devlin 2012/11/01 22:05:15 This part of the comment was made before I read pa
cduvall 2012/11/02 01:53:53 I made this more robust so it checks to make sure
41
42 def GetLinkToRefType(namespace_name, ref_type, api_data_source, api_list):
not at google - send to devlin 2012/11/01 22:00:49 You're gonna hate me here but now that there's inj
cduvall 2012/11/02 01:53:53 Done.
25 """Returns a link given a $ref. 43 """Returns a link given a $ref.
26 """ 44 """
27 if ref_type.startswith(namespace_name + '.'): 45 if ref_type.startswith('%s.' % namespace_name):
28 type_name = ref_type[len(namespace_name + '.'):] 46 name = ref_type[len('%s.' % namespace_name):]
29 return { 'href': '#type-' + type_name, 'text': type_name } 47 try:
48 type_name = _FindInAPI(name, api_data_source[namespace_name])
not at google - send to devlin 2012/11/01 22:00:49 as I mentioned above, "type" is overloaded in this
cduvall 2012/11/02 01:53:53 Done.
49 except FileNotFoundError:
not at google - send to devlin 2012/11/01 22:05:15 But speaking of that, this seems like a bug where
cduvall 2012/11/02 01:53:53 I tried this with refs to app.window.create and li
not at google - send to devlin 2012/11/02 17:26:47 I meant like, if there's a reference to 'app.windo
50 return None
51 if type_name is None:
52 return None
53 return { 'href': '#%s-%s' % (type_name, name), 'text': name }
30 elif '.' not in ref_type: 54 elif '.' not in ref_type:
31 return { 'href': '#type-' + ref_type, 'text': ref_type } 55 try:
32 api, type_name = ref_type.rsplit('.', 1) 56 type_name = _FindInAPI(ref_type, api_data_source[namespace_name])
33 return { 'href': api + '.html#type-' + type_name, 'text': ref_type } 57 except FileNotFoundError:
58 return None
59 if type_name is None:
60 return None
61 return { 'href': '#%s-%s' % (type_name, ref_type), 'text': ref_type }
not at google - send to devlin 2012/11/01 22:00:49 It seems like the above two conditions could be co
cduvall 2012/11/02 01:53:53 Done.
62 parts = ref_type.split('.')
63 for i, part in enumerate(parts):
64 if '.'.join(parts[:i]) not in api_list:
65 continue
66 try:
67 api = api_data_source['.'.join(parts[:i])]
not at google - send to devlin 2012/11/01 22:00:49 '.'.join(parts[:i]) is repeated, assign to variabl
cduvall 2012/11/02 01:53:53 Done.
68 except FileNotFoundError:
69 continue
70 name = '.'.join(parts[i:])
71 type_name = _FindInAPI(name, api)
not at google - send to devlin 2012/11/01 22:00:49 again, s/type_name/?/
cduvall 2012/11/02 01:53:53 Done.
72 if type_name is None:
73 continue
74 return {
75 'href': '%s.html#%s-%s' % ('.'.join(parts[:i]), type_name, name),
not at google - send to devlin 2012/11/01 22:00:49 re-use api_name here.
cduvall 2012/11/02 01:53:53 Done.
76 'text': ref_type
77 }
78 return None
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698