| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import logging | 5 import logging |
| 6 import os | 6 import os |
| 7 | 7 |
| 8 from file_system import FileNotFoundError | 8 from file_system import FileNotFoundError |
| 9 import compiled_file_system as compiled_fs | |
| 10 import third_party.json_schema_compiler.model as model | 9 import third_party.json_schema_compiler.model as model |
| 11 from docs_server_utils import SanitizeAPIName | 10 import docs_server_utils as utils |
| 11 |
| 12 # Increment this if the data model changes for APIDataSource. |
| 13 _VERSION = 1 |
| 12 | 14 |
| 13 # These files are special cases that shouldn't be in the API list. | 15 # These files are special cases that shouldn't be in the API list. |
| 14 IGNORED_FILES = [ | 16 IGNORED_FILES = [ |
| 15 'devtools' | 17 'devtools' |
| 16 ] | 18 ] |
| 17 | 19 |
| 18 class APIListDataSource(object): | 20 class APIListDataSource(object): |
| 19 """ This class creates a list of chrome.* APIs and chrome.experimental.* APIs | 21 """ 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. | 22 for extensions and apps that are used in the api_index.html and |
| 23 experimental.html pages. |
| 24 |api_path| is the path to the API schemas. |
| 25 |public_path| is the path to the public HTML templates. |
| 26 An API is considered listable if it's in both |api_path| and |public_path| - |
| 27 the API schemas may contain undocumentable APIs, and the public HTML templates |
| 28 will contain non-API articles. |
| 21 """ | 29 """ |
| 22 class Factory(object): | 30 class Factory(object): |
| 23 def __init__(self, cache_factory, file_system, api_path, public_path): | 31 def __init__(self, compiled_fs_factory, api_path, public_path): |
| 24 self._cache = cache_factory.Create(self._ListAPIs, compiled_fs.LIST) | 32 self._compiled_fs = compiled_fs_factory.Create( |
| 25 self._file_system = file_system | 33 self._ListAPIs, APIListDataSource, version=_VERSION) |
| 34 self._identity_fs = compiled_fs_factory.GetOrCreateIdentity() |
| 26 def Normalize(string): | 35 def Normalize(string): |
| 27 return string if string.endswith('/') else (string + '/') | 36 return string if string.endswith('/') else (string + '/') |
| 28 self._api_path = Normalize(api_path) | 37 self._api_path = Normalize(api_path) |
| 29 self._public_path = Normalize(public_path) | 38 self._public_path = Normalize(public_path) |
| 30 | 39 |
| 31 def _GetAPIsInSubdirectory(self, api_names, doc_type): | 40 def _GetAPIsInSubdirectory(self, api_names, doc_type): |
| 32 public_templates = self._file_system.ReadSingle( | 41 public_templates = self._identity_fs.GetFromFileListing( |
| 33 self._public_path + doc_type + '/') | 42 '%s%s/' % (self._public_path, doc_type)) |
| 34 template_names = [os.path.splitext(name)[0] | 43 template_names = set(os.path.splitext(name)[0] |
| 35 for name in public_templates] | 44 for name in public_templates) |
| 36 experimental_apis = [] | 45 experimental_apis = [] |
| 37 chrome_apis = [] | 46 chrome_apis = [] |
| 38 for template_name in sorted(template_names): | 47 for template_name in sorted(template_names): |
| 39 if template_name in IGNORED_FILES: | 48 if template_name in IGNORED_FILES: |
| 40 continue | 49 continue |
| 41 if model.UnixName(template_name) in api_names: | 50 if model.UnixName(template_name) in api_names: |
| 42 if template_name.startswith('experimental'): | 51 if template_name.startswith('experimental'): |
| 43 experimental_apis.append({ | 52 experimental_apis.append({ |
| 44 'name': template_name.replace('_', '.') | 53 'name': template_name.replace('_', '.') |
| 45 }) | 54 }) |
| 46 else: | 55 else: |
| 47 chrome_apis.append({ 'name': template_name.replace('_', '.') }) | 56 chrome_apis.append({ 'name': template_name.replace('_', '.') }) |
| 48 if len(chrome_apis): | 57 if len(chrome_apis): |
| 49 chrome_apis[-1]['last'] = True | 58 chrome_apis[-1]['last'] = True |
| 50 if len(experimental_apis): | 59 if len(experimental_apis): |
| 51 experimental_apis[-1]['last'] = True | 60 experimental_apis[-1]['last'] = True |
| 52 return { | 61 return { |
| 53 'chrome': chrome_apis, | 62 'chrome': chrome_apis, |
| 54 'experimental': experimental_apis | 63 'experimental': experimental_apis |
| 55 } | 64 } |
| 56 | 65 |
| 57 def _ListAPIs(self, base_dir, apis): | 66 def _ListAPIs(self, base_dir, apis): |
| 58 api_names = set(SanitizeAPIName(name, self._api_path) for name in apis) | 67 api_names = set(utils.SanitizeAPIName(name) for name in apis) |
| 59 return { | 68 return { |
| 60 'apps': self._GetAPIsInSubdirectory(api_names, 'apps'), | 69 'apps': self._GetAPIsInSubdirectory(api_names, 'apps'), |
| 61 'extensions': self._GetAPIsInSubdirectory(api_names, 'extensions') | 70 'extensions': self._GetAPIsInSubdirectory(api_names, 'extensions') |
| 62 } | 71 } |
| 63 | 72 |
| 64 def Create(self): | 73 def Create(self): |
| 65 return APIListDataSource(self._cache, self._api_path) | 74 return APIListDataSource(self._compiled_fs, self._api_path) |
| 66 | 75 |
| 67 def __init__(self, cache, api_path): | 76 def __init__(self, compiled_fs, api_path): |
| 68 self._cache = cache | 77 self._compiled_fs = compiled_fs |
| 69 self._api_path = api_path | 78 self._api_path = api_path |
| 70 | 79 |
| 71 def GetAllNames(self): | 80 def GetAllNames(self): |
| 72 names = [] | 81 names = [] |
| 73 for i in ['apps', 'extensions']: | 82 for platform in ['apps', 'extensions']: |
| 74 for j in ['chrome', 'experimental']: | 83 for category in ['chrome', 'experimental']: |
| 75 names.extend(self.get(i).get(j)) | 84 names.extend(self.get(platform).get(category)) |
| 76 return [api_name['name'] for api_name in names] | 85 return [api_name['name'] for api_name in names] |
| 77 | 86 |
| 78 def get(self, key): | 87 def get(self, key): |
| 79 try: | 88 try: |
| 80 return self._cache.GetFromFileListing(self._api_path)[key] | 89 return self._compiled_fs.GetFromFileListing(self._api_path)[key] |
| 81 except FileNotFoundError as e: | 90 except FileNotFoundError as e: |
| 82 raise ValueError('%s: Error listing files for "%s".' % (e, key)) | 91 raise ValueError('%s: Error listing files for "%s".' % (e, key)) |
| OLD | NEW |