mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-06 03:56:54 +00:00
Add check for text file encodings
This commit is contained in:
parent
56b4a7e506
commit
69d0dfbab2
5 changed files with 58 additions and 0 deletions
29
pre_commit_hooks/check_encoding.py
Normal file
29
pre_commit_hooks/check_encoding.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import argparse
|
||||
from typing import Optional
|
||||
from typing import Sequence
|
||||
|
||||
|
||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('filenames', nargs='*', help='Filenames to check.')
|
||||
parser.add_argument('--encoding', help='Encoding to assert.')
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
retval = 0
|
||||
for filename in args.filenames:
|
||||
try:
|
||||
with open(filename, encoding=args.encoding) as f:
|
||||
f.read()
|
||||
except LookupError as exc:
|
||||
# Unknown encoding, don't bother with the rest
|
||||
print(f'{__file__}: {exc}')
|
||||
retval = 2
|
||||
break
|
||||
except Exception as exc:
|
||||
print(f'{filename}: {exc}')
|
||||
retval = 1
|
||||
return retval
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
Loading…
Add table
Add a link
Reference in a new issue