Index: chrome/common/extensions/docs/server2/host_file_system_provider.py |
diff --git a/chrome/common/extensions/docs/server2/host_file_system_creator.py b/chrome/common/extensions/docs/server2/host_file_system_provider.py |
similarity index 34% |
rename from chrome/common/extensions/docs/server2/host_file_system_creator.py |
rename to chrome/common/extensions/docs/server2/host_file_system_provider.py |
index cf741058ca9b6bec371bd768c076d0f655fb7902..3b8cf290b66232d48ce065c0a735809acd622fa4 100644 |
--- a/chrome/common/extensions/docs/server2/host_file_system_creator.py |
+++ b/chrome/common/extensions/docs/server2/host_file_system_provider.py |
@@ -6,35 +6,88 @@ from caching_file_system import CachingFileSystem |
from local_file_system import LocalFileSystem |
from offline_file_system import OfflineFileSystem |
from subversion_file_system import SubversionFileSystem |
+from third_party.json_schema_compiler.memoize import memoize |
-class HostFileSystemCreator(object): |
- '''Creates host file systems with configuration information. By default, SVN |
- file systems are created, although a constructor method can be passed in to |
- override this behavior (likely for testing purposes). |
+ |
+class HostFileSystemProvider(object): |
+ '''Provides host file systems ("host" meaning the file system that hosts the |
+ server's source code and templates) tracking trunk, or any branch. |
+ |
+ File system instances are memoized to maintain the in-memory caches across |
+ multiple callers. |
''' |
def __init__(self, |
object_store_creator, |
+ max_trunk_revision=None, |
+ default_trunk_instance=None, |
offline=False, |
constructor_for_test=None): |
+ ''' |
+ |object_store_creator| |
+ Provides caches for file systems that need one. |
+ |max_trunk_revision| |
+ If not None, the maximum revision that a 'trunk' file system will be |
+ created at. If None, 'trunk' file systems will use HEAD. |
+ |default_trunk_instance| |
+ If not None, 'trunk' file systems provided by this class without a |
+ specific revision will return |default_trunk_instance| instead. |
+ |offline| |
+ If True all provided file systems will be wrapped in an OfflineFileSystem. |
+ |constructor_for_test| |
+ Provides a custom constructor rather than creating SubversionFileSystems. |
+ ''' |
self._object_store_creator = object_store_creator |
- # Determines whether or not created file systems will be wrapped in an |
- # OfflineFileSystem. |
+ self._max_trunk_revision = max_trunk_revision |
+ self._default_trunk_instance = default_trunk_instance |
self._offline = offline |
- # Provides custom create behavior, useful in tests. |
self._constructor_for_test = constructor_for_test |
- def Create(self, branch='trunk', revision=None, offline=None): |
- '''Creates either SVN file systems or specialized file systems from the |
- constructor passed into this instance. Wraps the resulting file system in |
- an Offline file system if the offline flag is set, and finally wraps it in a |
- Caching file system. |
+ @memoize |
+ def GetTrunk(self, revision=None): |
+ '''Gets a file system tracking 'trunk'. Use this method rather than |
+ GetBranch('trunk') because the behaviour is subtly different; 'trunk' can |
+ be pinned to a max revision (|max_trunk_revision| in constructor) and can |
+ have its default instance overridden (|default_trunk_instance| in |
+ constructor). |
+ |
+ |revision| if non-None determines a specific revision to pin the host file |
+ system at, though it will be ignored if it exceeds |max_trunk_revision|. |
+ If None then |revision| will track |max_trunk_revision| if is has been |
+ set, or just HEAD (which might change during server runtime!). |
+ ''' |
+ if revision is None: |
+ if self._default_trunk_instance is not None: |
+ return self._default_trunk_instance |
+ return self._Create('trunk', revision=self._max_trunk_revision) |
+ if self._max_trunk_revision is not None: |
+ revision = min(revision, self._max_trunk_revision) |
+ return self._Create('trunk', revision=revision) |
+ |
+ @memoize |
+ def GetBranch(self, branch): |
+ '''Gets a file system tracking |branch|, for example '1150' - anything other |
+ than 'trunk', which must be constructed via the GetTrunk() method. |
+ |
+ Note: Unlike GetTrunk this function doesn't take a |revision| argument |
+ since we assume that branches hardly ever change, while trunk frequently |
+ changes. |
+ ''' |
+ assert isinstance(branch, basestring), 'Branch %s must be a string' % branch |
+ assert branch != 'trunk', 'Cannot specify branch=\'trunk\', use GetTrunk()' |
+ return self._Create(branch) |
+ |
+ def _Create(self, branch, revision=None): |
+ '''Creates SVN file systems (or if in a test, potentially whatever |
+ |self._constructor_for_test specifies). Wraps the resulting file system in |
+ an Offline file system if the offline flag is set, and finally wraps it in |
+ a Caching file system. |
''' |
if self._constructor_for_test is not None: |
file_system = self._constructor_for_test(branch=branch, revision=revision) |
else: |
file_system = SubversionFileSystem.Create(branch=branch, |
revision=revision) |
- if offline or (offline is None and self._offline): |
+ if self._offline: |
file_system = OfflineFileSystem(file_system) |
return CachingFileSystem(file_system, self._object_store_creator) |
@@ -42,16 +95,16 @@ class HostFileSystemCreator(object): |
def ForLocal(object_store_creator): |
'''Used in creating a server instance on localhost. |
''' |
- return HostFileSystemCreator( |
+ return HostFileSystemProvider( |
object_store_creator, |
constructor_for_test=lambda **_: LocalFileSystem.Create()) |
@staticmethod |
def ForTest(file_system, object_store_creator): |
- '''Used in creating a test server instance. The HostFileSystemCreator |
+ '''Used in creating a test server instance. The HostFileSystemProvider |
returned here will always return |file_system| when its Create() method is |
called. |
''' |
- return HostFileSystemCreator( |
+ return HostFileSystemProvider( |
object_store_creator, |
constructor_for_test=lambda **_: file_system) |