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_data_source
import APIDataSource
6 from api_list_data_source
import APIListDataSource
7 from branch_utility
import BranchUtility
8 from compiled_file_system
import CompiledFileSystem
9 from content_providers
import ContentProviders
10 from document_renderer
import DocumentRenderer
11 from empty_dir_file_system
import EmptyDirFileSystem
12 from environment
import IsDevServer
13 from gcs_file_system_provider
import CloudStorageFileSystemProvider
14 from host_file_system_iterator
import HostFileSystemIterator
15 from host_file_system_provider
import HostFileSystemProvider
16 from object_store_creator
import ObjectStoreCreator
17 from platform_bundle
import PlatformBundle
18 from samples_data_source
import SamplesDataSource
19 from table_of_contents_renderer
import TableOfContentsRenderer
20 from template_renderer
import TemplateRenderer
21 from test_branch_utility
import TestBranchUtility
22 from test_object_store
import TestObjectStore
25 class ServerInstance(object):
31 host_file_system_provider
,
32 gcs_file_system_provider
,
35 |object_store_creator|
36 The ObjectStoreCreator used to create almost all caches.
38 Factory used to create CompiledFileSystems, a higher-level cache type
39 than ObjectStores. This can usually be derived from just
40 |object_store_creator| but under special circumstances a different
41 implementation needs to be passed in.
43 Has knowledge of Chrome branches, channels, and versions.
44 |host_file_system_provider|
45 Creates FileSystem instances which host the server at alternative
48 The path which all HTML is generated relative to. Usually this is /
49 but some servlets need to override this.
51 self
.object_store_creator
= object_store_creator
53 self
.compiled_fs_factory
= compiled_fs_factory
55 self
.host_file_system_provider
= host_file_system_provider
56 host_fs_at_master
= host_file_system_provider
.GetMaster()
58 self
.gcs_file_system_provider
= gcs_file_system_provider
60 assert base_path
.startswith('/') and base_path
.endswith('/')
61 self
.base_path
= base_path
63 self
.host_file_system_iterator
= HostFileSystemIterator(
64 host_file_system_provider
,
67 self
.platform_bundle
= PlatformBundle(
69 self
.compiled_fs_factory
,
71 self
.host_file_system_iterator
,
72 self
.object_store_creator
,
75 self
.content_providers
= ContentProviders(
77 self
.compiled_fs_factory
,
79 self
.gcs_file_system_provider
)
81 # TODO(kalman): Move all the remaining DataSources into DataSourceRegistry,
82 # then factor out the DataSource creation into a factory method, so that
83 # the entire ServerInstance doesn't need to be passed in here.
84 self
.template_renderer
= TemplateRenderer(self
)
86 # TODO(kalman): It may be better for |document_renderer| to construct a
87 # TemplateDataSource itself rather than depending on template_renderer, but
88 # for that the above todo should be addressed.
89 self
.document_renderer
= DocumentRenderer(
90 TableOfContentsRenderer(host_fs_at_master
,
92 self
.template_renderer
),
96 def ForTest(file_system
=None, file_system_provider
=None, base_path
='/'):
97 object_store_creator
= ObjectStoreCreator
.ForTest()
98 if file_system
is None and file_system_provider
is None:
99 raise ValueError('Either |file_system| or |file_system_provider| '
101 if file_system
and file_system_provider
:
102 raise ValueError('Only one of |file_system| and |file_system_provider| '
104 if file_system_provider
is None:
105 file_system_provider
= HostFileSystemProvider
.ForTest(
107 object_store_creator
)
108 return ServerInstance(object_store_creator
,
109 CompiledFileSystem
.Factory(object_store_creator
),
110 TestBranchUtility
.CreateWithCannedData(),
111 file_system_provider
,
112 CloudStorageFileSystemProvider(object_store_creator
),
117 object_store_creator
= ObjectStoreCreator(start_empty
=False,
118 store_type
=TestObjectStore
)
119 host_file_system_provider
= HostFileSystemProvider
.ForLocal(
120 object_store_creator
)
121 return ServerInstance(
122 object_store_creator
,
123 CompiledFileSystem
.Factory(object_store_creator
),
124 BranchUtility
.Create(object_store_creator
),
125 host_file_system_provider
,
126 CloudStorageFileSystemProvider(object_store_creator
))