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 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 | 9 from file_system import FileNotFoundError |
| 10 import file_system_cache as fs_cache |
10 | 11 |
11 STATIC_DIR_PREFIX = 'docs/server2' | 12 STATIC_DIR_PREFIX = 'docs/server2' |
12 DOCS_PATH = 'docs' | 13 DOCS_PATH = 'docs' |
13 | 14 |
14 class ServerInstance(object): | 15 class ServerInstance(object): |
15 """This class is used to hold a data source and fetcher for an instance of a | 16 """This class is used to hold a data source and fetcher for an instance of a |
16 server. Each new branch will get its own ServerInstance. | 17 server. Each new branch will get its own ServerInstance. |
17 """ | 18 """ |
18 def __init__(self, | 19 def __init__(self, |
19 template_data_source_factory, | 20 template_data_source_factory, |
20 example_zipper, | 21 example_zipper, |
21 cache_builder): | 22 cache_builder): |
22 self._template_data_source_factory = template_data_source_factory | 23 self._template_data_source_factory = template_data_source_factory |
23 self._example_zipper = example_zipper | 24 self._example_zipper = example_zipper |
24 self._cache = cache_builder.build(lambda x: x) | 25 self._cache = cache_builder.build(lambda x: x, fs_cache.STATIC) |
25 mimetypes.init() | 26 mimetypes.init() |
26 | 27 |
27 def _FetchStaticResource(self, path, response): | 28 def _FetchStaticResource(self, path, response): |
28 """Fetch a resource in the 'static' directory. | 29 """Fetch a resource in the 'static' directory. |
29 """ | 30 """ |
30 try: | 31 try: |
31 result = self._cache.GetFromFile(STATIC_DIR_PREFIX + '/' + path) | 32 result = self._cache.GetFromFile(STATIC_DIR_PREFIX + '/' + path) |
32 except FileNotFoundError: | 33 except FileNotFoundError: |
33 return None | 34 return None |
34 base, ext = os.path.splitext(path) | 35 base, ext = os.path.splitext(path) |
(...skipping 14 matching lines...) Expand all Loading... |
49 elif path.startswith('static/'): | 50 elif path.startswith('static/'): |
50 content = self._FetchStaticResource(path, response) | 51 content = self._FetchStaticResource(path, response) |
51 else: | 52 else: |
52 content = templates.Render(path) | 53 content = templates.Render(path) |
53 | 54 |
54 if content: | 55 if content: |
55 response.out.write(content) | 56 response.out.write(content) |
56 else: | 57 else: |
57 response.set_status(404); | 58 response.set_status(404); |
58 response.out.write(templates.Render('404')) | 59 response.out.write(templates.Render('404')) |
OLD | NEW |