summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBertrand Croq <bertrand.croq@net-ng.com>2019-03-06 14:40:59 +0100
committerBertrand Croq <bertrand.croq@net-ng.com>2019-03-06 14:40:59 +0100
commita278be9cc74e4f57db01a7ecd3700d7e6cfb13a4 (patch)
treeb35502b6c3957a1dc02f5c10ca4ea0e4bd22c563
parent6eb47254bf57f241b33122528e70e4ac80e12be2 (diff)
downloadisort-a278be9cc74e4f57db01a7ecd3700d7e6cfb13a4.tar.gz
work on file encoding detection
-rw-r--r--isort/isort.py45
-rw-r--r--isort/main.py8
2 files changed, 38 insertions, 15 deletions
diff --git a/isort/isort.py b/isort/isort.py
index dc5cc7a6..8e8e968f 100644
--- a/isort/isort.py
+++ b/isort/isort.py
@@ -26,6 +26,7 @@ OTHER DEALINGS IN THE SOFTWARE.
"""
from __future__ import absolute_import, division, print_function, unicode_literals
+import codecs
import copy
import io
import itertools
@@ -46,7 +47,7 @@ class SortImports(object):
incorrectly_sorted = False
skipped = False
- def __init__(self, file_path=None, file_contents=None, write_to_stdout=False, check=False,
+ def __init__(self, file_path=None, file_contents=None, file_=None, write_to_stdout=False, check=False,
show_diff=False, settings_path=None, ask_to_apply=False, check_skip=True, **setting_overrides):
if not settings_path and file_path:
settings_path = os.path.dirname(os.path.abspath(file_path))
@@ -100,7 +101,8 @@ class SortImports(object):
" or matches a glob in 'skip_glob' setting".format(file_path))
file_contents = None
elif not file_contents:
- file_encoding = coding_check(file_path)
+ with io.open(file_path, 'rb') as f:
+ file_encoding = coding_check(f)
with io.open(file_path, encoding=file_encoding, newline='') as file_to_import_sort:
try:
file_contents = file_to_import_sort.read()
@@ -125,6 +127,24 @@ class SortImports(object):
"{} encoding or {} fallback encoding".format(file_path,
self.file_encoding,
file_to_import_sort.encoding))
+ elif file_:
+ self.file_encoding = coding_check(file_)
+ file_.seek(0)
+ reader = codecs.getreader(self.file_encoding)
+ file_contents = reader(file_).read()
+
+ # try to decode file_contents
+ if file_contents:
+ try:
+ basestring
+ # python 2
+ need_decode = (str, bytes)
+ except NameError:
+ # python 3
+ need_decode = bytes
+
+ if isinstance(file_contents, need_decode):
+ file_contents = file_contents.decode(coding_check(file_contents.splitlines()))
if file_contents is None or ("isort:" + "skip_file") in file_contents:
self.skipped = True
@@ -1000,19 +1020,16 @@ class SortImports(object):
self.imports[placed_module][import_type][module] = None
-def coding_check(fname, default='utf-8'):
+def coding_check(lines, default='utf-8'):
# see https://www.python.org/dev/peps/pep-0263/
pattern = re.compile(br'coding[:=]\s*([-\w.]+)')
- coding = default
- with io.open(fname, 'rb') as f:
- for line_number, line in enumerate(f, 1):
- groups = re.findall(pattern, line)
- if groups:
- coding = groups[0].decode('ascii')
- break
- if line_number > 2:
- break
-
- return coding
+ for line_number, line in enumerate(lines, 1):
+ groups = re.findall(pattern, line)
+ if groups:
+ return groups[0].decode('ascii')
+ if line_number > 2:
+ break
+
+ return default
diff --git a/isort/main.py b/isort/main.py
index bf6d93c9..40b26e39 100644
--- a/isort/main.py
+++ b/isort/main.py
@@ -326,7 +326,13 @@ def main(argv=None):
file_names = arguments.pop('files', [])
if file_names == ['-']:
- SortImports(file_contents=sys.stdin.read(), write_to_stdout=True, **arguments)
+ try:
+ # python 3
+ file_ = sys.stdin.buffer
+ except AttributeError:
+ # python 2
+ file_ = sys.stdin
+ SortImports(file_=file_, write_to_stdout=True, **arguments)
else:
if not file_names:
file_names = ['.']