Chromium Code Reviews| OLD | NEW | 
|---|---|
| (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 if ref.startswith('%s.' % namespace_name): | |
| 45 ref = ref[len('%s.' % namespace_name):] | |
| 46 if '.' not in ref: | |
| 47 try: | |
| 48 category = _ClassifySchemaNode( | |
| 49 ref, | |
| 50 self._api_data_source.get(namespace_name)) | |
| 51 except FileNotFoundError: | |
| 52 return None | |
| 53 if category is None: | |
| 54 return None | |
| 55 return { 'href': '#%s-%s' % (category, ref), 'text': ref } | |
| 56 parts = ref.split('.') | |
| 
 
not at google - send to devlin
2012/11/05 19:47:50
It would be nice to write it in such a way that th
 
cduvall
2012/11/06 00:58:54
I ended up needing a special case for $refs not pr
 
 | |
| 57 api_list = self._api_list_data_source.GetAllNames() | |
| 58 for i, part in enumerate(parts): | |
| 59 if '.'.join(parts[:i]) not in api_list: | |
| 60 continue | |
| 61 try: | |
| 62 api_name = '.'.join(parts[:i]) | |
| 63 api = self._api_data_source.get(api_name) | |
| 64 except FileNotFoundError: | |
| 65 continue | |
| 66 name = '.'.join(parts[i:]) | |
| 67 category = _ClassifySchemaNode(name, api) | |
| 68 if category is None: | |
| 69 continue | |
| 70 return { | |
| 71 'href': '%s.html#%s-%s' % (api_name, category, name.replace('.', '-')), | |
| 72 'text': ref | |
| 73 } | |
| 74 return None | |
| OLD | NEW |