Refactor mixed_line_ending.py

This commit is contained in:
Morgan Courbet 2017-07-20 20:58:53 +02:00
parent f9915cbbe2
commit 0e223bcb15
No known key found for this signature in database
GPG key ID: 467299D324A21B24

View file

@ -94,35 +94,38 @@ def _detect_line_ending(filename):
buf = f.read() buf = f.read()
le_counts = {} le_counts = {}
for le_enum in LineEnding:
le_counts[le_enum] = len(le_enum.regex.findall(buf))
mixed = False for le_enum in LineEnding:
le_found_previously = False le_counts[le_enum] = len(le_enum.regex.findall(buf))
most_le = None
max_le_count = 0
for le, le_count in le_counts.items(): mixed = False
le_found_cur = le_count > 0 le_found_previously = False
most_le = None
max_le_count = 0
mixed |= le_found_previously and le_found_cur for le, le_count in le_counts.items():
le_found_previously |= le_found_cur le_found_cur = le_count > 0
if le_count == max_le_count: mixed |= le_found_previously and le_found_cur
most_le = None le_found_previously |= le_found_cur
elif le_count > max_le_count:
max_le_count = le_count
most_le = le
if not mixed: if le_count == max_le_count:
return MixedLineDetection.NOT_MIXED most_le = None
elif le_count > max_le_count:
max_le_count = le_count
most_le = le
for mld in MixedLineDetection: if not mixed:
if mld.line_ending_enum is not None \ return MixedLineDetection.NOT_MIXED
and mld.line_ending_enum == most_le:
return mld
return MixedLineDetection.UNKNOWN for mld in MixedLineDetection:
if (
mld.line_ending_enum is not None and
mld.line_ending_enum == most_le
):
return mld
return MixedLineDetection.UNKNOWN
def _process_no_fix(filenames): def _process_no_fix(filenames):
@ -154,8 +157,6 @@ def _process_fix_auto(filenames):
if detect_result == MixedLineDetection.NOT_MIXED: if detect_result == MixedLineDetection.NOT_MIXED:
print('The file %s has no mixed line ending', filename) print('The file %s has no mixed line ending', filename)
mle_found |= False
elif detect_result == MixedLineDetection.UNKNOWN: elif detect_result == MixedLineDetection.UNKNOWN:
print( print(
'Could not define most frequent line ending in ' 'Could not define most frequent line ending in '