mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-05 03:26:53 +00:00
pre-commit-hooks: python3.6+
This commit is contained in:
parent
551d1a07b3
commit
f5c42a050b
60 changed files with 291 additions and 493 deletions
|
|
@ -1,18 +1,13 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import argparse
|
||||
import collections
|
||||
from typing import IO
|
||||
from typing import NamedTuple
|
||||
from typing import Optional
|
||||
from typing import Sequence
|
||||
from typing import Union
|
||||
|
||||
DEFAULT_PRAGMA = b'# -*- coding: utf-8 -*-'
|
||||
|
||||
|
||||
def has_coding(line): # type: (bytes) -> bool
|
||||
def has_coding(line: bytes) -> bool:
|
||||
if not line.strip():
|
||||
return False
|
||||
return (
|
||||
|
|
@ -25,30 +20,30 @@ def has_coding(line): # type: (bytes) -> bool
|
|||
)
|
||||
|
||||
|
||||
class ExpectedContents(
|
||||
collections.namedtuple(
|
||||
'ExpectedContents', ('shebang', 'rest', 'pragma_status', 'ending'),
|
||||
),
|
||||
):
|
||||
"""
|
||||
pragma_status:
|
||||
- True: has exactly the coding pragma expected
|
||||
- False: missing coding pragma entirely
|
||||
- None: has a coding pragma, but it does not match
|
||||
"""
|
||||
__slots__ = ()
|
||||
class ExpectedContents(NamedTuple):
|
||||
shebang: bytes
|
||||
rest: bytes
|
||||
# True: has exactly the coding pragma expected
|
||||
# False: missing coding pragma entirely
|
||||
# None: has a coding pragma, but it does not match
|
||||
pragma_status: Optional[bool]
|
||||
ending: bytes
|
||||
|
||||
@property
|
||||
def has_any_pragma(self): # type: () -> bool
|
||||
def has_any_pragma(self) -> bool:
|
||||
return self.pragma_status is not False
|
||||
|
||||
def is_expected_pragma(self, remove): # type: (bool) -> bool
|
||||
def is_expected_pragma(self, remove: bool) -> bool:
|
||||
expected_pragma_status = not remove
|
||||
return self.pragma_status is expected_pragma_status
|
||||
|
||||
|
||||
def _get_expected_contents(first_line, second_line, rest, expected_pragma):
|
||||
# type: (bytes, bytes, bytes, bytes) -> ExpectedContents
|
||||
def _get_expected_contents(
|
||||
first_line: bytes,
|
||||
second_line: bytes,
|
||||
rest: bytes,
|
||||
expected_pragma: bytes,
|
||||
) -> ExpectedContents:
|
||||
ending = b'\r\n' if first_line.endswith(b'\r\n') else b'\n'
|
||||
|
||||
if first_line.startswith(b'#!'):
|
||||
|
|
@ -60,7 +55,7 @@ def _get_expected_contents(first_line, second_line, rest, expected_pragma):
|
|||
rest = second_line + rest
|
||||
|
||||
if potential_coding.rstrip(b'\r\n') == expected_pragma:
|
||||
pragma_status = True # type: Optional[bool]
|
||||
pragma_status: Optional[bool] = True
|
||||
elif has_coding(potential_coding):
|
||||
pragma_status = None
|
||||
else:
|
||||
|
|
@ -72,8 +67,11 @@ def _get_expected_contents(first_line, second_line, rest, expected_pragma):
|
|||
)
|
||||
|
||||
|
||||
def fix_encoding_pragma(f, remove=False, expected_pragma=DEFAULT_PRAGMA):
|
||||
# type: (IO[bytes], bool, bytes) -> int
|
||||
def fix_encoding_pragma(
|
||||
f: IO[bytes],
|
||||
remove: bool = False,
|
||||
expected_pragma: bytes = DEFAULT_PRAGMA,
|
||||
) -> int:
|
||||
expected = _get_expected_contents(
|
||||
f.readline(), f.readline(), f.read(), expected_pragma,
|
||||
)
|
||||
|
|
@ -103,21 +101,20 @@ def fix_encoding_pragma(f, remove=False, expected_pragma=DEFAULT_PRAGMA):
|
|||
return 1
|
||||
|
||||
|
||||
def _normalize_pragma(pragma): # type: (Union[bytes, str]) -> bytes
|
||||
if not isinstance(pragma, bytes):
|
||||
pragma = pragma.encode('UTF-8')
|
||||
return pragma.rstrip()
|
||||
def _normalize_pragma(pragma: str) -> bytes:
|
||||
return pragma.encode().rstrip()
|
||||
|
||||
|
||||
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
|
||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||
parser = argparse.ArgumentParser(
|
||||
'Fixes the encoding pragma of python files',
|
||||
)
|
||||
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
|
||||
parser.add_argument(
|
||||
'--pragma', default=DEFAULT_PRAGMA, type=_normalize_pragma,
|
||||
help='The encoding pragma to use. Default: {}'.format(
|
||||
DEFAULT_PRAGMA.decode(),
|
||||
help=(
|
||||
f'The encoding pragma to use. '
|
||||
f'Default: {DEFAULT_PRAGMA.decode()}'
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
|
|
@ -141,9 +138,7 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
|
|||
retv |= file_ret
|
||||
if file_ret:
|
||||
print(
|
||||
fmt.format(
|
||||
pragma=args.pragma.decode(), filename=filename,
|
||||
),
|
||||
fmt.format(pragma=args.pragma.decode(), filename=filename),
|
||||
)
|
||||
|
||||
return retv
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue