diff options
author | cduvall@chromium.org <cduvall@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-10 17:47:50 +0000 |
---|---|---|
committer | cduvall@chromium.org <cduvall@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-10 17:47:50 +0000 |
commit | 16efc1793838850833e68d0d6b4e453ab7be34ef (patch) | |
tree | d40ec4791701691344c0d0b7a27c2867dc9f7c3e | |
parent | e7ea0eb4f5b5d6368b09b63d7d5ee5f93a7fa8a0 (diff) | |
download | chromium_src-16efc1793838850833e68d0d6b4e453ab7be34ef.zip chromium_src-16efc1793838850833e68d0d6b4e453ab7be34ef.tar.gz chromium_src-16efc1793838850833e68d0d6b4e453ab7be34ef.tar.bz2 |
Extensions Docs Server: Uniform handling of file not found errors
All the FileSystem implementations now handle missing files the same way (by
throwing a FileNotFoundError).
BUG=141664
Review URL: https://chromiumcodereview.appspot.com/10830252
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@151078 0039d316-1c4b-4281-b951-d872f2087c98
20 files changed, 73 insertions, 38 deletions
diff --git a/chrome/common/extensions/docs/server2/api_data_source.py b/chrome/common/extensions/docs/server2/api_data_source.py index c6578c8..fe5a1c2 100644 --- a/chrome/common/extensions/docs/server2/api_data_source.py +++ b/chrome/common/extensions/docs/server2/api_data_source.py @@ -6,6 +6,7 @@ import json import logging import os +from file_system import FileNotFoundError from handlebar_dict_generator import HandlebarDictGenerator import third_party.json_schema_compiler.json_comment_eater as json_comment_eater import third_party.json_schema_compiler.model as model @@ -60,12 +61,14 @@ class APIDataSource(object): try: perms = self._permissions_cache.GetFromFile( self._base_path + '/_permission_features.json') - api_perms = perms.get(path, None) - if api_perms['channel'] == 'dev': - api_perms['dev'] = True - return api_perms - except Exception: + except FileNotFoundError: return None + api_perms = perms.get(path, None) + if api_perms is None: + return None + if api_perms['channel'] == 'dev': + api_perms['dev'] = True + return api_perms def _GenerateHandlebarContext(self, api_name, handlebar, path): return_dict = { 'permissions': self._GetFeature(path) } @@ -90,10 +93,11 @@ class APIDataSource(object): return self._GenerateHandlebarContext(key, self._json_cache.GetFromFile(self._base_path + '/' + json_path), path) - except OSError: + except FileNotFoundError: try: return self._GenerateHandlebarContext(key, self._idl_cache.GetFromFile(self._base_path + '/' + idl_path), path) - except OSError: + except FileNotFoundError as e: + logging.error(e) raise diff --git a/chrome/common/extensions/docs/server2/api_data_source_test.py b/chrome/common/extensions/docs/server2/api_data_source_test.py index 253b084d..160a42e 100755 --- a/chrome/common/extensions/docs/server2/api_data_source_test.py +++ b/chrome/common/extensions/docs/server2/api_data_source_test.py @@ -7,6 +7,7 @@ import json import os import unittest +from file_system import FileNotFoundError from file_system_cache import FileSystemCache from local_file_system import LocalFileSystem from api_data_source import APIDataSource @@ -26,7 +27,7 @@ class APIDataSourceTest(unittest.TestCase): def testSimple(self): cache_builder = FileSystemCache.Builder(LocalFileSystem(self._base_path)) data_source_factory = APIDataSource.Factory(cache_builder, - './', + '.', FakeSamplesDataSource()) data_source = data_source_factory.Create({}) @@ -36,7 +37,7 @@ class APIDataSourceTest(unittest.TestCase): self.assertEqual(expected, data_source['test_file']) self.assertEqual(expected, data_source['testFile']) self.assertEqual(expected, data_source['testFile.html']) - self.assertRaises(OSError, data_source.get, 'junk') + self.assertRaises(FileNotFoundError, data_source.get, 'junk') if __name__ == '__main__': unittest.main() diff --git a/chrome/common/extensions/docs/server2/api_list_data_source.py b/chrome/common/extensions/docs/server2/api_list_data_source.py index 5fd607e..b1a2f38 100644 --- a/chrome/common/extensions/docs/server2/api_list_data_source.py +++ b/chrome/common/extensions/docs/server2/api_list_data_source.py @@ -2,8 +2,10 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import logging import os +from file_system import FileNotFoundError import third_party.json_schema_compiler.model as model from docs_server_utils import SanitizeAPIName @@ -52,5 +54,6 @@ class APIListDataSource(object): def get(self, key): try: return self._cache.GetFromFileListing(self._api_path)[key] - except Exception as e: + except FileNotFoundError as e: + logging.error(e) return None diff --git a/chrome/common/extensions/docs/server2/converter.py b/chrome/common/extensions/docs/server2/converter.py index 2320663..53c5d06 100755 --- a/chrome/common/extensions/docs/server2/converter.py +++ b/chrome/common/extensions/docs/server2/converter.py @@ -37,7 +37,8 @@ IGNORED_FILES = [ # names of the JSON files do not give enough information on the actual API name. CUSTOM_MAPPINGS = { 'experimental_input_virtual_keyboard': 'experimental_input_virtualKeyboard', - 'input_ime': 'input_ime' + 'input_ime': 'input_ime', + 'app_window': 'app_window' } # These are the extension-only APIs that don't have explicit entries in diff --git a/chrome/common/extensions/docs/server2/file_system.py b/chrome/common/extensions/docs/server2/file_system.py index 873cdbf..51e54bd 100644 --- a/chrome/common/extensions/docs/server2/file_system.py +++ b/chrome/common/extensions/docs/server2/file_system.py @@ -4,6 +4,10 @@ import os +class FileNotFoundError(Exception): + def __init__(self, filename): + Exception.__init__(self, filename) + def _ProcessFileData(data, path): if os.path.splitext(path)[-1] not in ['.js', '.html', '.json']: return data diff --git a/chrome/common/extensions/docs/server2/intro_data_source.py b/chrome/common/extensions/docs/server2/intro_data_source.py index c256352..76b18eb 100644 --- a/chrome/common/extensions/docs/server2/intro_data_source.py +++ b/chrome/common/extensions/docs/server2/intro_data_source.py @@ -3,9 +3,11 @@ # found in the LICENSE file. from HTMLParser import HTMLParser +import logging import re from docs_server_utils import FormatKey +from file_system import FileNotFoundError from third_party.handlebar import Handlebar class _IntroParser(HTMLParser): @@ -74,9 +76,11 @@ class IntroDataSource(object): def get(self, key): real_path = FormatKey(key) + error = None for base_path in self._base_paths: try: return self._cache.GetFromFile(base_path + '/' + real_path) - except Exception: + except FileNotFoundError as error: pass + logging.error(error) return None diff --git a/chrome/common/extensions/docs/server2/local_file_system.py b/chrome/common/extensions/docs/server2/local_file_system.py index 7360e2a..89dfd9f 100644 --- a/chrome/common/extensions/docs/server2/local_file_system.py +++ b/chrome/common/extensions/docs/server2/local_file_system.py @@ -18,16 +18,23 @@ class LocalFileSystem(file_system.FileSystem): return path.replace('/', os.sep) def _ReadFile(self, filename, binary): - with open(os.path.join(self._base_path, filename), 'r') as f: - contents = f.read() - if binary: - return contents - return file_system._ProcessFileData(contents, filename) + try: + with open(os.path.join(self._base_path, filename), 'r') as f: + contents = f.read() + if binary: + return contents + return file_system._ProcessFileData(contents, filename) + except IOError: + raise file_system.FileNotFoundError(filename) def _ListDir(self, dir_name): all_files = [] full_path = os.path.join(self._base_path, dir_name) - for path in os.listdir(full_path): + try: + files = os.listdir(full_path) + except OSError: + raise file_system.FileNotFoundError(dir_name) + for path in files: if path.startswith('.'): continue if os.path.isdir(os.path.join(full_path, path)): @@ -46,4 +53,9 @@ class LocalFileSystem(file_system.FileSystem): return Future(value=result) def Stat(self, path): - return self.StatInfo(os.stat(os.path.join(self._base_path, path)).st_mtime) + try: + return self.StatInfo( + os.stat(os.path.join(self._base_path, path)).st_mtime) + except OSError: + raise file_system.FileNotFoundError(path) + diff --git a/chrome/common/extensions/docs/server2/server_instance.py b/chrome/common/extensions/docs/server2/server_instance.py index 2fa2a95..5c46aa2 100644 --- a/chrome/common/extensions/docs/server2/server_instance.py +++ b/chrome/common/extensions/docs/server2/server_instance.py @@ -6,6 +6,8 @@ from fnmatch import fnmatch import mimetypes import os +from file_system import FileNotFoundError + STATIC_DIR_PREFIX = 'docs/server2' DOCS_PATH = 'docs' @@ -27,11 +29,11 @@ class ServerInstance(object): """ try: result = self._cache.GetFromFile(STATIC_DIR_PREFIX + '/' + path) - base, ext = os.path.splitext(path) - response.headers['content-type'] = mimetypes.types_map[ext] - return result - except Exception: - return '' + except FileNotFoundError: + return None + base, ext = os.path.splitext(path) + response.headers['content-type'] = mimetypes.types_map[ext] + return result def Get(self, path, request, response): templates = self._template_data_source_factory.Create(request) diff --git a/chrome/common/extensions/docs/server2/subversion_file_system.py b/chrome/common/extensions/docs/server2/subversion_file_system.py index b5d2642..a27d788 100644 --- a/chrome/common/extensions/docs/server2/subversion_file_system.py +++ b/chrome/common/extensions/docs/server2/subversion_file_system.py @@ -19,8 +19,10 @@ class SubversionFileSystem(file_system.FileSystem): def Stat(self, path): directory = path.rsplit('/', 1)[0] - dir_html = self._fetcher.Fetch(directory + '/').content - return self.StatInfo(int(re.search('([0-9]+)', dir_html).group(0))) + result = self._fetcher.Fetch(directory + '/') + if result.status_code == 404: + raise file_system.FileNotFoundError(path) + return self.StatInfo(int(re.search('([0-9]+)', result.content).group(0))) class _AsyncFetchFuture(object): def __init__(self, paths, fetcher, binary): @@ -42,7 +44,7 @@ class _AsyncFetchFuture(object): for path, future in self._fetches: result = future.Get() if result.status_code == 404: - self._value[path] = None + raise file_system.FileNotFoundError(path) elif path.endswith('/'): self._value[path] = self._ListDir(result.content) elif not self._binary: diff --git a/chrome/common/extensions/docs/server2/template_data_source.py b/chrome/common/extensions/docs/server2/template_data_source.py index dcd81c4..3c36b71 100644 --- a/chrome/common/extensions/docs/server2/template_data_source.py +++ b/chrome/common/extensions/docs/server2/template_data_source.py @@ -5,6 +5,7 @@ import logging from docs_server_utils import FormatKey +from file_system import FileNotFoundError from third_party.handlebar import Handlebar EXTENSIONS_URL = '/chrome/extensions' @@ -125,6 +126,6 @@ class TemplateDataSource(object): real_path = FormatKey(template_name) try: return self._cache.GetFromFile(base_path + '/' + real_path) - except Exception as e: + except FileNotFoundError as e: logging.error(e) return None diff --git a/chrome/common/extensions/docs/server2/template_data_source_test.py b/chrome/common/extensions/docs/server2/template_data_source_test.py index b078d4ed..7a84bd6 100755 --- a/chrome/common/extensions/docs/server2/template_data_source_test.py +++ b/chrome/common/extensions/docs/server2/template_data_source_test.py @@ -52,8 +52,8 @@ class TemplateDataSourceTest(unittest.TestCase): self._fake_intro_data_source, self._fake_samples_data_source, cache_builder, - './', - './') + '.', + '.') .Create(_FakeRequest())) def testSimple(self): diff --git a/chrome/common/extensions/docs/server2/templates/intros/app_window.html b/chrome/common/extensions/docs/server2/templates/intros/app_window.html deleted file mode 100644 index e69de29..0000000 --- a/chrome/common/extensions/docs/server2/templates/intros/app_window.html +++ /dev/null diff --git a/chrome/common/extensions/docs/server2/templates/intros/events.html b/chrome/common/extensions/docs/server2/templates/intros/events.html index c307689..709c6f1 100644 --- a/chrome/common/extensions/docs/server2/templates/intros/events.html +++ b/chrome/common/extensions/docs/server2/templates/intros/events.html @@ -143,7 +143,7 @@ To retrieve a list of currently registered rules, call the <code>getRules()</code> function. It accepts an optional array of rule identifiers with the same semantics as <code>removeRules</code> and a callback function. -<p> +</p> <pre> var rule_ids = ["id1", "id2", ...]; @@ -154,4 +154,5 @@ function getRules(rule_ids, function callback(details) {...}); The <code>details</code> parameter passed to the <code>calback()</code> function refers to an array of rules including filled optional parameters. </p> -</div>
\ No newline at end of file +</div> +{{/is_apps}}
\ No newline at end of file diff --git a/chrome/common/extensions/docs/server2/templates/private/standard_apps_api.html b/chrome/common/extensions/docs/server2/templates/private/standard_apps_api.html index 6fc72c9..4a44898 100644 --- a/chrome/common/extensions/docs/server2/templates/private/standard_apps_api.html +++ b/chrome/common/extensions/docs/server2/templates/private/standard_apps_api.html @@ -23,7 +23,7 @@ {{/api.permissions.dev}} {{+partials.table_of_contents toc:intro.toc}} {{- This is unindented because it contains <pre> tags -}} -{{+intro.intro}} +{{+intro.intro is_apps:true}} {{+partials.api_reference}} </div> </div> diff --git a/chrome/common/extensions/docs/server2/templates/private/standard_apps_article.html b/chrome/common/extensions/docs/server2/templates/private/standard_apps_article.html index f34e418..8592c5a 100644 --- a/chrome/common/extensions/docs/server2/templates/private/standard_apps_article.html +++ b/chrome/common/extensions/docs/server2/templates/private/standard_apps_article.html @@ -14,7 +14,7 @@ {{+partials.table_of_contents toc:article.toc}} {{/article.toc}} {{- This may contain <pre> tags so it is not indented -}} -{{+article.intro}} +{{+article.intro is_apps:true}} </div> </div> </body> diff --git a/chrome/common/extensions/docs/server2/templates/private/standard_extensions_api.html b/chrome/common/extensions/docs/server2/templates/private/standard_extensions_api.html index c0a09c8..8a3e12b 100644 --- a/chrome/common/extensions/docs/server2/templates/private/standard_extensions_api.html +++ b/chrome/common/extensions/docs/server2/templates/private/standard_extensions_api.html @@ -23,7 +23,7 @@ {{/api.permissions.dev}} {{+partials.table_of_contents toc:intro.toc}} {{- This is unindented because it contains <pre> tags -}} -{{+intro.intro}} +{{+intro.intro is_apps:false}} {{+partials.api_reference}} </div> </div> diff --git a/chrome/common/extensions/docs/server2/templates/private/standard_extensions_article.html b/chrome/common/extensions/docs/server2/templates/private/standard_extensions_article.html index 6fad975..7a58370 100644 --- a/chrome/common/extensions/docs/server2/templates/private/standard_extensions_article.html +++ b/chrome/common/extensions/docs/server2/templates/private/standard_extensions_article.html @@ -14,7 +14,7 @@ {{+partials.table_of_contents toc:article.toc}} {{/article.toc}} {{- This may contain <pre> tags so it is not indented -}} -{{+article.intro}} +{{+article.intro is_apps:false}} </div> </div> </body> diff --git a/chrome/common/extensions/docs/server2/templates/public/apps/app_window.html b/chrome/common/extensions/docs/server2/templates/public/apps/app_window.html index fa6335a..2beef39 100644 --- a/chrome/common/extensions/docs/server2/templates/public/apps/app_window.html +++ b/chrome/common/extensions/docs/server2/templates/public/apps/app_window.html @@ -1 +1 @@ -{{+partials.standard_apps_api api:apis.app_window intro:intros.app_window}}
\ No newline at end of file +{{+partials.standard_apps_api api:apis.app_window}}
\ No newline at end of file diff --git a/chrome/common/extensions/docs/static/app.window.html b/chrome/common/extensions/docs/static/app.window.html deleted file mode 100644 index e69de29..0000000 --- a/chrome/common/extensions/docs/static/app.window.html +++ /dev/null diff --git a/chrome/common/extensions/docs/static/events.html b/chrome/common/extensions/docs/static/events.html index 9c23b53..7dc4032 100644 --- a/chrome/common/extensions/docs/static/events.html +++ b/chrome/common/extensions/docs/static/events.html @@ -142,7 +142,7 @@ To retrieve a list of currently registered rules, call the <code>getRules()</code> function. It accepts an optional array of rule identifiers with the same semantics as <code>removeRules</code> and a callback function. -<p> +</p> <pre> var rule_ids = ["id1", "id2", ...]; |