Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: chrome/common/extensions/docs/server2/local_fetcher.py

Issue 10689144: Extensions Docs Server: Samples zip files (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: better sample page Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 class LocalFetcher(object): 7 class LocalFetcher(object):
8 """Class to fetch resources from local filesystem. 8 """Class to fetch resources from local filesystem.
9 """ 9 """
10 def __init__(self, base_path): 10 def __init__(self, base_path):
11 self._base_path = self._ConvertToFilepath(base_path) 11 self._base_path = self._ConvertToFilepath(base_path)
12 12
13 def _ConvertToFilepath(self, path): 13 def _ConvertToFilepath(self, path):
14 return path.replace('/', os.sep) 14 return path.replace('/', os.sep)
15 15
16 class _Response(object): 16 class _Response(object):
17 """Response object matching what is returned from urlfetch. 17 """Response object matching what is returned from urlfetch.
18 """ 18 """
19 def __init__(self, content): 19 def __init__(self, content):
20 self.content = content 20 self.content = content
21 self.headers = {} 21 self.headers = {}
22 22
23 def _ReadFile(self, filename): 23 def _ReadFile(self, filename):
24 path = os.path.join(self._base_path, filename) 24 path = os.path.join(self._base_path, filename)
25 with open(path, 'r') as f: 25 with open(path, 'r') as f:
26 return f.read() 26 return f.read()
27 27
28 def FetchDirectory(self, directory):
not at google - send to devlin 2012/07/12 00:22:48 Could you make recursive an optional parameter, de
cduvall 2012/07/12 01:51:23 Done.
29 all_files = []
30 for path, subdirs, files in os.walk(
31 os.path.join(self._base_path, self._ConvertToFilepath(directory))):
32 for filename in files:
33 full_path = os.path.join(path, filename)
34 if os.path.isdir(full_path):
35 all_files.append(full_path + '/')
36 else:
37 all_files.append(full_path)
38 # Remove |_base_path| from files before returning.
not at google - send to devlin 2012/07/12 00:22:48 This is the kind of contract that should be docume
cduvall 2012/07/12 01:51:23 Done.
39 return self._Response(
40 map(lambda x: x.replace(self._base_path + os.sep, ''), all_files))
41
28 def FetchResource(self, path): 42 def FetchResource(self, path):
29 # A response object is returned to match the behavior of urlfetch. 43 # A response object is returned to match the behavior of urlfetch.
30 # See: developers.google.com/appengine/docs/python/urlfetch/responseobjects 44 # See: developers.google.com/appengine/docs/python/urlfetch/responseobjects
31 return self._Response(self._ReadFile(self._ConvertToFilepath(path))) 45 return self._Response(self._ReadFile(self._ConvertToFilepath(path)))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698