api_list_data_source.py revision 5821806d5e7f356e8fa4b058a389a808ea183019
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
8from file_system import FileNotFoundError
9import compiled_file_system as compiled_fs
10import third_party.json_schema_compiler.model as model
11from docs_server_utils import SanitizeAPIName
12
13# These files are special cases that shouldn't be in the API list.
14IGNORED_FILES = [
15  'devtools'
16]
17
18class APIListDataSource(object):
19  """ This class creates a list of chrome.* APIs and chrome.experimental.* APIs
20  that are used in the api_index.html and experimental.html pages.
21  """
22  class Factory(object):
23    def __init__(self, cache_factory, file_system, api_path, public_path):
24      self._cache = cache_factory.Create(self._ListAPIs, compiled_fs.LIST)
25      self._file_system = file_system
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._file_system.ReadSingle(
33          self._public_path + doc_type + '/')
34      template_names = [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 template_name in IGNORED_FILES:
40          continue
41        if model.UnixName(template_name) in api_names:
42          if template_name.startswith('experimental'):
43            experimental_apis.append({
44              'name': template_name.replace('_', '.')
45            })
46          else:
47            chrome_apis.append({ 'name': template_name.replace('_', '.') })
48      if len(chrome_apis):
49        chrome_apis[-1]['last'] = True
50      if len(experimental_apis):
51        experimental_apis[-1]['last'] = True
52      return {
53        'chrome': chrome_apis,
54        'experimental': experimental_apis
55      }
56
57    def _ListAPIs(self, apis):
58      api_names = set(SanitizeAPIName(name, self._api_path) for name in apis)
59      return {
60        'apps': self._GetAPIsInSubdirectory(api_names, 'apps'),
61        'extensions': self._GetAPIsInSubdirectory(api_names, 'extensions')
62      }
63
64    def Create(self):
65      return APIListDataSource(self._cache, self._api_path)
66
67  def __init__(self, cache, api_path):
68    self._cache = cache
69    self._api_path = api_path
70
71  def GetAllNames(self):
72    names = []
73    for i in ['apps', 'extensions']:
74      for j in ['chrome', 'experimental']:
75       names.extend(self.get(i).get(j))
76    return [api_name['name'] for api_name in names]
77
78  def get(self, key):
79    try:
80      return self._cache.GetFromFileListing(self._api_path)[key]
81    except FileNotFoundError as e:
82      raise ValueError('%s: Error listing files for "%s".' % (e, key))
83