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 with open(os.path.join(self._base_path, filename), 'r') as f: | 21 try: |
22 contents = f.read() | 22 with open(os.path.join(self._base_path, filename), 'r') as f: |
23 if binary: | 23 contents = f.read() |
24 return contents | 24 if binary: |
25 return file_system._ProcessFileData(contents, filename) | 25 return contents |
26 return file_system._ProcessFileData(contents, filename) | |
27 except IOError: | |
not at google - send to devlin
2012/08/10 06:12:16
why IOError here and OSError other times? Python q
cduvall
2012/08/10 17:25:16
Yep, os.listdir throws OSError, but open throws IO
| |
28 raise file_system.FileNotFoundError(filename) | |
26 | 29 |
27 def _ListDir(self, dir_name): | 30 def _ListDir(self, dir_name): |
28 all_files = [] | 31 all_files = [] |
29 full_path = os.path.join(self._base_path, dir_name) | 32 full_path = os.path.join(self._base_path, dir_name) |
30 for path in os.listdir(full_path): | 33 try: |
34 files = os.listdir(full_path) | |
35 except OSError: | |
36 raise file_system.FileNotFoundError(dir_name) | |
37 for path in files: | |
31 if path.startswith('.'): | 38 if path.startswith('.'): |
32 continue | 39 continue |
33 if os.path.isdir(os.path.join(full_path, path)): | 40 if os.path.isdir(os.path.join(full_path, path)): |
34 all_files.append(path + '/') | 41 all_files.append(path + '/') |
35 else: | 42 else: |
36 all_files.append(path) | 43 all_files.append(path) |
37 return all_files | 44 return all_files |
38 | 45 |
39 def Read(self, paths, binary=False): | 46 def Read(self, paths, binary=False): |
40 result = {} | 47 result = {} |
41 for path in paths: | 48 for path in paths: |
42 if path.endswith('/'): | 49 if path.endswith('/'): |
43 result[path] = self._ListDir(self._ConvertToFilepath(path)) | 50 result[path] = self._ListDir(self._ConvertToFilepath(path)) |
44 else: | 51 else: |
45 result[path] = self._ReadFile(self._ConvertToFilepath(path), binary) | 52 result[path] = self._ReadFile(self._ConvertToFilepath(path), binary) |
46 return Future(value=result) | 53 return Future(value=result) |
47 | 54 |
48 def Stat(self, path): | 55 def Stat(self, path): |
49 return self.StatInfo(os.stat(os.path.join(self._base_path, path)).st_mtime) | 56 try: |
57 return self.StatInfo( | |
58 os.stat(os.path.join(self._base_path, path)).st_mtime) | |
59 except OSError: | |
60 raise file_system.FileNotFoundError(path) | |
61 | |
OLD | NEW |