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

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

Issue 10704252: Extensions Docs Server: Internal file system (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixed up 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 from io import BytesIO 6 from io import BytesIO
7 import re 7 import re
8 from zipfile import ZipFile 8 from zipfile import ZipFile
9 9
10 class ExampleZipper(object): 10 class ExampleZipper(object):
11 """This class creates a zip file given a samples directory. 11 """This class creates a zip file given a samples directory.
12 """ 12 """
13 def __init__(self, fetcher, cache_builder, base_path, match_path): 13 def __init__(self, file_system, cache_builder, base_path, match_path):
14 self._base_path = base_path 14 self._base_path = base_path
15 self._zip_cache = cache_builder.build(self._MakeZipFile) 15 self._zip_cache = cache_builder.build(self._MakeZipFile)
16 self._fetcher = fetcher 16 self._file_system = file_system
17 self._match_path = match_path 17 self._match_path = match_path
18 18
19 def _MakeZipFile(self, files): 19 def _MakeZipFile(self, files):
20 zip_path = os.path.commonprefix(files).rsplit('/', 1)[-2] 20 zip_path = os.path.commonprefix(files).rsplit('/', 1)[-2]
21 prefix = zip_path.rsplit('/', 1)[-2] 21 prefix = zip_path.rsplit('/', 1)[-2]
22 if zip_path + '/manifest.json' not in files: 22 if zip_path + '/manifest.json' not in files:
23 return None 23 return None
24 zip_bytes = BytesIO() 24 zip_bytes = BytesIO()
25 zip_file = ZipFile(zip_bytes, mode='w') 25 zip_file = ZipFile(zip_bytes, mode='w')
26 try: 26 try:
27 for filename in files: 27 for filename, contents in self._file_system.Read(files).Get().iteritems():
28 zip_file.writestr( 28 zip_file.writestr(filename[len(prefix):].strip('/'), contents)
29 filename[len(prefix):].strip('/'),
30 self._fetcher.FetchResource(filename).content)
31 finally: 29 finally:
32 zip_file.close() 30 zip_file.close()
33 return zip_bytes.getvalue() 31 return zip_bytes.getvalue()
34 32
35 def create(self, path): 33 def Create(self, path):
36 """ Creates a new zip file from the recursive contents of |path| 34 """ Creates a new zip file from the recursive contents of |path|
37 as returned by |_zip_cache|. 35 as returned by |_zip_cache|.
38 Paths within the zip file are given relative to and including |path|. 36 Paths within the zip file are given relative to and including |path|.
39 """ 37 """
40 return self._zip_cache.getFromFileListing(self._base_path + '/' + path, 38 return self._zip_cache.GetFromFileListing(
41 recursive=True) 39 self._base_path + '/' + path)
OLDNEW
« no previous file with comments | « chrome/common/extensions/docs/server2/echo_handler.py ('k') | chrome/common/extensions/docs/server2/fake_url_fetcher.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698