Commit 76c3060c by Timothée Peignier

finish compat with python2.x and python3.x

parent 49d651b2
......@@ -85,9 +85,9 @@ class Compressor(object):
namespace = settings.PIPELINE_TEMPLATE_NAMESPACE
base_path = self.base_path(paths)
for path in paths:
contents = self.read_file(path)
contents = re.sub(b"\r?\n", b"\\\\n", contents)
contents = re.sub(b"'", b"\\'", contents)
contents = self.read_text(path)
contents = re.sub("\r?\n", "\\\\n", contents)
contents = re.sub("'", "\\'", contents)
name = self.template_name(path, base_path)
compiled += "%s['%s'] = %s('%s');\n" % (
namespace,
......@@ -130,15 +130,15 @@ class Compressor(object):
asset_url = self.construct_asset_path(asset_path, path,
output_filename, variant)
return "url(%s)" % asset_url
content = self.read_file(path)
content = self.read_text(path)
# content needs to be unicode to avoid explosions with non-ascii chars
content = re.sub(URL_DETECTOR, reconstruct, force_text(content))
content = re.sub(URL_DETECTOR, reconstruct, content)
stylesheets.append(content)
return '\n'.join(stylesheets)
def concatenate(self, paths):
"""Concatenate together a list of files"""
return b"\n".join([self.read_file(path) for path in paths])
return "\n".join([self.read_text(path) for path in paths])
def construct_asset_path(self, asset_path, css_path, output_filename, variant=None):
"""Return a rewritten asset URL for a stylesheet"""
......@@ -175,7 +175,7 @@ class Compressor(object):
"""Return the base64 encoded contents"""
if path in self.__class__.asset_contents:
return self.__class__.asset_contents[path]
data = self.read_file(path)
data = self.read_bytes(path)
self.__class__.asset_contents[path] = base64.b64encode(data)
return self.__class__.asset_contents[path]
......@@ -201,13 +201,17 @@ class Compressor(object):
output_path = posixpath.join(settings.PIPELINE_ROOT, posixpath.dirname(output_filename))
return relpath(absolute_path, output_path)
def read_file(self, path):
def read_bytes(self, path):
"""Read file content in binary mode"""
file = default_storage.open(path)
content = file.read()
file.close()
return content
def read_text(self, path):
content = self.read_bytes(path)
return force_text(content)
class CompressorBase(object):
def __init__(self, verbose):
......
......@@ -39,7 +39,7 @@ class CompressorTest(TestCase):
_('pipeline/js/first.js'),
_('pipeline/js/second.js')
])
self.assertEquals(b"""function concat() {\n console.log(arguments);\n}\n\nfunction cat() {\n console.log("hello world");\n}\n""", js)
self.assertEquals("""function concat() {\n console.log(arguments);\n}\n\nfunction cat() {\n console.log("hello world");\n}\n""", js)
@patch.object(base64, 'b64encode')
def test_encoded_content(self, mock):
......
......@@ -34,12 +34,8 @@ class StorageTest(TestCase):
'css/first.css': (self.storage, 'css/first.css'),
'images/arrow.png': (self.storage, 'images/arrow.png')
}))
self.assertEqual(processed_files, [
('css/first.css', 'css/first.css', True),
('images/arrow.png', 'images/arrow.png', True),
('testing.css', 'testing.css', True),
('scripts.css', 'scripts.css', True)
])
self.assertTrue(('css/first.css', 'css/first.css', True) in processed_files)
self.assertTrue(('images/arrow.png', 'images/arrow.png', True) in processed_files)
def tearDown(self):
settings.PIPELINE_CSS = {}
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