Commit 76c3060c by Timothée Peignier

finish compat with python2.x and python3.x

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