Skip to content

Commit 0279449

Browse files
author
Andrey Fedoseev
committed
Add read_file and write_file functions to work with text files taking settings.FILE_CHARSET into consideration
1 parent f6898d9 commit 0279449

File tree

4 files changed

+50
-17
lines changed

4 files changed

+50
-17
lines changed

static_precompiler/compilers/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ def get_source(self, source_path):
158158
:returns: str
159159
160160
"""
161-
with open(self.get_full_source_path(source_path)) as source:
162-
return source.read()
161+
return utils.read_file(self.get_full_source_path(source_path))
163162

164163
def compile(self, source_path, from_management=False, verbosity=0):
165164
""" Compile the given source path and return relative path to the compiled file.

static_precompiler/compilers/libsass.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,12 @@ def compile_file(self, source_path):
6464
compiled = encoding.force_str(compiled)
6565
sourcemap = encoding.force_str(sourcemap)
6666

67-
with open(full_output_path, "w+") as output_file:
68-
output_file.write(compiled)
67+
utils.write_file(compiled, full_output_path)
6968

7069
utils.convert_urls(full_output_path, source_path)
7170

7271
if self.is_sourcemap_enabled:
73-
with open(sourcemap_path, "w+") as output_file:
74-
output_file.write(sourcemap)
75-
72+
utils.write_file(sourcemap, sourcemap_path)
7673
utils.fix_sourcemap(sourcemap_path, source_path, full_output_path)
7774

7875
return self.get_output_path(source_path)

static_precompiler/tests/test_utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
1+
# coding: utf-8
2+
from __future__ import unicode_literals
13
import django.core.exceptions
24
import pytest
35
from pretend import stub
46

57
from static_precompiler import compilers, exceptions, utils
8+
import os
9+
import six
10+
11+
12+
def test_write_read_file(tmpdir, settings):
13+
settings.FILE_CHARSET = "utf-8"
14+
path = os.path.join(tmpdir.dirname, "foo.txt")
15+
utils.write_file("Привет, Мир!", path)
16+
17+
assert os.path.exists(path)
18+
read_content = utils.read_file(path)
19+
assert isinstance(read_content, six.text_type)
20+
assert read_content == "Привет, Мир!"
621

722

823
def test_build_compilers(monkeypatch):

static_precompiler/utils.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import django.core.cache
1212
import django.core.exceptions
13+
import django.conf
1314
from django.utils import encoding, six
1415

1516
from static_precompiler import exceptions, settings
@@ -21,10 +22,10 @@
2122

2223

2324
if six.PY2:
24-
# noinspection PyUnresolvedReferences
25+
# noinspection PyUnresolvedReferences,PyCompatibility
2526
from urlparse import urljoin
2627
else:
27-
# noinspection PyUnresolvedReferences
28+
# noinspection PyUnresolvedReferences,PyCompatibility
2829
from urllib.parse import urljoin
2930

3031

@@ -36,6 +37,30 @@ def normalize_path(posix_path):
3637
return os.path.join(*posix_path.split("/"))
3738

3839

40+
def read_file(path):
41+
""" Return the contents of a file as unicode. """
42+
if six.PY2:
43+
with open(path) as file_object:
44+
return file_object.read().decode(django.conf.settings.FILE_CHARSET)
45+
else:
46+
with open(path, encoding=django.conf.settings.FILE_CHARSET) as file_object:
47+
return file_object.read()
48+
49+
50+
def write_file(content, path):
51+
""" Write unicode content to a file. """
52+
53+
# Convert to unicode
54+
content = encoding.force_text(content)
55+
56+
if six.PY2:
57+
with open(path, "w+") as file_object:
58+
file_object.write(content.encode(django.conf.settings.FILE_CHARSET))
59+
else:
60+
with open(path, "w+", encoding=django.conf.settings.FILE_CHARSET) as file_object:
61+
file_object.write(content)
62+
63+
3964
def fix_line_breaks(text):
4065
""" Convert Win line breaks to Unix
4166
"""
@@ -131,10 +156,9 @@ def convert(self, content, path):
131156

132157

133158
def convert_urls(compiled_full_path, source_path):
134-
with open(compiled_full_path, "r+") as compiled_file:
135-
content = compiled_file.read()
136-
with open(compiled_full_path, "w") as compiled_file:
137-
compiled_file.write(url_converter.convert(content, source_path))
159+
content = read_file(compiled_full_path)
160+
converted_content = url_converter.convert(content, source_path)
161+
write_file(converted_content, compiled_full_path)
138162

139163

140164
def build_compilers():
@@ -218,14 +242,12 @@ def compile_static_lazy(path):
218242

219243
def fix_sourcemap(sourcemap_full_path, source_path, compiled_full_path):
220244

221-
with open(sourcemap_full_path) as sourcemap_file:
222-
sourcemap = json.loads(sourcemap_file.read())
245+
sourcemap = json.loads(read_file(sourcemap_full_path))
223246

224247
# Stylus, unlike SASS, can't add correct relative paths in source map when the compiled file
225248
# is not in the same dir as the source file. We fix it here.
226249
sourcemap["sourceRoot"] = "../" * len(source_path.split("/")) + posixpath.dirname(source_path)
227250
sourcemap["sources"] = [os.path.basename(source) for source in sourcemap["sources"]]
228251
sourcemap["file"] = posixpath.basename(os.path.basename(compiled_full_path))
229252

230-
with open(sourcemap_full_path, "w") as sourcemap_file:
231-
sourcemap_file.write(json.dumps(sourcemap))
253+
write_file(json.dumps(sourcemap), sourcemap_full_path)

0 commit comments

Comments
 (0)