| 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 logging | 5 import logging |
| 6 import os | 6 import os |
| 7 | 7 |
| 8 class LocalFetcher(object): | 8 class LocalFetcher(object): |
| 9 """Class to fetch resources from local filesystem. | 9 """Class to fetch resources from local filesystem. |
| 10 """ | 10 """ |
| 11 def __init__(self, base_path): | 11 def __init__(self, base_path): |
| 12 self.base_path = base_path | 12 self._base_path = self._ConvertToFilepath(base_path) |
| 13 |
| 14 def _ConvertToFilepath(self, path): |
| 15 return path.replace('/', os.sep) |
| 13 | 16 |
| 14 class _Resource(object): | 17 class _Resource(object): |
| 15 def __init__(self): | 18 def __init__(self, content): |
| 16 self.content = '' | 19 self.content = content |
| 17 self.headers = {} | 20 self.headers = {} |
| 18 | 21 |
| 19 def _ReadFile(self, filename): | 22 def _ReadFile(self, filename): |
| 20 with open(filename, 'r') as f: | 23 path = os.path.join(self._base_path, filename) |
| 24 logging.info('Reading: ' + path) |
| 25 with open(path, 'r') as f: |
| 21 return f.read() | 26 return f.read() |
| 22 | 27 |
| 23 def FetchResource(self, branch, path): | 28 def FetchResource(self, path): |
| 24 real_path = os.path.join(*path.split('/')) | 29 return self._Resource(self._ReadFile(self._ConvertToFilepath(path))) |
| 25 result = self._Resource() | |
| 26 logging.info('Reading: ' + os.path.join(self.base_path, real_path)) | |
| 27 result.content = self._ReadFile(os.path.join(self.base_path, real_path)) | |
| 28 return result | |
| OLD | NEW |