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): |
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): | 20 def _ReadFile(self, filename): |
21 with open(os.path.join(self._base_path, filename), 'r') as f: | 21 with open(os.path.join(self._base_path, filename), 'r') as f: |
22 return f.read() | 22 data = f.read() |
| 23 if os.path.splitext(filename)[-1] in ['.js', '.html', '.json']: |
| 24 try: |
| 25 return unicode(data, 'utf-8') |
| 26 except: |
| 27 return unicode(data, 'latin-1') |
| 28 return data |
23 | 29 |
24 def _ListDir(self, dir_name): | 30 def _ListDir(self, dir_name): |
25 all_files = [] | 31 all_files = [] |
26 full_path = os.path.join(self._base_path, dir_name) | 32 full_path = os.path.join(self._base_path, dir_name) |
27 for path in os.listdir(full_path): | 33 for path in os.listdir(full_path): |
28 if path.startswith('.'): | 34 if path.startswith('.'): |
29 continue | 35 continue |
30 if os.path.isdir(os.path.join(full_path, path)): | 36 if os.path.isdir(os.path.join(full_path, path)): |
31 all_files.append(path + '/') | 37 all_files.append(path + '/') |
32 else: | 38 else: |
33 all_files.append(path) | 39 all_files.append(path) |
34 return all_files | 40 return all_files |
35 | 41 |
36 def Read(self, paths): | 42 def Read(self, paths): |
37 result = {} | 43 result = {} |
38 for path in paths: | 44 for path in paths: |
39 if path.endswith('/'): | 45 if path.endswith('/'): |
40 result[path] = self._ListDir(self._ConvertToFilepath(path)) | 46 result[path] = self._ListDir(self._ConvertToFilepath(path)) |
41 else: | 47 else: |
42 result[path] = self._ReadFile(self._ConvertToFilepath(path)) | 48 result[path] = self._ReadFile(self._ConvertToFilepath(path)) |
43 return Future(value=result) | 49 return Future(value=result) |
44 | 50 |
45 def Stat(self, path): | 51 def Stat(self, path): |
46 return self.StatInfo(os.stat(os.path.join(self._base_path, path)).st_mtime) | 52 return self.StatInfo(os.stat(os.path.join(self._base_path, path)).st_mtime) |
OLD | NEW |