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
3 # found in the LICENSE file.
5 from api_categorizer
import APICategorizer
6 from api_data_source
import APIDataSource
7 from api_list_data_source
import APIListDataSource
8 from api_models
import APIModels
9 from availability_finder
import AvailabilityFinder
10 from compiled_file_system
import CompiledFileSystem
11 from content_providers
import ContentProviders
12 from document_renderer
import DocumentRenderer
13 from empty_dir_file_system
import EmptyDirFileSystem
14 from environment
import IsDevServer
15 from features_bundle
import FeaturesBundle
16 from github_file_system_provider
import GithubFileSystemProvider
17 from host_file_system_provider
import HostFileSystemProvider
18 from host_file_system_iterator
import HostFileSystemIterator
19 from object_store_creator
import ObjectStoreCreator
20 from path_canonicalizer
import PathCanonicalizer
21 from reference_resolver
import ReferenceResolver
22 from samples_data_source
import SamplesDataSource
23 from table_of_contents_renderer
import TableOfContentsRenderer
24 from template_renderer
import TemplateRenderer
25 from test_branch_utility
import TestBranchUtility
26 from test_object_store
import TestObjectStore
29 class ServerInstance(object):
35 host_file_system_provider
,
36 github_file_system_provider
,
39 |object_store_creator|
40 The ObjectStoreCreator used to create almost all caches.
42 Factory used to create CompiledFileSystems, a higher-level cache type
43 than ObjectStores. This can usually be derived from just
44 |object_store_creator| but under special circumstances a different
45 implementation needs to be passed in.
47 Has knowledge of Chrome branches, channels, and versions.
48 |host_file_system_provider|
49 Creates FileSystem instances which host the server at alternative
51 |github_file_system_provider|
52 Creates FileSystem instances backed by GitHub.
54 The path which all HTML is generated relative to. Usually this is /
55 but some servlets need to override this.
57 self
.object_store_creator
= object_store_creator
59 self
.compiled_fs_factory
= compiled_fs_factory
61 self
.host_file_system_provider
= host_file_system_provider
62 host_fs_at_trunk
= host_file_system_provider
.GetTrunk()
64 self
.github_file_system_provider
= github_file_system_provider
66 assert base_path
.startswith('/') and base_path
.endswith('/')
67 self
.base_path
= base_path
69 self
.host_file_system_iterator
= HostFileSystemIterator(
70 host_file_system_provider
,
73 self
.features_bundle
= FeaturesBundle(
75 self
.compiled_fs_factory
,
76 self
.object_store_creator
)
78 self
.api_models
= APIModels(
80 self
.compiled_fs_factory
,
83 self
.availability_finder
= AvailabilityFinder(
86 self
.host_file_system_iterator
,
90 self
.api_categorizer
= APICategorizer(
94 self
.api_data_source_factory
= APIDataSource
.Factory(
95 self
.compiled_fs_factory
,
97 self
.availability_finder
,
99 self
.object_store_creator
)
101 self
.api_list_data_source_factory
= APIListDataSource
.Factory(
102 self
.compiled_fs_factory
,
104 self
.features_bundle
,
105 self
.object_store_creator
,
107 self
.availability_finder
,
108 self
.api_categorizer
)
110 self
.ref_resolver_factory
= ReferenceResolver
.Factory(
111 self
.api_data_source_factory
,
113 object_store_creator
)
115 self
.api_data_source_factory
.SetReferenceResolverFactory(
116 self
.ref_resolver_factory
)
118 # Note: samples are super slow in the dev server because it doesn't support
119 # async fetch, so disable them.
121 extension_samples_fs
= EmptyDirFileSystem()
122 app_samples_fs
= EmptyDirFileSystem()
124 extension_samples_fs
= host_fs_at_trunk
125 app_samples_fs
= github_file_system_provider
.Create(
126 'GoogleChrome', 'chrome-app-samples')
127 self
.samples_data_source_factory
= SamplesDataSource
.Factory(
128 extension_samples_fs
,
130 CompiledFileSystem
.Factory(object_store_creator
),
131 self
.ref_resolver_factory
,
134 self
.api_data_source_factory
.SetSamplesDataSourceFactory(
135 self
.samples_data_source_factory
)
137 self
.path_canonicalizer
= PathCanonicalizer(
138 self
.compiled_fs_factory
,
141 self
.content_providers
= ContentProviders(
142 self
.compiled_fs_factory
,
144 self
.github_file_system_provider
)
146 # TODO(kalman): Move all the remaining DataSources into DataSourceRegistry,
147 # then factor out the DataSource creation into a factory method, so that
148 # the entire ServerInstance doesn't need to be passed in here.
149 self
.template_renderer
= TemplateRenderer(self
)
151 # TODO(kalman): It may be better for |document_renderer| to construct a
152 # TemplateDataSource itself rather than depending on template_renderer, but
153 # for that the above todo should be addressed.
154 self
.document_renderer
= DocumentRenderer(TableOfContentsRenderer(
157 self
.template_renderer
))
160 def ForTest(file_system
=None, file_system_provider
=None, base_path
='/'):
161 object_store_creator
= ObjectStoreCreator
.ForTest()
162 if file_system
is None and file_system_provider
is None:
163 raise ValueError('Either |file_system| or |file_system_provider| '
165 if file_system
and file_system_provider
:
166 raise ValueError('Only one of |file_system| and |file_system_provider| '
168 if file_system_provider
is None:
169 file_system_provider
= HostFileSystemProvider
.ForTest(
171 object_store_creator
)
172 return ServerInstance(object_store_creator
,
173 CompiledFileSystem
.Factory(object_store_creator
),
174 TestBranchUtility
.CreateWithCannedData(),
175 file_system_provider
,
176 GithubFileSystemProvider
.ForEmpty(),
181 object_store_creator
= ObjectStoreCreator(start_empty
=False,
182 store_type
=TestObjectStore
)
183 host_file_system_provider
= HostFileSystemProvider
.ForLocal(
184 object_store_creator
)
185 return ServerInstance(
186 object_store_creator
,
187 CompiledFileSystem
.Factory(object_store_creator
),
188 TestBranchUtility
.CreateWithCannedData(),
189 host_file_system_provider
,
190 GithubFileSystemProvider
.ForEmpty())