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 api_data_source import APIDataSource |
| 10 from api_list_data_source import APIListDataSource |
| 11 from appengine_memcache import AppEngineMemcache |
| 12 from example_zipper import ExampleZipper |
| 13 from file_system_cache import FileSystemCache |
| 14 from intro_data_source import IntroDataSource |
| 15 from local_file_system import LocalFileSystem |
| 16 from memcache_file_system import MemcacheFileSystem |
| 17 from samples_data_source import SamplesDataSource |
| 18 from subversion_file_system import SubversionFileSystem |
| 19 from template_data_source import TemplateDataSource |
| 20 from appengine_url_fetcher import AppEngineUrlFetcher |
| 21 |
9 STATIC_DIR_PREFIX = 'docs/server2' | 22 STATIC_DIR_PREFIX = 'docs/server2' |
10 DOCS_PREFIX = 'docs' | 23 EXTENSIONS_PATH = 'chrome/common/extensions' |
| 24 DOCS_PATH = 'docs' |
| 25 API_PATH = 'api' |
| 26 INTRO_PATH = DOCS_PATH + '/server2/templates/intros' |
| 27 ARTICLE_PATH = DOCS_PATH + '/server2/templates/articles' |
| 28 PUBLIC_TEMPLATE_PATH = DOCS_PATH + '/server2/templates/public' |
| 29 PRIVATE_TEMPLATE_PATH = DOCS_PATH + '/server2/templates/private' |
| 30 EXAMPLES_PATH = DOCS_PATH + '/examples' |
| 31 FULL_EXAMPLES_PATH = DOCS_PATH + '/' + EXAMPLES_PATH |
| 32 |
| 33 # The branch that the server will default to when no branch is specified in the |
| 34 # URL. This is necessary because it is not possible to pass flags to the script |
| 35 # handler. |
| 36 DEFAULT_BRANCH = 'local' |
| 37 |
| 38 # Global cache of instances because Handler is recreated for every request. |
| 39 SERVER_INSTANCES = {} |
| 40 |
| 41 def GetInstanceForBranch(branch, |
| 42 memcache, |
| 43 urlfetch, |
| 44 local_path=EXTENSIONS_PATH): |
| 45 if branch in SERVER_INSTANCES: |
| 46 return SERVER_INSTANCES[branch] |
| 47 if branch == 'local': |
| 48 file_system = LocalFileSystem(local_path) |
| 49 else: |
| 50 fetcher = AppEngineUrlFetcher( |
| 51 _GetURLFromBranch(branch) + '/' + EXTENSIONS_PATH, |
| 52 urlfetch) |
| 53 file_system = MemcacheFileSystem(SubversionFileSystem(fetcher), |
| 54 AppEngineMemcache(branch, memcache)) |
| 55 |
| 56 cache_builder = FileSystemCache.Builder(file_system) |
| 57 api_data_source = APIDataSource(cache_builder, API_PATH) |
| 58 api_list_data_source = APIListDataSource(cache_builder, |
| 59 file_system, |
| 60 API_PATH, |
| 61 PUBLIC_TEMPLATE_PATH) |
| 62 intro_data_source = IntroDataSource(cache_builder, |
| 63 [INTRO_PATH, ARTICLE_PATH]) |
| 64 samples_data_source_factory = SamplesDataSource.Factory(branch, |
| 65 file_system, |
| 66 cache_builder, |
| 67 EXAMPLES_PATH) |
| 68 template_data_source_factory = TemplateDataSource.Factory( |
| 69 branch, |
| 70 api_data_source, |
| 71 api_list_data_source, |
| 72 intro_data_source, |
| 73 samples_data_source_factory, |
| 74 cache_builder, |
| 75 PUBLIC_TEMPLATE_PATH, |
| 76 PRIVATE_TEMPLATE_PATH) |
| 77 example_zipper = ExampleZipper(file_system, |
| 78 cache_builder, |
| 79 DOCS_PATH, |
| 80 EXAMPLES_PATH) |
| 81 SERVER_INSTANCES[branch] = ServerInstance( |
| 82 template_data_source_factory, |
| 83 example_zipper, |
| 84 cache_builder) |
| 85 return SERVER_INSTANCES[branch] |
11 | 86 |
12 class ServerInstance(object): | 87 class ServerInstance(object): |
13 """This class is used to hold a data source and fetcher for an instance of a | 88 """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. | 89 server. Each new branch will get its own ServerInstance. |
15 """ | 90 """ |
16 def __init__(self, | 91 def __init__(self, |
17 template_data_source_factory, | 92 template_data_source_factory, |
18 example_zipper, | 93 example_zipper, |
19 cache_builder): | 94 cache_builder): |
20 self._template_data_source_factory = template_data_source_factory | 95 self._template_data_source_factory = template_data_source_factory |
(...skipping 12 matching lines...) Expand all Loading... |
33 except Exception: | 108 except Exception: |
34 return '' | 109 return '' |
35 | 110 |
36 def Get(self, path, request, response): | 111 def Get(self, path, request, response): |
37 templates = self._template_data_source_factory.Create(request) | 112 templates = self._template_data_source_factory.Create(request) |
38 | 113 |
39 if fnmatch(path, 'examples/*.zip'): | 114 if fnmatch(path, 'examples/*.zip'): |
40 content = self._example_zipper.Create(path[:-len('.zip')]) | 115 content = self._example_zipper.Create(path[:-len('.zip')]) |
41 response.headers['content-type'] = mimetypes.types_map['.zip'] | 116 response.headers['content-type'] = mimetypes.types_map['.zip'] |
42 elif path.startswith('examples/'): | 117 elif path.startswith('examples/'): |
43 content = self._cache.GetFromFile(DOCS_PREFIX + '/' + path) | 118 content = self._cache.GetFromFile(DOCS_PATH + '/' + path) |
44 response.headers['content-type'] = 'text/plain' | 119 response.headers['content-type'] = 'text/plain' |
45 elif path.startswith('static/'): | 120 elif path.startswith('static/'): |
46 content = self._FetchStaticResource(path, response) | 121 content = self._FetchStaticResource(path, response) |
47 else: | 122 else: |
48 content = templates.Render(path) | 123 content = templates.Render(path) |
49 | 124 |
50 if content: | 125 if content: |
51 response.out.write(content) | 126 response.out.write(content) |
52 else: | 127 else: |
53 response.set_status(404); | 128 response.set_status(404); |
54 response.out.write(templates.Render('404')) | 129 response.out.write(templates.Render('404')) |
OLD | NEW |