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 import sys |
6 | 7 |
7 from file_system import FileSystem, FileNotFoundError, StatInfo, ToUnicode | 8 from file_system import FileSystem, FileNotFoundError, StatInfo, ToUnicode |
8 from future import Future | 9 from future import Future |
9 | 10 |
10 class LocalFileSystem(FileSystem): | 11 class LocalFileSystem(FileSystem): |
11 """FileSystem implementation which fetches resources from the local | 12 '''FileSystem implementation which fetches resources from the local |
12 filesystem. | 13 filesystem. |
13 """ | 14 ''' |
14 def __init__(self, base_path): | 15 def __init__(self, base_path=None): |
| 16 if base_path is None: |
| 17 # Default to the base of the extensions directory. |
| 18 base_path = os.path.join(sys.path[0], os.pardir, os.pardir) |
15 self._base_path = self._ConvertToFilepath(base_path) | 19 self._base_path = self._ConvertToFilepath(base_path) |
16 | 20 |
17 def _ConvertToFilepath(self, path): | 21 def _ConvertToFilepath(self, path): |
18 return path.replace('/', os.sep) | 22 return path.replace('/', os.sep) |
19 | 23 |
20 def _ReadFile(self, filename, binary): | 24 def _ReadFile(self, filename, binary): |
21 try: | 25 try: |
22 mode = 'rb' if binary else 'r' | 26 mode = 'rb' if binary else 'r' |
23 with open(os.path.join(self._base_path, filename), mode) as f: | 27 with open(os.path.join(self._base_path, filename), mode) as f: |
24 contents = f.read() | 28 contents = f.read() |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 for filename in os.listdir(path)) | 63 for filename in os.listdir(path)) |
60 else: | 64 else: |
61 versions = None | 65 versions = None |
62 try: | 66 try: |
63 return StatInfo(os.stat(path).st_mtime, versions) | 67 return StatInfo(os.stat(path).st_mtime, versions) |
64 except OSError as e: | 68 except OSError as e: |
65 raise FileNotFoundError('os.stat failed for %s: %s' % (path, e)) | 69 raise FileNotFoundError('os.stat failed for %s: %s' % (path, e)) |
66 | 70 |
67 def Stat(self, path): | 71 def Stat(self, path): |
68 return self._CreateStatInfo(os.path.join(self._base_path, path)) | 72 return self._CreateStatInfo(os.path.join(self._base_path, path)) |
OLD | NEW |