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

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

Issue 14125010: Docserver: Add support for viewing docs with a codereview patch applied (Closed) Base URL: https://src.chromium.org/svn/trunk/src/
Patch Set: rebase Created 7 years, 7 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
OLDNEW
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 class _CacheEntry(object): 5 class _CacheEntry(object):
6 def __init__(self, cache_data, version): 6 def __init__(self, cache_data, version):
7 self._cache_data = cache_data 7 self.cache_data = cache_data
8 self.version = version 8 self.version = version
9 9
10 class CompiledFileSystem(object): 10 class CompiledFileSystem(object):
11 """This class caches FileSystem data that has been processed. 11 """This class caches FileSystem data that has been processed.
12 """ 12 """
13 class Factory(object): 13 class Factory(object):
14 """A class to build a CompiledFileSystem backed by |file_system|. 14 """A class to build a CompiledFileSystem backed by |file_system|.
15 """ 15 """
16 def __init__(self, file_system, object_store_creator): 16 def __init__(self, file_system, object_store_creator):
not at google - send to devlin 2013/05/11 20:39:53 See comments in CacheChainFileSystem. I don't thin
方觉(Fang Jue) 2013/05/12 03:01:47 No. Factory doesn't need to be changed. But there
17 self._file_system = file_system 17 self._file_system = file_system
18 self._object_store_creator = object_store_creator 18 self._object_store_creator = object_store_creator
19 19
20 def Create(self, populate_function, cls, category=None): 20 def Create(self, populate_function, cls, category=None):
21 """Create a CompiledFileSystem that populates the cache by calling 21 """Create a CompiledFileSystem that populates the cache by calling
22 |populate_function| with (path, data), where |data| is the data that was 22 |populate_function| with (path, data), where |data| is the data that was
23 fetched from |path|. 23 fetched from |path|.
24 The namespace for the file system is derived like ObjectStoreCreator: from 24 The namespace for the file system is derived like ObjectStoreCreator: from
25 |cls| along with an optional |category|. 25 |cls| along with an optional |category|.
26 """ 26 """
27 return self._Create(populate_function, cls, category)
28
29 def _Create(self,
30 populate_function,
31 cls,
32 category=None,
33 constructor=None):
27 assert isinstance(cls, type) 34 assert isinstance(cls, type)
28 assert not cls.__name__[0].islower() # guard against non-class types 35 assert not cls.__name__[0].islower() # guard against non-class types
29 full_name = cls.__name__ 36 full_name = cls.__name__
30 if category is not None: 37 if category is not None:
31 full_name = '%s/%s' % (full_name, category) 38 full_name = '%s/%s' % (full_name, category)
32 def create_object_store(category): 39 def create_object_store(category):
33 return self._object_store_creator.Create( 40 return self._object_store_creator.Create(
34 CompiledFileSystem, category='%s/%s' % (full_name, category)) 41 CompiledFileSystem, category='%s/%s' % (full_name, category))
35 return CompiledFileSystem(self._file_system, 42 if constructor is None:
36 populate_function, 43 constructor = CompiledFileSystem
37 create_object_store('file'), 44 return constructor(self._file_system,
38 create_object_store('list')) 45 populate_function,
46 create_object_store('file'),
47 create_object_store('list'))
39 48
40 def CreateIdentity(self, cls): 49 def CreateIdentity(self, cls):
41 '''Handy helper to get or create the identity compiled file system. 50 '''Handy helper to get or create the identity compiled file system.
42 GetFromFile will return the file's contents. 51 GetFromFile will return the file's contents.
43 GetFromFileListing will return the directory list. 52 GetFromFileListing will return the directory list.
44 ''' 53 '''
45 return self.Create(lambda _, x: x, cls) 54 return self.Create(lambda _, x: x, cls)
46 55
47 def __init__(self, 56 def __init__(self,
48 file_system, 57 file_system,
(...skipping 14 matching lines...) Expand all
63 else: 72 else:
64 files.append(filename) 73 files.append(filename)
65 return files 74 return files
66 75
67 def GetFromFile(self, path, binary=False): 76 def GetFromFile(self, path, binary=False):
68 """Calls |populate_function| on the contents of the file at |path|. If 77 """Calls |populate_function| on the contents of the file at |path|. If
69 |binary| is True then the file will be read as binary - but this will only 78 |binary| is True then the file will be read as binary - but this will only
70 apply for the first time the file is fetched; if already cached, |binary| 79 apply for the first time the file is fetched; if already cached, |binary|
71 will be ignored. 80 will be ignored.
72 """ 81 """
82 return self._GetCacheEntryFromFile(path, binary).cache_data
83
84 def _GetCacheEntryFromFile(self, path, binary=False):
73 version = self._file_system.Stat(path).version 85 version = self._file_system.Stat(path).version
74 cache_entry = self._file_object_store.Get(path).Get() 86 cache_entry = self._file_object_store.Get(path).Get()
75 if (cache_entry is not None) and (version == cache_entry.version): 87 if (cache_entry is not None) and (version == cache_entry.version):
76 return cache_entry._cache_data 88 return cache_entry
77 cache_data = self._populate_function( 89 cache_data = self._populate_function(
78 path, 90 path,
79 self._file_system.ReadSingle(path, binary=binary)) 91 self._file_system.ReadSingle(path, binary=binary))
80 self._file_object_store.Set(path, _CacheEntry(cache_data, version)) 92 cache_entry = _CacheEntry(cache_data, version)
81 return cache_data 93 self._file_object_store.Set(path, cache_entry)
94 return cache_entry
82 95
83 def GetFromFileListing(self, path): 96 def GetFromFileListing(self, path):
84 """Calls |populate_function| on the listing of the files at |path|. 97 """Calls |populate_function| on the listing of the files at |path|.
85 Assumes that the path given is to a directory. 98 Assumes that the path given is to a directory.
86 """ 99 """
87 if not path.endswith('/'): 100 if not path.endswith('/'):
88 path += '/' 101 path += '/'
102 return self._GetCacheEntryFromFileListing(path).cache_data
103
104 def _GetCacheEntryFromFileListing(self, path):
105 assert path.endswith('/')
89 version = self._file_system.Stat(path).version 106 version = self._file_system.Stat(path).version
90 cache_entry = self._list_object_store.Get(path).Get() 107 cache_entry = self._list_object_store.Get(path).Get()
91 if (cache_entry is not None) and (version == cache_entry.version): 108 if (cache_entry is not None) and (version == cache_entry.version):
92 return cache_entry._cache_data 109 return cache_entry
93 cache_data = self._populate_function(path, self._RecursiveList(path)) 110 cache_data = self._populate_function(path, self._RecursiveList(path))
94 self._list_object_store.Set(path, _CacheEntry(cache_data, version)) 111 cache_entry = _CacheEntry(cache_data, version)
95 return cache_data 112 self._list_object_store.Set(path, cache_entry)
113 return cache_entry
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698