OLD | NEW |
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 from file_system import FileNotFoundError | 5 from file_system import FileNotFoundError |
| 6 from object_store_creator import ObjectStoreCreator |
6 import logging | 7 import logging |
7 import object_store | |
8 import re | 8 import re |
9 import string | 9 import string |
10 | 10 |
11 def _ClassifySchemaNode(node_name, api): | 11 def _ClassifySchemaNode(node_name, api): |
12 """Attempt to classify |node_name| in an API, determining whether |node_name| | 12 """Attempt to classify |node_name| in an API, determining whether |node_name| |
13 refers to a type, function, event, or property in |api|. | 13 refers to a type, function, event, or property in |api|. |
14 """ | 14 """ |
15 if '.' in node_name: | 15 if '.' in node_name: |
16 node_name, rest = node_name.split('.', 1) | 16 node_name, rest = node_name.split('.', 1) |
17 else: | 17 else: |
(...skipping 25 matching lines...) Expand all Loading... |
43 title is set to the name of the node. | 43 title is set to the name of the node. |
44 | 44 |
45 $ref:[api.node The Title] - Same as the previous form but title is set to | 45 $ref:[api.node The Title] - Same as the previous form but title is set to |
46 "The Title". | 46 "The Title". |
47 """ | 47 """ |
48 | 48 |
49 # Matches after a $ref: that doesn't have []s. | 49 # Matches after a $ref: that doesn't have []s. |
50 _bare_ref = re.compile('\w+(\.\w+)*') | 50 _bare_ref = re.compile('\w+(\.\w+)*') |
51 | 51 |
52 class Factory(object): | 52 class Factory(object): |
53 def __init__(self, | 53 def __init__(self, api_data_source_factory, api_list_data_source_factory): |
54 api_data_source_factory, | |
55 api_list_data_source_factory, | |
56 object_store): | |
57 self._api_data_source_factory = api_data_source_factory | 54 self._api_data_source_factory = api_data_source_factory |
58 self._api_list_data_source_factory = api_list_data_source_factory | 55 self._api_list_data_source_factory = api_list_data_source_factory |
59 self._object_store = object_store | |
60 | 56 |
61 def Create(self): | 57 def Create(self): |
62 return ReferenceResolver( | 58 return ReferenceResolver( |
63 self._api_data_source_factory.Create(None, disable_refs=True), | 59 self._api_data_source_factory.Create(None, disable_refs=True), |
64 self._api_list_data_source_factory.Create(), | 60 self._api_list_data_source_factory.Create(), |
65 self._object_store) | 61 ObjectStoreCreator(ReferenceResolver).Create()) |
66 | 62 |
67 def __init__(self, api_data_source, api_list_data_source, object_store): | 63 def __init__(self, api_data_source, api_list_data_source, object_store): |
68 self._api_data_source = api_data_source | 64 self._api_data_source = api_data_source |
69 self._api_list_data_source = api_list_data_source | 65 self._api_list_data_source = api_list_data_source |
70 self._object_store = object_store | 66 self._object_store = object_store |
71 | 67 |
72 def _GetRefLink(self, ref, api_list, namespace, title): | 68 def _GetRefLink(self, ref, api_list, namespace, title): |
73 # Check nodes within each API the ref might refer to. | 69 # Check nodes within each API the ref might refer to. |
74 parts = ref.split('.') | 70 parts = ref.split('.') |
75 for i, part in enumerate(parts): | 71 for i, part in enumerate(parts): |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 'text': title if title else ref, | 114 'text': title if title else ref, |
119 'name': ref | 115 'name': ref |
120 } | 116 } |
121 | 117 |
122 return None | 118 return None |
123 | 119 |
124 def GetLink(self, ref, namespace=None, title=None): | 120 def GetLink(self, ref, namespace=None, title=None): |
125 """Resolve $ref |ref| in namespace |namespace| if not None, returning None | 121 """Resolve $ref |ref| in namespace |namespace| if not None, returning None |
126 if it cannot be resolved. | 122 if it cannot be resolved. |
127 """ | 123 """ |
128 link = self._object_store.Get(_MakeKey(namespace, ref, title), | 124 link = self._object_store.Get(_MakeKey(namespace, ref, title)).Get() |
129 object_store.REFERENCE_RESOLVER).Get() | |
130 if link is not None: | 125 if link is not None: |
131 return link | 126 return link |
132 | 127 |
133 api_list = self._api_list_data_source.GetAllNames() | 128 api_list = self._api_list_data_source.GetAllNames() |
134 link = self._GetRefLink(ref, api_list, namespace, title) | 129 link = self._GetRefLink(ref, api_list, namespace, title) |
135 | 130 |
136 if link is None and namespace is not None: | 131 if link is None and namespace is not None: |
137 # Try to resolve the ref in the current namespace if there is one. | 132 # Try to resolve the ref in the current namespace if there is one. |
138 link = self._GetRefLink('%s.%s' % (namespace, ref), | 133 link = self._GetRefLink('%s.%s' % (namespace, ref), |
139 api_list, | 134 api_list, |
140 namespace, | 135 namespace, |
141 title) | 136 title) |
142 | 137 |
143 if link is not None: | 138 if link is not None: |
144 self._object_store.Set(_MakeKey(namespace, ref, title), | 139 self._object_store.Set(_MakeKey(namespace, ref, title), link) |
145 link, | |
146 object_store.REFERENCE_RESOLVER) | |
147 return link | 140 return link |
148 | 141 |
149 def SafeGetLink(self, ref, namespace=None, title=None): | 142 def SafeGetLink(self, ref, namespace=None, title=None): |
150 """Resolve $ref |ref| in namespace |namespace|, or globally if None. If it | 143 """Resolve $ref |ref| in namespace |namespace|, or globally if None. If it |
151 cannot be resolved, pretend like it is a link to a type. | 144 cannot be resolved, pretend like it is a link to a type. |
152 """ | 145 """ |
153 ref_data = self.GetLink(ref, namespace=namespace, title=title) | 146 ref_data = self.GetLink(ref, namespace=namespace, title=title) |
154 if ref_data is not None: | 147 if ref_data is not None: |
155 return ref_data | 148 return ref_data |
156 logging.error('$ref %s could not be resolved in namespace %s.' % | 149 logging.error('$ref %s could not be resolved in namespace %s.' % |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 ref = '' | 185 ref = '' |
193 rest = ref_and_rest | 186 rest = ref_and_rest |
194 else: | 187 else: |
195 ref = match.group() | 188 ref = match.group() |
196 rest = ref_and_rest[match.end():] | 189 rest = ref_and_rest[match.end():] |
197 | 190 |
198 ref_dict = self.SafeGetLink(ref, namespace=namespace, title=title) | 191 ref_dict = self.SafeGetLink(ref, namespace=namespace, title=title) |
199 formatted_text.append('<a href="%(href)s">%(text)s</a>%(rest)s' % | 192 formatted_text.append('<a href="%(href)s">%(text)s</a>%(rest)s' % |
200 { 'href': ref_dict['href'], 'text': ref_dict['text'], 'rest': rest }) | 193 { 'href': ref_dict['href'], 'text': ref_dict['text'], 'rest': rest }) |
201 return ''.join(formatted_text) | 194 return ''.join(formatted_text) |
OLD | NEW |