Commit 64ff751b authored by cduvall@chromium.org's avatar cduvall@chromium.org

Extensions Docs Server: Fix zipper

The ExampleZipper was getting caught on unicode characters. I added a parameter
to Read in FileSystem that will return the files without converting to unicode.

BUG=131095

Review URL: https://chromiumcodereview.appspot.com/10826037

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@148980 0039d316-1c4b-4281-b951-d872f2087c98
parent 274bb5fb
...@@ -24,8 +24,9 @@ class ExampleZipper(object): ...@@ -24,8 +24,9 @@ class ExampleZipper(object):
zip_bytes = BytesIO() zip_bytes = BytesIO()
zip_file = ZipFile(zip_bytes, mode='w') zip_file = ZipFile(zip_bytes, mode='w')
try: try:
for filename, contents in self._file_system.Read(files).Get().iteritems(): for name, file_contents in (
zip_file.writestr(filename[len(prefix):].strip('/'), contents) self._file_system.Read(files, binary=True).Get().iteritems()):
zip_file.writestr(name[len(prefix):].strip('/'), file_contents)
finally: finally:
zip_file.close() zip_file.close()
return zip_bytes.getvalue() return zip_bytes.getvalue()
......
...@@ -21,10 +21,14 @@ class FileSystem(object): ...@@ -21,10 +21,14 @@ class FileSystem(object):
def __init__(self, version): def __init__(self, version):
self.version = version self.version = version
def Read(self, paths): def Read(self, paths, binary=False):
"""Reads each file in paths and returns a dictionary mapping the path to the """Reads each file in paths and returns a dictionary mapping the path to the
contents. If a path in paths ends with a '/', it is assumed to be a contents. If a path in paths ends with a '/', it is assumed to be a
directory, and a list of files in the directory is mapped to the path. directory, and a list of files in the directory is mapped to the path.
If binary=False, the contents of each file will be unicode parsed as utf-8,
and failing that as latin-1 (some extension docs use latin-1). If
binary=True then the contents will be a str.
""" """
raise NotImplementedError() raise NotImplementedError()
......
...@@ -17,9 +17,12 @@ class LocalFileSystem(file_system.FileSystem): ...@@ -17,9 +17,12 @@ class LocalFileSystem(file_system.FileSystem):
def _ConvertToFilepath(self, path): def _ConvertToFilepath(self, path):
return path.replace('/', os.sep) return path.replace('/', os.sep)
def _ReadFile(self, filename): def _ReadFile(self, filename, binary):
with open(os.path.join(self._base_path, filename), 'r') as f: with open(os.path.join(self._base_path, filename), 'r') as f:
return file_system._ProcessFileData(f.read(), filename) contents = f.read()
if binary:
return contents
return file_system._ProcessFileData(contents, filename)
def _ListDir(self, dir_name): def _ListDir(self, dir_name):
all_files = [] all_files = []
...@@ -33,13 +36,13 @@ class LocalFileSystem(file_system.FileSystem): ...@@ -33,13 +36,13 @@ class LocalFileSystem(file_system.FileSystem):
all_files.append(path) all_files.append(path)
return all_files return all_files
def Read(self, paths): def Read(self, paths, binary=False):
result = {} result = {}
for path in paths: for path in paths:
if path.endswith('/'): if path.endswith('/'):
result[path] = self._ListDir(self._ConvertToFilepath(path)) result[path] = self._ListDir(self._ConvertToFilepath(path))
else: else:
result[path] = self._ReadFile(self._ConvertToFilepath(path)) result[path] = self._ReadFile(self._ConvertToFilepath(path), binary)
return Future(value=result) return Future(value=result)
def Stat(self, path): def Stat(self, path):
......
...@@ -27,7 +27,7 @@ class MemcacheFileSystem(FileSystem): ...@@ -27,7 +27,7 @@ class MemcacheFileSystem(FileSystem):
stat_info = self.StatInfo(version) stat_info = self.StatInfo(version)
return stat_info return stat_info
def Read(self, paths): def Read(self, paths, binary=False):
"""Reads a list of files. If a file is in memcache and it is not out of """Reads a list of files. If a file is in memcache and it is not out of
date, it is returned. Otherwise, the file is retrieved from the file system. date, it is returned. Otherwise, the file is retrieved from the file system.
""" """
...@@ -45,7 +45,7 @@ class MemcacheFileSystem(FileSystem): ...@@ -45,7 +45,7 @@ class MemcacheFileSystem(FileSystem):
uncached.append(path) uncached.append(path)
continue continue
result[path] = data result[path] = data
new_items = self._file_system.Read(uncached).Get() new_items = self._file_system.Read(uncached, binary=binary).Get()
for item in new_items: for item in new_items:
version = self.Stat(item).version version = self.Stat(item).version
value = new_items[item] value = new_items[item]
......
...@@ -14,8 +14,8 @@ class SubversionFileSystem(file_system.FileSystem): ...@@ -14,8 +14,8 @@ class SubversionFileSystem(file_system.FileSystem):
def __init__(self, fetcher): def __init__(self, fetcher):
self._fetcher = fetcher self._fetcher = fetcher
def Read(self, paths): def Read(self, paths, binary=False):
return Future(delegate=_AsyncFetchFuture(paths, self._fetcher)) return Future(delegate=_AsyncFetchFuture(paths, self._fetcher, binary))
def Stat(self, path): def Stat(self, path):
directory = path.rsplit('/', 1)[0] directory = path.rsplit('/', 1)[0]
...@@ -23,12 +23,13 @@ class SubversionFileSystem(file_system.FileSystem): ...@@ -23,12 +23,13 @@ class SubversionFileSystem(file_system.FileSystem):
return self.StatInfo(int(re.search('([0-9]+)', dir_html).group(0))) return self.StatInfo(int(re.search('([0-9]+)', dir_html).group(0)))
class _AsyncFetchFuture(object): class _AsyncFetchFuture(object):
def __init__(self, paths, fetcher): def __init__(self, paths, fetcher, binary):
# A list of tuples of the form (path, Future). # A list of tuples of the form (path, Future).
self._fetches = [] self._fetches = []
self._value = {} self._value = {}
self._error = None self._error = None
self._fetches = [(path, fetcher.FetchAsync(path)) for path in paths] self._fetches = [(path, fetcher.FetchAsync(path)) for path in paths]
self._binary = binary
def _ListDir(self, directory): def _ListDir(self, directory):
dom = xml.parseString(directory) dom = xml.parseString(directory)
...@@ -44,8 +45,10 @@ class _AsyncFetchFuture(object): ...@@ -44,8 +45,10 @@ class _AsyncFetchFuture(object):
self._value[path] = None self._value[path] = None
elif path.endswith('/'): elif path.endswith('/'):
self._value[path] = self._ListDir(result.content) self._value[path] = self._ListDir(result.content)
else: elif not self._binary:
self._value[path] = file_system._ProcessFileData(result.content, path) self._value[path] = file_system._ProcessFileData(result.content, path)
else:
self._value[path] = result.content
if self._error is not None: if self._error is not None:
raise self._error raise self._error
return self._value return self._value
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment