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