Commit 9b2ae111 by Timothée Peignier

pass storage as argument

parent 0122689a
......@@ -10,7 +10,8 @@ from pipeline.utils import to_class
class Compiler(object):
def __init__(self, verbose=False):
def __init__(self, storage=default_storage, verbose=False):
self.storage = storage
self.verbose = verbose
def compilers(self):
......@@ -25,10 +26,10 @@ class Compiler(object):
new_path = self.output_path(path, compiler.output_extension)
content = self.read_file(path)
try:
compiled_content = compiler.compile_file(content, default_storage.path(path))
compiled_content = compiler.compile_file(content, self.storage.path(path))
self.save_file(new_path, compiled_content)
except CompilerError:
if not default_storage.exists(new_path) or not settings.PIPELINE:
if not self.storage.exists(new_path) or not settings.PIPELINE:
raise
paths[index] = new_path
return paths
......@@ -38,13 +39,13 @@ class Compiler(object):
return '.'.join((path[0], extension))
def read_file(self, path):
file = default_storage.open(path, 'rb')
file = self.storage.open(path, 'rb')
content = file.read()
file.close()
return content
def save_file(self, path, content):
return default_storage.save(path, ContentFile(smart_str(content)))
return self.storage.save(path, ContentFile(smart_str(content)))
class CompilerBase(object):
......@@ -57,9 +58,6 @@ class CompilerBase(object):
def compile_file(self, content, path):
raise NotImplementedError
def save_file(self, path, content):
return default_storage.save(path, ContentFile(smart_str(content)))
class CompilerError(Exception):
pass
......
......@@ -6,8 +6,8 @@ import subprocess
from itertools import takewhile
from pipeline.conf import settings
from pipeline.storage import default_storage
from pipeline.utils import to_class, relpath
from pipeline.storage import default_storage
MAX_IMAGE_SIZE = 32700
......@@ -40,7 +40,8 @@ FONT_EXTS = ['.ttf', '.otf', '.woff']
class Compressor(object):
asset_contents = {}
def __init__(self, verbose=False):
def __init__(self, storage=default_storage, verbose=False):
self.storage = storage
self.verbose = verbose
def js_compressor(self):
......@@ -159,7 +160,7 @@ class Compressor(object):
font = ext in FONT_EXTS
if not variant:
return False
if not (re.search(EMBEDDABLE, path) and default_storage.exists(path)):
if not (re.search(EMBEDDABLE, path) and self.storage.exists(path)):
return False
if not ext in EMBED_EXTS:
return False
......
......@@ -61,10 +61,11 @@ class Package(object):
class Packager(object):
def __init__(self, verbose=False, css_packages=None, js_packages=None):
def __init__(self, storage=default_storage, verbose=False, css_packages=None, js_packages=None):
self.storage = storage
self.verbose = verbose
self.compressor = Compressor(verbose)
self.compiler = Compiler(verbose)
self.compressor = Compressor(storage=storage, verbose=verbose)
self.compiler = Compiler(storage=storage, verbose=verbose)
if css_packages is None:
css_packages = settings.PIPELINE_CSS
if js_packages is None:
......@@ -85,7 +86,7 @@ class Packager(object):
)
def individual_url(self, filename):
return default_storage.url(filename)
return self.storage.url(filename)
def pack_stylesheets(self, package, **kwargs):
return self.pack(package, self.compressor.compress_css, css_compressed,
......@@ -113,7 +114,7 @@ class Packager(object):
return self.compressor.compile_templates(package.templates)
def save_file(self, path, content):
return default_storage.save(path, ContentFile(smart_str(content)))
return self.storage.save(path, ContentFile(smart_str(content)))
def create_packages(self, config):
packages = {}
......
......@@ -25,7 +25,7 @@ class PipelineStorage(StaticFilesStorage):
if dry_run:
return []
packager = Packager()
packager = Packager(storage=self)
for package_name in packager.packages['css']:
package = packager.package_for('css', package_name)
output_file = packager.pack_stylesheets(package)
......
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