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

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

Issue 10704252: Extensions Docs Server: Internal file system (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixed up Created 8 years, 5 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
(Empty)
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.
4
5 import os
6
7 class FileSystemCache(object):
8 """This class caches FileSystem data that has been processed.
9 """
10 class Builder(object):
11 """A class to build a FileSystemCache.
12 """
13 def __init__(self, file_system):
14 self._file_system = file_system
15
16 def build(self, populate_function):
17 return FileSystemCache(self._file_system, populate_function)
18
19 class _CacheEntry(object):
20 def __init__(self, cache_data, version):
21 self._cache_data = cache_data
22 self.version = version
23
24 def __init__(self, file_system, populate_function):
25 self._file_system = file_system
26 self._populate_function = populate_function
27 self._cache = {}
28
29 def _FetchFile(self, filename):
30 return self._file_system.Read([filename]).Get()[filename]
31
32 def _RecursiveList(self, files):
33 all_files = files[:]
34 dirs = {}
35 for filename in files:
36 if filename.endswith('/'):
37 all_files.remove(filename)
38 dirs.update(self._file_system.Read([filename]).Get())
39 for dir_, files in dirs.iteritems():
40 all_files.extend(self._RecursiveList([dir_ + f for f in files]))
41 return all_files
42
43 def GetFromFile(self, path):
44 """Calls |populate_function| on the contents of the file at |path|.
45 """
46 version = self._file_system.Stat(path).version
47 if path in self._cache:
48 if version > self._cache[path].version:
49 self._cache.pop(path)
50 else:
51 return self._cache[path]._cache_data
52 cache_data = self._FetchFile(path)
53 self._cache[path] = self._CacheEntry(self._populate_function(cache_data),
54 version)
55 return self._cache[path]._cache_data
56
57 def GetFromFileListing(self, path):
58 """Calls |populate_function| on the listing of the files at |path|.
59 Assumes that the path given is to a directory.
60 """
61 if not path.endswith('/'):
62 path += '/'
63 version = self._file_system.Stat(path).version
64 if path in self._cache:
65 if version > self._cache[path].version:
66 self._cache.pop(path)
67 else:
68 return self._cache[path]._cache_data
69 cache_data = self._RecursiveList([path + f for f in self._FetchFile(path)])
70 self._cache[path] = self._CacheEntry(self._populate_function(cache_data),
71 version)
72 return self._cache[path]._cache_data
OLDNEW
« no previous file with comments | « chrome/common/extensions/docs/server2/file_system.py ('k') | chrome/common/extensions/docs/server2/future.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698