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 from future import Future | 7 from future import Future |
| 8 | 8 |
| 9 class _Response(object): | 9 class _Response(object): |
| 10 def __init__(self): | 10 def __init__(self): |
| 11 self.content = '' | 11 self.content = '' |
| 12 self.headers = { 'content-type': 'none' } | 12 self.headers = { 'content-type': 'none' } |
| 13 self.status_code = 200 | 13 self.status_code = 200 |
| 14 | 14 |
| 15 class FakeUrlFetcher(object): | 15 class FakeUrlFetcher(object): |
| 16 def __init__(self, base_path): | 16 def __init__(self, base_path): |
| 17 self._base_path = base_path | 17 self._base_path = base_path |
| 18 | 18 |
| 19 def _ReadFile(self, filename): | 19 def _ReadFile(self, filename): |
| 20 with open(os.path.join(self._base_path, filename), 'r') as f: | 20 with open(os.path.join(self._base_path, filename), 'r') as f: |
| 21 return f.read() | 21 return f.read() |
| 22 | 22 |
| 23 def _ListDir(self, directory): | 23 def _ListDir(self, directory): |
| 24 if not os.path.isdir(os.path.join(self._base_path, directory)): | |
| 25 return self._ReadFile(directory[:-1]) | |
|
not at google - send to devlin
2012/08/13 05:34:15
How can _ListDir be passed something that isn't a
cduvall
2012/08/13 19:45:45
This is just for testing. The file has the content
| |
| 24 files = os.listdir(os.path.join(self._base_path, directory)) | 26 files = os.listdir(os.path.join(self._base_path, directory)) |
| 25 html = '<html><title>Revision: 00000</title>\n' | 27 html = '<html><title>Revision: 00000</title>\n' |
| 26 for filename in files: | 28 for filename in files: |
| 27 if filename.startswith('.'): | 29 if filename.startswith('.'): |
| 28 continue | 30 continue |
| 29 if os.path.isdir(os.path.join(self._base_path, directory, filename)): | 31 if os.path.isdir(os.path.join(self._base_path, directory, filename)): |
| 30 html += '<a>' + filename + '/</a>\n' | 32 html += '<a>' + filename + '/</a>\n' |
| 31 else: | 33 else: |
| 32 html += '<a>' + filename + '</a>\n' | 34 html += '<a>' + filename + '</a>\n' |
| 33 html += '</html>' | 35 html += '</html>' |
| 34 return html | 36 return html |
| 35 | 37 |
| 36 def FetchAsync(self, url): | 38 def FetchAsync(self, url): |
| 37 return Future(value=self.Fetch(url)) | 39 return Future(value=self.Fetch(url)) |
| 38 | 40 |
| 39 def Fetch(self, url): | 41 def Fetch(self, url): |
| 40 result = _Response() | 42 result = _Response() |
| 41 if url.endswith('/'): | 43 if url.endswith('/'): |
| 42 result.content = self._ListDir(url) | 44 result.content = self._ListDir(url) |
| 43 else: | 45 else: |
| 44 result.content = self._ReadFile(url) | 46 result.content = self._ReadFile(url) |
| 45 return result | 47 return result |
| OLD | NEW |