OLD | NEW |
---|---|
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 import os | 5 import os |
6 | 6 |
7 from file_system import FileSystem | 7 from file_system import FileSystem |
8 from future import Future | 8 from future import Future |
9 | 9 |
10 class LocalFileSystem(FileSystem): | 10 class LocalFileSystem(FileSystem): |
11 """FileSystem implementation which fetches resources from the local | 11 """FileSystem implementation which fetches resources from the local |
12 filesystem. | 12 filesystem. |
13 """ | 13 """ |
14 def __init__(self, base_path): | 14 def __init__(self, base_path, inc_version=True): |
15 self._base_path = self._ConvertToFilepath(base_path) | 15 self._base_path = self._ConvertToFilepath(base_path) |
16 self._inc_version = inc_version | |
17 self._version = 0 | |
16 | 18 |
17 def _ConvertToFilepath(self, path): | 19 def _ConvertToFilepath(self, path): |
18 return path.replace('/', os.sep) | 20 return path.replace('/', os.sep) |
19 | 21 |
20 def _ReadFile(self, filename): | 22 def _ReadFile(self, filename): |
21 with open(os.path.join(self._base_path, filename), 'r') as f: | 23 with open(os.path.join(self._base_path, filename), 'r') as f: |
22 return f.read() | 24 return f.read() |
23 | 25 |
24 def _ListDir(self, dir_name): | 26 def _ListDir(self, dir_name): |
25 all_files = [] | 27 all_files = [] |
26 full_path = os.path.join(self._base_path, dir_name) | 28 full_path = os.path.join(self._base_path, dir_name) |
27 for path in os.listdir(full_path): | 29 for path in os.listdir(full_path): |
28 if os.path.isdir(os.path.join(full_path, path)): | 30 if os.path.isdir(os.path.join(full_path, path)): |
29 all_files.append(path + '/') | 31 all_files.append(path + '/') |
30 else: | 32 else: |
31 all_files.append(path) | 33 all_files.append(path) |
32 return all_files | 34 return all_files |
33 | 35 |
34 def Read(self, paths): | 36 def Read(self, paths): |
35 result = {} | 37 result = {} |
36 for path in paths: | 38 for path in paths: |
37 if path.endswith('/'): | 39 if path.endswith('/'): |
38 result[path] = self._ListDir(self._ConvertToFilepath(path)) | 40 result[path] = self._ListDir(self._ConvertToFilepath(path)) |
39 else: | 41 else: |
40 result[path] = self._ReadFile(self._ConvertToFilepath(path)) | 42 result[path] = self._ReadFile(self._ConvertToFilepath(path)) |
41 return Future(value=result) | 43 return Future(value=result) |
42 | 44 |
43 def Stat(self, path): | 45 def Stat(self, path): |
44 return self.StatInfo(0) | 46 if self._inc_version: |
47 self._version += 1 | |
48 return self.StatInfo(self._version) | |
not at google - send to devlin
2012/07/23 13:13:27
I don't understand this inc_version stuff. This is
cduvall
2012/07/23 19:04:41
Done.
| |
OLD | NEW |