avoid reading the file twice

pull/240/head
Spencer Young 5 years ago
parent 940f95426b
commit b6d4df4fd7

@ -33,15 +33,16 @@ 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

Loading…
Cancel
Save