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

Side by Side Diff: chrome/common/extensions/docs/server2/reference_resolver.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: addressed comments 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
(Empty)
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
3 # found in the LICENSE file.
4
5 from file_system import FileNotFoundError
6
7 def _ClassifySchemaNode(node_name, api):
8 if '.' in node_name:
9 node_name, rest = node_name.split('.', 1)
10 else:
11 rest = None
12 for key, group in [('types', 'type'),
13 ('functions', 'method'),
14 ('events', 'event'),
15 ('properties', 'property')]:
16 for item in api.get(key, []):
17 if item['name'] == node_name:
18 if rest is not None:
19 ret = _ClassifySchemaNode(rest, item)
20 if ret is not None:
21 return ret
22 else:
23 return group
24 return None
25
26 class ReferenceResolver(object):
27 """Resolves references to $ref's by searching through the APIs to find the
28 correct node.
29 """
30 def __init__(self, api_data_source, api_list_data_source):
31 self._api_data_source = api_data_source
32 self._api_list_data_source = api_list_data_source
33
34 def GetLinkToRefType(self, namespace_name, ref_type):
not at google - send to devlin 2012/11/02 17:26:48 The "type" seems incorrect now that we're resolvin
cduvall 2012/11/03 01:30:05 Done.
35 if ref_type.startswith('%s.' % namespace_name):
36 ref_type = ref_type[len('%s.' % namespace_name):]
37 if '.' not in ref_type:
38 try:
39 category = _ClassifySchemaNode(
40 ref_type,
41 self._api_data_source[namespace_name])
not at google - send to devlin 2012/11/02 17:26:48 General comment: do we need all these __getitem__
cduvall 2012/11/03 01:30:05 Took out the __getitem__ stuff.
42 except FileNotFoundError:
43 return None
44 if category is None:
45 return None
46 return { 'href': '#%s-%s' % (category, ref_type), 'text': ref_type }
47 parts = ref_type.split('.')
48 api_list = self._api_list_data_source.GetAllNames()
49 for i, part in enumerate(parts):
50 if '.'.join(parts[:i]) not in api_list:
51 continue
52 try:
53 api_name = '.'.join(parts[:i])
54 api = self._api_data_source[api_name]
55 except FileNotFoundError:
56 continue
57 name = '.'.join(parts[i:])
58 category = _ClassifySchemaNode(name, api)
59 if category is None:
60 continue
61 return {
62 'href': '%s.html#%s-%s' % (api_name, category, name.replace('.', '-')),
63 'text': ref_type
64 }
65 return None
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698