Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: chrome/common/extensions/docs/server2/server_instance.py

Issue 10830252: Extensions Docs Server: Uniform handling of file not found errors (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 from fnmatch import fnmatch 5 from fnmatch import fnmatch
6 import mimetypes 6 import mimetypes
7 import os 7 import os
8 8
9 from file_system import FileNotFoundError
10
9 STATIC_DIR_PREFIX = 'docs/server2' 11 STATIC_DIR_PREFIX = 'docs/server2'
10 DOCS_PATH = 'docs' 12 DOCS_PATH = 'docs'
11 13
12 class ServerInstance(object): 14 class ServerInstance(object):
13 """This class is used to hold a data source and fetcher for an instance of a 15 """This class is used to hold a data source and fetcher for an instance of a
14 server. Each new branch will get its own ServerInstance. 16 server. Each new branch will get its own ServerInstance.
15 """ 17 """
16 def __init__(self, 18 def __init__(self,
17 template_data_source_factory, 19 template_data_source_factory,
18 example_zipper, 20 example_zipper,
19 cache_builder): 21 cache_builder):
20 self._template_data_source_factory = template_data_source_factory 22 self._template_data_source_factory = template_data_source_factory
21 self._example_zipper = example_zipper 23 self._example_zipper = example_zipper
22 self._cache = cache_builder.build(lambda x: x) 24 self._cache = cache_builder.build(lambda x: x)
23 mimetypes.init() 25 mimetypes.init()
24 26
25 def _FetchStaticResource(self, path, response): 27 def _FetchStaticResource(self, path, response):
26 """Fetch a resource in the 'static' directory. 28 """Fetch a resource in the 'static' directory.
27 """ 29 """
28 try: 30 try:
29 result = self._cache.GetFromFile(STATIC_DIR_PREFIX + '/' + path) 31 result = self._cache.GetFromFile(STATIC_DIR_PREFIX + '/' + path)
30 base, ext = os.path.splitext(path) 32 base, ext = os.path.splitext(path)
31 response.headers['content-type'] = mimetypes.types_map[ext] 33 response.headers['content-type'] = mimetypes.types_map[ext]
32 return result 34 return result
33 except Exception: 35 except FileNotFoundError:
34 return '' 36 return None
35 37
36 def Get(self, path, request, response): 38 def Get(self, path, request, response):
37 templates = self._template_data_source_factory.Create(request) 39 templates = self._template_data_source_factory.Create(request)
38 40
39 if fnmatch(path, 'extensions/examples/*.zip'): 41 if fnmatch(path, 'extensions/examples/*.zip'):
40 content = self._example_zipper.Create( 42 content = self._example_zipper.Create(
41 path[len('extensions/'):-len('.zip')]) 43 path[len('extensions/'):-len('.zip')])
42 response.headers['content-type'] = mimetypes.types_map['.zip'] 44 response.headers['content-type'] = mimetypes.types_map['.zip']
43 elif path.startswith('extensions/examples/'): 45 elif path.startswith('extensions/examples/'):
44 content = self._cache.GetFromFile( 46 content = self._cache.GetFromFile(
45 DOCS_PATH + '/' + path[len('extensions/'):]) 47 DOCS_PATH + '/' + path[len('extensions/'):])
46 response.headers['content-type'] = 'text/plain' 48 response.headers['content-type'] = 'text/plain'
47 elif path.startswith('static/'): 49 elif path.startswith('static/'):
48 content = self._FetchStaticResource(path, response) 50 content = self._FetchStaticResource(path, response)
49 else: 51 else:
50 content = templates.Render(path) 52 content = templates.Render(path)
51 53
52 if content: 54 if content:
53 response.out.write(content) 55 response.out.write(content)
54 else: 56 else:
55 response.set_status(404); 57 response.set_status(404);
56 response.out.write(templates.Render('404')) 58 response.out.write(templates.Render('404'))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698