avoid reading the file twice
This commit is contained in:
@@ -33,16 +33,17 @@ from yamllint.linter import PROBLEM_LEVELS
|
|||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def yamlopen(fp, **iowrapper_kwargs):
|
def yamlopen(fp, **iowrapper_kwargs):
|
||||||
encoding = iowrapper_kwargs.pop('encoding', None)
|
|
||||||
with io.open(fp, mode='rb') as raw_file:
|
with io.open(fp, mode='rb') as raw_file:
|
||||||
if encoding is None:
|
if iowrapper_kwargs.get('encoding'):
|
||||||
|
with io.TextIOWrapper(raw_file, **iowrapper_kwargs) as decoded:
|
||||||
|
yield decoded
|
||||||
|
else:
|
||||||
raw_data = raw_file.read()
|
raw_data = raw_file.read()
|
||||||
encoding = chardet.detect(raw_data).get('encoding') or 'utf-8'
|
encoding = chardet.detect(raw_data).get('encoding') or 'utf-8'
|
||||||
raw_file.seek(0)
|
iowrapper_kwargs['encoding'] = encoding
|
||||||
with io.TextIOWrapper(
|
buffer = io.BytesIO(raw_data)
|
||||||
raw_file, encoding=encoding, **iowrapper_kwargs
|
with io.TextIOWrapper(buffer, **iowrapper_kwargs) as decoded:
|
||||||
) as decoded:
|
yield decoded
|
||||||
yield decoded
|
|
||||||
|
|
||||||
|
|
||||||
def find_files_recursively(items, conf):
|
def find_files_recursively(items, conf):
|
||||||
|
|||||||
Reference in New Issue
Block a user