reference_resolver.py revision effb81e5f8246d0db0270817048dc992db66e9fb
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 5from copy import deepcopy 6import logging 7import re 8 9from file_system import FileNotFoundError 10 11 12def _ClassifySchemaNode(node_name, api): 13 """Attempt to classify |node_name| in an API, determining whether |node_name| 14 refers to a type, function, event, or property in |api|. 15 """ 16 if '.' in node_name: 17 node_name, rest = node_name.split('.', 1) 18 else: 19 rest = None 20 for key, group in [('types', 'type'), 21 ('functions', 'method'), 22 ('events', 'event'), 23 ('properties', 'property')]: 24 for item in api.get(key, []): 25 if item['name'] == node_name: 26 if rest is not None: 27 ret = _ClassifySchemaNode(rest, item) 28 if ret is not None: 29 return ret 30 else: 31 return group, node_name 32 return None 33 34 35def _MakeKey(namespace, ref): 36 key = '%s/%s' % (namespace, ref) 37 # AppEngine doesn't like keys > 500, but there will be some other stuff 38 # that goes into this key, so truncate it earlier. This shoudn't be 39 # happening anyway unless there's a bug, such as http://crbug.com/314102. 40 max_size = 256 41 if len(key) > max_size: 42 logging.error('Key was >%s characters: %s' % (max_size, key)) 43 key = key[:max_size] 44 return key 45 46 47class ReferenceResolver(object): 48 """Resolves references to $ref's by searching through the APIs to find the 49 correct node. 50 51 $ref's have two forms: 52 53 $ref:api.node - Replaces the $ref with a link to node on the API page. The 54 title is set to the name of the node. 55 56 $ref:[api.node The Title] - Same as the previous form but title is set to 57 "The Title". 58 """ 59 60 # Matches after a $ref: that doesn't have []s. 61 _bare_ref = re.compile('\w+(\.\w+)*') 62 63 def __init__(self, api_data_source, api_models, object_store): 64 self._api_data_source = api_data_source 65 self._api_models = api_models 66 self._object_store = object_store 67 68 def _GetRefLink(self, ref, api_list, namespace): 69 # Check nodes within each API the ref might refer to. 70 parts = ref.split('.') 71 for i, part in enumerate(parts): 72 api_name = '.'.join(parts[:i]) 73 if api_name not in api_list: 74 continue 75 try: 76 api = self._api_data_source.get(api_name, disable_refs=True) 77 except FileNotFoundError: 78 continue 79 name = '.'.join(parts[i:]) 80 # Attempt to find |name| in the API. 81 node_info = _ClassifySchemaNode(name, api) 82 if node_info is None: 83 # Check to see if this ref is a property. If it is, we want the ref to 84 # the underlying type the property is referencing. 85 for prop in api.get('properties', []): 86 # If the name of this property is in the ref text, replace the 87 # property with its type, and attempt to classify it. 88 if prop['name'] in name and 'link' in prop: 89 name_as_prop_type = name.replace(prop['name'], prop['link']['name']) 90 node_info = _ClassifySchemaNode(name_as_prop_type, api) 91 if node_info is not None: 92 name = name_as_prop_type 93 text = ref.replace(prop['name'], prop['link']['name']) 94 break 95 if node_info is None: 96 continue 97 else: 98 text = ref 99 category, node_name = node_info 100 if namespace is not None and text.startswith('%s.' % namespace): 101 text = text[len('%s.' % namespace):] 102 api_model = self._api_models.GetModel(api_name).Get() 103 filename = api_model.documentation_options.get('documented_in', api_name) 104 return { 105 'href': '%s#%s-%s' % (filename, category, name.replace('.', '-')), 106 'text': text, 107 'name': node_name 108 } 109 110 # If it's not a reference to an API node it might just be a reference to an 111 # API. Check this last so that links within APIs take precedence over links 112 # to other APIs. 113 if ref in api_list: 114 return { 115 'href': '%s' % ref, 116 'text': ref, 117 'name': ref 118 } 119 120 return None 121 122 def GetLink(self, ref, namespace=None, title=None): 123 """Resolve $ref |ref| in namespace |namespace| if not None, returning None 124 if it cannot be resolved. 125 """ 126 db_key = _MakeKey(namespace, ref) 127 link = self._object_store.Get(db_key).Get() 128 if link is None: 129 api_list = self._api_models.GetNames() 130 link = self._GetRefLink(ref, api_list, namespace) 131 if link is None and namespace is not None: 132 # Try to resolve the ref in the current namespace if there is one. 133 link = self._GetRefLink('%s.%s' % (namespace, ref), api_list, namespace) 134 if link is None: 135 return None 136 self._object_store.Set(db_key, link) 137 else: 138 link = deepcopy(link) 139 if title is not None: 140 link['text'] = title 141 return link 142 143 def SafeGetLink(self, ref, namespace=None, title=None): 144 """Resolve $ref |ref| in namespace |namespace|, or globally if None. If it 145 cannot be resolved, pretend like it is a link to a type. 146 """ 147 ref_data = self.GetLink(ref, namespace=namespace, title=title) 148 if ref_data is not None: 149 return ref_data 150 logging.error('$ref %s could not be resolved in namespace %s.' % 151 (ref, namespace)) 152 type_name = ref.rsplit('.', 1)[-1] 153 return { 154 'href': '#type-%s' % type_name, 155 'text': title or ref, 156 'name': ref 157 } 158 159 # TODO(ahernandez.miralles): This function is no longer needed, 160 # and uses a deprecated style of ref 161 def ResolveAllLinks(self, text, relative_to='', namespace=None): 162 """This method will resolve all $ref links in |text| using namespace 163 |namespace| if not None. Any links that cannot be resolved will be replaced 164 using the default link format that |SafeGetLink| uses. 165 The links will be generated relative to |relative_to|. 166 """ 167 if text is None or '$ref:' not in text: 168 return text 169 170 # requestPath should be of the form (apps|extensions)/...../page.html. 171 # link_prefix should that the target will point to 172 # (apps|extensions)/target.html. Note multiplying a string by a negative 173 # number gives the empty string. 174 link_prefix = '../' * (relative_to.count('/') - 1) 175 split_text = text.split('$ref:') 176 # |split_text| is an array of text chunks that all start with the 177 # argument to '$ref:'. 178 formatted_text = [split_text[0]] 179 for ref_and_rest in split_text[1:]: 180 title = None 181 if ref_and_rest.startswith('[') and ']' in ref_and_rest: 182 # Text was '$ref:[foo.bar maybe title] other stuff'. 183 ref_with_title, rest = ref_and_rest[1:].split(']', 1) 184 ref_with_title = ref_with_title.split(None, 1) 185 if len(ref_with_title) == 1: 186 # Text was '$ref:[foo.bar] other stuff'. 187 ref = ref_with_title[0] 188 else: 189 # Text was '$ref:[foo.bar title] other stuff'. 190 ref, title = ref_with_title 191 else: 192 # Text was '$ref:foo.bar other stuff'. 193 match = self._bare_ref.match(ref_and_rest) 194 if match is None: 195 ref = '' 196 rest = ref_and_rest 197 else: 198 ref = match.group() 199 rest = ref_and_rest[match.end():] 200 201 ref_dict = self.SafeGetLink(ref, namespace=namespace, title=title) 202 formatted_text.append('<a href="%s%s">%s</a>%s' % 203 (link_prefix, ref_dict['href'], ref_dict['text'], rest)) 204 return ''.join(formatted_text) 205