Chromium Code Reviews| 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 def _MakeSubversionDir(path): | |
| 8 """This function is necessary to make the output of a LocalFetcher match what | |
| 9 a SubversionFetcher would give. | |
|
not at google - send to devlin
2012/07/11 00:35:09
I think this will be unnecessary if listing files
cduvall
2012/07/11 20:56:30
Done.
| |
| 10 """ | |
| 11 page = '<title>Local ' + os.path.join(*path.split('/')) + '</title>' | |
| 12 for filename in os.listdir(path): | |
| 13 if os.path.isdir(os.path.join(path, filename)): | |
| 14 filename += '/' | |
| 15 page += '\n<a>' + filename + '</a>' | |
| 16 return page | |
| 17 | |
| 7 class LocalFetcher(object): | 18 class LocalFetcher(object): |
| 8 """Class to fetch resources from local filesystem. | 19 """Class to fetch resources from local filesystem. |
| 9 """ | 20 """ |
| 10 def __init__(self, base_path): | 21 def __init__(self, base_path): |
| 11 self._base_path = self._ConvertToFilepath(base_path) | 22 self._base_path = self._ConvertToFilepath(base_path) |
| 12 | 23 |
| 13 def _ConvertToFilepath(self, path): | 24 def _ConvertToFilepath(self, path): |
| 14 return path.replace('/', os.sep) | 25 return path.replace('/', os.sep) |
| 15 | 26 |
| 16 class _Response(object): | 27 class _Response(object): |
| 17 """Response object matching what is returned from urlfetch. | 28 """Response object matching what is returned from urlfetch. |
| 18 """ | 29 """ |
| 19 def __init__(self, content): | 30 def __init__(self, content): |
| 20 self.content = content | 31 self.content = content |
| 21 self.headers = {} | 32 self.headers = {} |
| 22 | 33 |
| 23 def _ReadFile(self, filename): | 34 def _ReadFile(self, filename): |
| 24 path = os.path.join(self._base_path, filename) | 35 path = os.path.join(self._base_path, filename) |
| 36 if os.path.isdir(path): | |
| 37 return _MakeSubversionDir(path) | |
| 25 with open(path, 'r') as f: | 38 with open(path, 'r') as f: |
| 26 return f.read() | 39 return f.read() |
| 27 | 40 |
| 28 def FetchResource(self, path): | 41 def FetchResource(self, path): |
| 29 # A response object is returned to match the behavior of urlfetch. | 42 # A response object is returned to match the behavior of urlfetch. |
| 30 # See: developers.google.com/appengine/docs/python/urlfetch/responseobjects | 43 # See: developers.google.com/appengine/docs/python/urlfetch/responseobjects |
| 31 return self._Response(self._ReadFile(self._ConvertToFilepath(path))) | 44 return self._Response(self._ReadFile(self._ConvertToFilepath(path))) |
| OLD | NEW |