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

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: cleanup 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 class Factory(object):
31 def __init__(self, api_data_source, api_list_data_source):
32 self._api_data_source = api_data_source
33 self._api_list_data_source = api_list_data_source
34
35 def Create(self):
36 return ReferenceResolver(self._api_data_source,
37 self._api_list_data_source)
38
39 def __init__(self, api_data_source, api_list_data_source):
40 self._api_data_source = api_data_source
41 self._api_list_data_source = api_list_data_source
42
43 def GetLink(self, namespace_name, ref):
44 # Try to resolve the ref in the current namespace first.
45 try:
46 api = self._api_data_source.get(namespace_name)
47 category = _ClassifySchemaNode(ref, api)
48 if category is not None:
49 return {
50 'href': '#%s-%s' % (category, ref.replace('.', '-')),
51 'text': ref
52 }
53 except FileNotFoundError:
54 pass
55 parts = ref.split('.')
56 api_list = self._api_list_data_source.GetAllNames()
57 for i, part in enumerate(parts):
58 if '.'.join(parts[:i]) not in api_list:
59 continue
60 try:
61 api_name = '.'.join(parts[:i])
62 api = self._api_data_source.get(api_name)
63 except FileNotFoundError:
64 continue
65 name = '.'.join(parts[i:])
66 category = _ClassifySchemaNode(name, api)
67 if category is None:
68 continue
69 text = ref
70 if text.startswith('%s.' % namespace_name):
71 text = text[len('%s.' % namespace_name):]
72 return {
73 'href': '%s.html#%s-%s' % (api_name, category, name.replace('.', '-')),
74 'text': text
75 }
76 return None
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698