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

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: 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 from file_system import FileNotFoundError
6
5 class _CacheEntry(object): 7 class _CacheEntry(object):
6 def __init__(self, cache_data, version): 8 def __init__(self, cache_data, version):
7 self._cache_data = cache_data 9 self.cache_data = cache_data
8 self.version = version 10 self.version = version
9 11
10 class CompiledFileSystem(object): 12 class CompiledFileSystem(object):
11 """This class caches FileSystem data that has been processed. 13 """This class caches FileSystem data that has been processed.
12 """ 14 """
13 class Factory(object): 15 class Factory(object):
14 """A class to build a CompiledFileSystem backed by |file_system|. 16 """A class to build a CompiledFileSystem backed by |file_system|.
15 """ 17 """
16 def __init__(self, file_system, object_store_creator_factory): 18 def __init__(self,
19 file_system,
20 object_store_creator_factory,
21 patched_file_system=None,
22 patched_object_store_creator_factory=None):
not at google - send to devlin 2013/05/07 05:45:40 this class is conceptually quite simple and now...
方觉(Fang Jue) 2013/05/07 06:03:55 It's definitely not a good way to achieve it. What
not at google - send to devlin 2013/05/07 06:14:31 That sounds interesting, though it seems to me lik
23 assert ((patched_file_system is None and
24 patched_object_store_creator_factory is None) or
25 (patched_file_system is not None and
26 patched_object_store_creator_factory is not None))
17 self._file_system = file_system 27 self._file_system = file_system
18 self._object_store_creator_factory = object_store_creator_factory 28 self._object_store_creator_factory = object_store_creator_factory
29 self._patched_file_system = patched_file_system
30 self._patched_object_store_creator_factory = (
31 patched_object_store_creator_factory)
19 32
20 def Create(self, populate_function, cls, category=None): 33 def Create(self, populate_function, cls, category=None):
21 """Create a CompiledFileSystem that populates the cache by calling 34 """Create a CompiledFileSystem that populates the cache by calling
22 |populate_function| with (path, data), where |data| is the data that was 35 |populate_function| with (path, data), where |data| is the data that was
23 fetched from |path|. 36 fetched from |path|.
24 The namespace for the file system is derived like ObjectStoreCreator: from 37 The namespace for the file system is derived like ObjectStoreCreator: from
25 |cls| along with an optional |category|. 38 |cls| along with an optional |category|.
26 """ 39 """
27 assert isinstance(cls, type) 40 assert isinstance(cls, type)
28 assert not cls.__name__[0].islower() # guard against non-class types 41 assert not cls.__name__[0].islower() # guard against non-class types
29 full_name = cls.__name__ 42 full_name = cls.__name__
30 if category is not None: 43 if category is not None:
31 full_name = '%s/%s' % (full_name, category) 44 full_name = '%s/%s' % (full_name, category)
32 object_store_creator = self._object_store_creator_factory.Create( 45 object_store_creator = self._object_store_creator_factory.Create(
33 CompiledFileSystem) 46 CompiledFileSystem)
34 return CompiledFileSystem( 47 base_compiled_fs = CompiledFileSystem(
35 self._file_system, 48 self._file_system,
36 populate_function, 49 populate_function,
37 object_store_creator.Create(category='%s/file' % full_name), 50 object_store_creator.Create(category='%s/file' % full_name),
38 object_store_creator.Create(category='%s/list' % full_name)) 51 object_store_creator.Create(category='%s/list' % full_name))
52 if self._patched_file_system:
53 patched_object_store_creator = (self.
54 _patched_object_store_creator_factory.Create(CompiledFileSystem))
55 return CompiledFileSystem(
56 self._patched_file_system,
57 populate_function,
58 patched_object_store_creator.Create(category='%s/file' % full_name),
59 patched_object_store_creator.Create(category='%s/list' % full_name),
60 base_compiled_fs)
61 else:
62 return base_compiled_fs
39 63
40 def CreateIdentity(self, cls): 64 def CreateIdentity(self, cls):
41 '''Handy helper to get or create the identity compiled file system. 65 '''Handy helper to get or create the identity compiled file system.
42 GetFromFile will return the file's contents. 66 GetFromFile will return the file's contents.
43 GetFromFileListing will return the directory list. 67 GetFromFileListing will return the directory list.
44 ''' 68 '''
45 return self.Create(lambda _, x: x, cls) 69 return self.Create(lambda _, x: x, cls)
46 70
47 def __init__(self, 71 def __init__(self,
48 file_system, 72 file_system,
49 populate_function, 73 populate_function,
50 file_object_store, 74 file_object_store,
51 list_object_store): 75 list_object_store,
76 base_compiled_fs=None):
52 self._file_system = file_system 77 self._file_system = file_system
53 self._populate_function = populate_function 78 self._populate_function = populate_function
54 self._file_object_store = file_object_store 79 self._file_object_store = file_object_store
55 self._list_object_store = list_object_store 80 self._list_object_store = list_object_store
81 self._base_compiled_fs = base_compiled_fs
56 82
57 def _RecursiveList(self, path): 83 def _RecursiveList(self, path):
58 files = [] 84 files = []
59 for filename in self._file_system.ReadSingle(path): 85 for filename in self._file_system.ReadSingle(path):
60 if filename.endswith('/'): 86 if filename.endswith('/'):
61 files.extend(['%s%s' % (filename, f) 87 files.extend(['%s%s' % (filename, f)
62 for f in self._RecursiveList('%s%s' % (path, filename))]) 88 for f in self._RecursiveList('%s%s' % (path, filename))])
63 else: 89 else:
64 files.append(filename) 90 files.append(filename)
65 return files 91 return files
66 92
67 def GetFromFile(self, path, binary=False): 93 def GetFromFile(self, path, binary=False):
68 """Calls |populate_function| on the contents of the file at |path|. If 94 """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 95 |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| 96 apply for the first time the file is fetched; if already cached, |binary|
71 will be ignored. 97 will be ignored.
72 """ 98 """
99 if self._base_compiled_fs:
100 # It's possible that a new file is added in this compiled file system
101 # and it doesn't exist in base_compiled_fs.
102 try:
103 version = self._file_system.Stat(path).version
104 cache_entry = self._base_compiled_fs._GetCacheEntryFromFile(path,
105 binary)
106 if version == cache_entry.version:
107 return cache_entry.cache_data
108 except FileNotFoundError:
109 pass
110 return self._GetCacheEntryFromFile(path, binary).cache_data
111
112 def _GetCacheEntryFromFile(self, path, binary=False):
73 version = self._file_system.Stat(path).version 113 version = self._file_system.Stat(path).version
74 cache_entry = self._file_object_store.Get(path).Get() 114 cache_entry = self._file_object_store.Get(path).Get()
75 if (cache_entry is not None) and (version == cache_entry.version): 115 if (cache_entry is not None) and (version == cache_entry.version):
76 return cache_entry._cache_data 116 return cache_entry
77 cache_data = self._populate_function( 117 cache_data = self._populate_function(
78 path, 118 path,
79 self._file_system.ReadSingle(path, binary=binary)) 119 self._file_system.ReadSingle(path, binary=binary))
80 self._file_object_store.Set(path, _CacheEntry(cache_data, version)) 120 cache_entry = _CacheEntry(cache_data, version)
81 return cache_data 121 self._file_object_store.Set(path, cache_entry)
122 return cache_entry
82 123
83 def GetFromFileListing(self, path): 124 def GetFromFileListing(self, path):
84 """Calls |populate_function| on the listing of the files at |path|. 125 """Calls |populate_function| on the listing of the files at |path|.
85 Assumes that the path given is to a directory. 126 Assumes that the path given is to a directory.
86 """ 127 """
87 if not path.endswith('/'): 128 if not path.endswith('/'):
88 path += '/' 129 path += '/'
130 if self._base_compiled_fs:
131 try:
132 version = self._file_system.Stat(path).version
133 cache_entry = self._base_compiled_fs._GetCacheEntryFromFileListing(
134 path)
135 if version == cache_entry.version:
136 return cache_entry.cache_data
137 except FileNotFoundError:
138 pass
139 return self._GetCacheEntryFromFileListing(path).cache_data
140
141 def _GetCacheEntryFromFileListing(self, path):
142 assert path.endswith('/')
89 version = self._file_system.Stat(path).version 143 version = self._file_system.Stat(path).version
90 cache_entry = self._list_object_store.Get(path).Get() 144 cache_entry = self._list_object_store.Get(path).Get()
91 if (cache_entry is not None) and (version == cache_entry.version): 145 if (cache_entry is not None) and (version == cache_entry.version):
92 return cache_entry._cache_data 146 return cache_entry
93 cache_data = self._populate_function(path, self._RecursiveList(path)) 147 cache_data = self._populate_function(path, self._RecursiveList(path))
94 self._list_object_store.Set(path, _CacheEntry(cache_data, version)) 148 cache_entry = _CacheEntry(cache_data, version)
95 return cache_data 149 self._list_object_store.Set(path, cache_entry)
150 return cache_entry
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698