1010
1111import django .core .cache
1212import django .core .exceptions
13+ import django .conf
1314from django .utils import encoding , six
1415
1516from static_precompiler import exceptions , settings
2122
2223
2324if six .PY2 :
24- # noinspection PyUnresolvedReferences
25+ # noinspection PyUnresolvedReferences,PyCompatibility
2526 from urlparse import urljoin
2627else :
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+
3964def fix_line_breaks (text ):
4065 """ Convert Win line breaks to Unix
4166 """
@@ -131,10 +156,9 @@ def convert(self, content, path):
131156
132157
133158def 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
140164def build_compilers ():
@@ -218,14 +242,12 @@ def compile_static_lazy(path):
218242
219243def 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