summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/docs/server2/api_categorizer.py
blob: 5e1339e37eeba2182e5d6f441c78f763fa23e05e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
import posixpath

from compiled_file_system import SingleFile
from extensions_paths import PUBLIC_TEMPLATES


class APICategorizer(object):
  ''' This class gets api category from documented apis.
  '''

  def __init__(self, file_system, compiled_fs_factory):
    self._file_system = file_system
    self._cache = compiled_fs_factory.Create(file_system,
                                             self._CollectDocumentedAPIs,
                                             APICategorizer)

  def _GenerateAPICategories(self, platform):
    return self._cache.GetFromFileListing(
        posixpath.join(PUBLIC_TEMPLATES, platform) + '/').Get()

  @SingleFile
  def _CollectDocumentedAPIs(self, base_dir, files):
    public_templates = []
    for root, _, files in self._file_system.Walk(base_dir):
      public_templates.extend(posixpath.join(root, name) for name in files)
    template_names = set(os.path.splitext(name)[0].replace('_', '.')
                         for name in public_templates)
    return template_names

  def GetCategory(self, platform, api_name):
    '''Return the type of api.'Chrome' means the public apis,
    private means the api only used by chrome, and experimental means
    the apis with "experimental" prefix.
    '''
    documented_apis = self._GenerateAPICategories(platform)
    if (api_name.endswith('Private') or
        api_name not in documented_apis):
      return 'private'
    if api_name.startswith('experimental.'):
      return 'experimental'
    return 'chrome'

  def IsDocumented(self, platform, api_name):
    return (api_name in self._GenerateAPICategories(platform))