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 import file_system | 7 import file_system |
8 from future import Future | 8 from future import Future |
9 | 9 |
10 class LocalFileSystem(file_system.FileSystem): | 10 class LocalFileSystem(file_system.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): |
15 self._base_path = self._ConvertToFilepath(base_path) | 15 self._base_path = self._ConvertToFilepath(base_path) |
16 | 16 |
17 def _ConvertToFilepath(self, path): | 17 def _ConvertToFilepath(self, path): |
18 return path.replace('/', os.sep) | 18 return path.replace('/', os.sep) |
19 | 19 |
20 def _ReadFile(self, filename, binary): | 20 def _ReadFile(self, filename, binary): |
21 try: | 21 try: |
22 mode = 'rb' if binary else 'r' | 22 mode = 'rb' if binary else 'r' |
23 with open(os.path.join(self._base_path, filename), mode) as f: | 23 with open(os.path.join(self._base_path, filename), mode) as f: |
24 contents = f.read() | 24 contents = f.read() |
25 if binary: | 25 if binary: |
26 return contents | 26 return contents |
27 return file_system._ProcessFileData(contents, filename) | 27 return file_system._ToUnicode(contents) |
28 except IOError: | 28 except IOError: |
29 raise file_system.FileNotFoundError(filename) | 29 raise file_system.FileNotFoundError(filename) |
30 | 30 |
31 def _ListDir(self, dir_name): | 31 def _ListDir(self, dir_name): |
32 all_files = [] | 32 all_files = [] |
33 full_path = os.path.join(self._base_path, dir_name) | 33 full_path = os.path.join(self._base_path, dir_name) |
34 try: | 34 try: |
35 files = os.listdir(full_path) | 35 files = os.listdir(full_path) |
36 except OSError: | 36 except OSError: |
37 raise file_system.FileNotFoundError(dir_name) | 37 raise file_system.FileNotFoundError(dir_name) |
(...skipping 21 matching lines...) Expand all Loading... |
59 for filename in os.listdir(path)) | 59 for filename in os.listdir(path)) |
60 else: | 60 else: |
61 versions = None | 61 versions = None |
62 return file_system.StatInfo(os.stat(path).st_mtime, versions) | 62 return file_system.StatInfo(os.stat(path).st_mtime, versions) |
63 | 63 |
64 def Stat(self, path): | 64 def Stat(self, path): |
65 try: | 65 try: |
66 return self._CreateStatInfo(os.path.join(self._base_path, path)) | 66 return self._CreateStatInfo(os.path.join(self._base_path, path)) |
67 except OSError: | 67 except OSError: |
68 raise file_system.FileNotFoundError(path) | 68 raise file_system.FileNotFoundError(path) |
OLD | NEW |