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

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

Issue 26418002: Docserver: Pull knowledge of host file systems into a single (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: correct similarity Created 7 years, 2 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 2013 The Chromium Authors. All rights reserved. 1 # Copyright 2013 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 caching_file_system import CachingFileSystem 5 from caching_file_system import CachingFileSystem
6 from local_file_system import LocalFileSystem 6 from local_file_system import LocalFileSystem
7 from offline_file_system import OfflineFileSystem 7 from offline_file_system import OfflineFileSystem
8 from subversion_file_system import SubversionFileSystem 8 from subversion_file_system import SubversionFileSystem
9 from third_party.json_schema_compiler.memoize import memoize
9 10
10 class HostFileSystemCreator(object): 11
11 '''Creates host file systems with configuration information. By default, SVN 12 class HostFileSystemProvider(object):
12 file systems are created, although a constructor method can be passed in to 13 '''Provides host file systems ("host" meaning the file system that hosts the
13 override this behavior (likely for testing purposes). 14 server's source code and templates) tracking trunk, or any branch.
15
16 File system instances are memoized to maintain the in-memory caches across
17 multiple callers.
14 ''' 18 '''
15 def __init__(self, 19 def __init__(self,
16 object_store_creator, 20 object_store_creator,
21 max_trunk_revision=None,
22 default_trunk_instance=None,
17 offline=False, 23 offline=False,
18 constructor_for_test=None): 24 constructor_for_test=None):
25 '''
26 |object_store_creator|
27 Provides caches for file systems that need one.
28 |max_trunk_revision|
29 If not None, the maximum revision that a 'trunk' file system will be
30 created at. If None, 'trunk' file systems will use HEAD.
31 |default_trunk_instance|
32 If not None, 'trunk' file systems provided by this class without a
33 specific revision will return |default_trunk_instance| instead.
34 |offline|
35 If True all provided file systems will be wrapped in an OfflineFileSystem.
36 |constructor_for_test|
37 Provides a custom constructor rather than creating SubversionFileSystems.
38 '''
19 self._object_store_creator = object_store_creator 39 self._object_store_creator = object_store_creator
20 # Determines whether or not created file systems will be wrapped in an 40 self._max_trunk_revision = max_trunk_revision
21 # OfflineFileSystem. 41 self._default_trunk_instance = default_trunk_instance
22 self._offline = offline 42 self._offline = offline
23 # Provides custom create behavior, useful in tests.
24 self._constructor_for_test = constructor_for_test 43 self._constructor_for_test = constructor_for_test
25 44
26 def Create(self, branch='trunk', revision=None, offline=None): 45 @memoize
27 '''Creates either SVN file systems or specialized file systems from the 46 def GetTrunk(self, revision=None):
28 constructor passed into this instance. Wraps the resulting file system in 47 '''Gets a file system tracking 'trunk'. Use this method rather than
29 an Offline file system if the offline flag is set, and finally wraps it in a 48 GetBranch('trunk') because the behaviour is subtly different; 'trunk' can
30 Caching file system. 49 be pinned to a max revision (|max_trunk_revision| in constructor) and can
50 have its default instance overridden (|default_trunk_instance| in
51 constructor).
52
53 |revision| if non-None determines a specific revision to pin the host file
54 system at, though it will be ignored if it exceeds |max_trunk_revision|.
55 If None then |revision| will track |max_trunk_revision| if is has been
56 set, or just HEAD (which might change during server runtime!).
57 '''
58 if revision is None:
59 if self._default_trunk_instance is not None:
60 return self._default_trunk_instance
61 return self._Create('trunk', revision=self._max_trunk_revision)
62 if self._max_trunk_revision is not None:
63 revision = min(revision, self._max_trunk_revision)
64 return self._Create('trunk', revision=revision)
65
66 @memoize
67 def GetBranch(self, branch):
68 '''Gets a file system tracking |branch|, for example '1150' - anything other
69 than 'trunk', which must be constructed via the GetTrunk() method.
70
71 Note: Unlike GetTrunk this function doesn't take a |revision| argument
72 since we assume that branches hardly ever change, while trunk frequently
73 changes.
74 '''
75 assert isinstance(branch, basestring), 'Branch %s must be a string' % branch
76 assert branch != 'trunk', 'Cannot specify branch=\'trunk\', use GetTrunk()'
77 return self._Create(branch)
78
79 def _Create(self, branch, revision=None):
80 '''Creates SVN file systems (or if in a test, potentially whatever
81 |self._constructor_for_test specifies). Wraps the resulting file system in
82 an Offline file system if the offline flag is set, and finally wraps it in
83 a Caching file system.
31 ''' 84 '''
32 if self._constructor_for_test is not None: 85 if self._constructor_for_test is not None:
33 file_system = self._constructor_for_test(branch=branch, revision=revision) 86 file_system = self._constructor_for_test(branch=branch, revision=revision)
34 else: 87 else:
35 file_system = SubversionFileSystem.Create(branch=branch, 88 file_system = SubversionFileSystem.Create(branch=branch,
36 revision=revision) 89 revision=revision)
37 if offline or (offline is None and self._offline): 90 if self._offline:
38 file_system = OfflineFileSystem(file_system) 91 file_system = OfflineFileSystem(file_system)
39 return CachingFileSystem(file_system, self._object_store_creator) 92 return CachingFileSystem(file_system, self._object_store_creator)
40 93
41 @staticmethod 94 @staticmethod
42 def ForLocal(object_store_creator): 95 def ForLocal(object_store_creator):
43 '''Used in creating a server instance on localhost. 96 '''Used in creating a server instance on localhost.
44 ''' 97 '''
45 return HostFileSystemCreator( 98 return HostFileSystemProvider(
46 object_store_creator, 99 object_store_creator,
47 constructor_for_test=lambda **_: LocalFileSystem.Create()) 100 constructor_for_test=lambda **_: LocalFileSystem.Create())
48 101
49 @staticmethod 102 @staticmethod
50 def ForTest(file_system, object_store_creator): 103 def ForTest(file_system, object_store_creator):
51 '''Used in creating a test server instance. The HostFileSystemCreator 104 '''Used in creating a test server instance. The HostFileSystemProvider
52 returned here will always return |file_system| when its Create() method is 105 returned here will always return |file_system| when its Create() method is
53 called. 106 called.
54 ''' 107 '''
55 return HostFileSystemCreator( 108 return HostFileSystemProvider(
56 object_store_creator, 109 object_store_creator,
57 constructor_for_test=lambda **_: file_system) 110 constructor_for_test=lambda **_: file_system)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698