mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-06 20:16:53 +00:00
Refactored literals
This commit is contained in:
parent
5c514f85cc
commit
068f38ba74
1 changed files with 8 additions and 4 deletions
|
|
@ -5,6 +5,10 @@ import os
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
from typing import IO
|
from typing import IO
|
||||||
|
|
||||||
|
LF = b'\n'
|
||||||
|
CR = b'\r'
|
||||||
|
CRLF = b'\r\n'
|
||||||
|
|
||||||
|
|
||||||
def fix_file(file_obj: IO[bytes]) -> int:
|
def fix_file(file_obj: IO[bytes]) -> int:
|
||||||
# Test for newline at end of file
|
# Test for newline at end of file
|
||||||
|
|
@ -15,13 +19,13 @@ def fix_file(file_obj: IO[bytes]) -> int:
|
||||||
return 0
|
return 0
|
||||||
last_character = file_obj.read(1)
|
last_character = file_obj.read(1)
|
||||||
# last_character will be '' for an empty file
|
# last_character will be '' for an empty file
|
||||||
if last_character not in {b'\n', b'\r'} and last_character != b'':
|
if last_character not in {LF, CR} and last_character != b'':
|
||||||
# Needs this seek for windows, otherwise IOError
|
# Needs this seek for windows, otherwise IOError
|
||||||
file_obj.seek(0, os.SEEK_END)
|
file_obj.seek(0, os.SEEK_END)
|
||||||
file_obj.write(b'\n')
|
file_obj.write(LF)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
while last_character in {b'\n', b'\r'}:
|
while last_character in {LF, CR}:
|
||||||
# Deal with the beginning of the file
|
# Deal with the beginning of the file
|
||||||
if file_obj.tell() == 1:
|
if file_obj.tell() == 1:
|
||||||
# If we've reached the beginning of the file and it is all
|
# If we've reached the beginning of the file and it is all
|
||||||
|
|
@ -38,7 +42,7 @@ def fix_file(file_obj: IO[bytes]) -> int:
|
||||||
# newlines. If we find extraneous newlines, then backtrack and trim them.
|
# newlines. If we find extraneous newlines, then backtrack and trim them.
|
||||||
position = file_obj.tell()
|
position = file_obj.tell()
|
||||||
remaining = file_obj.read()
|
remaining = file_obj.read()
|
||||||
for sequence in (b'\n', b'\r\n', b'\r'):
|
for sequence in (LF, CRLF, CR):
|
||||||
if remaining == sequence:
|
if remaining == sequence:
|
||||||
return 0
|
return 0
|
||||||
elif remaining.startswith(sequence):
|
elif remaining.startswith(sequence):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue