api_list_data_source.py revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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 5import logging 6import os 7 8import third_party.json_schema_compiler.model as model 9import docs_server_utils as utils 10 11class APIListDataSource(object): 12 """ This class creates a list of chrome.* APIs and chrome.experimental.* APIs 13 for extensions and apps that are used in the api_index.html and 14 experimental.html pages. 15 |api_path| is the path to the API schemas. 16 |public_path| is the path to the public HTML templates. 17 An API is considered listable if it's in both |api_path| and |public_path| - 18 the API schemas may contain undocumentable APIs, and the public HTML templates 19 will contain non-API articles. 20 """ 21 class Factory(object): 22 def __init__(self, compiled_fs_factory, api_path, public_path): 23 self._compiled_fs = compiled_fs_factory.Create(self._ListAPIs, 24 APIListDataSource) 25 self._identity_fs = compiled_fs_factory.CreateIdentity(APIListDataSource) 26 def Normalize(string): 27 return string if string.endswith('/') else (string + '/') 28 self._api_path = Normalize(api_path) 29 self._public_path = Normalize(public_path) 30 31 def _GetAPIsInSubdirectory(self, api_names, doc_type): 32 public_templates = self._identity_fs.GetFromFileListing( 33 '%s%s/' % (self._public_path, doc_type)) 34 template_names = set(os.path.splitext(name)[0] 35 for name in public_templates) 36 experimental_apis = [] 37 chrome_apis = [] 38 for template_name in sorted(template_names): 39 if model.UnixName(template_name) not in api_names: 40 continue 41 entry = {'name': template_name.replace('_', '.')} 42 if template_name.startswith('experimental'): 43 experimental_apis.append(entry) 44 else: 45 chrome_apis.append(entry) 46 if len(chrome_apis): 47 chrome_apis[-1]['last'] = True 48 if len(experimental_apis): 49 experimental_apis[-1]['last'] = True 50 return { 51 'chrome': chrome_apis, 52 'experimental': experimental_apis 53 } 54 55 def _ListAPIs(self, base_dir, apis): 56 api_names = set(utils.SanitizeAPIName(name) for name in apis) 57 return { 58 'apps': self._GetAPIsInSubdirectory(api_names, 'apps'), 59 'extensions': self._GetAPIsInSubdirectory(api_names, 'extensions') 60 } 61 62 def Create(self): 63 return APIListDataSource(self._compiled_fs, self._api_path) 64 65 def __init__(self, compiled_fs, api_path): 66 self._compiled_fs = compiled_fs 67 self._api_path = api_path 68 69 def GetAllNames(self): 70 names = [] 71 for platform in ['apps', 'extensions']: 72 for category in ['chrome', 'experimental']: 73 names.extend(self.get(platform).get(category)) 74 return [api_name['name'] for api_name in names] 75 76 def get(self, key): 77 return self._compiled_fs.GetFromFileListing(self._api_path)[key] 78