Add new byte-order-marker checker/fixer · lircs/pre-commit-hooks@d18bd5b · GitHub
Skip to content

Commit d18bd5b

Browse files
jgowdyasottile
authored andcommitted
Add new byte-order-marker checker/fixer
1 parent 5bd9e74 commit d18bd5b

5 files changed

Lines changed: 56 additions & 5 deletions

File tree

.pre-commit-hooks.yaml

Lines changed: 8 additions & 2 deletions

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ Require literal syntax when initializing empty or zero Python builtin types.
4242
- Ignore this requirement for specific builtin types with `--ignore=type1,type2,…`.
4343
- Forbid `dict` keyword syntax with `--no-allow-dict-kwargs`.
4444

45-
#### `check-byte-order-marker`
46-
Forbid files which have a UTF-8 byte-order marker
47-
4845
#### `check-case-conflict`
4946
Check for files with names that would conflict on a case-insensitive filesystem like MacOS HFS+ or Windows FAT.
5047

@@ -102,6 +99,9 @@ This hook replaces double quoted strings with single quoted strings.
10299
#### `end-of-file-fixer`
103100
Makes sure files end in a newline and only a newline.
104101

102+
#### `fix-byte-order-marker`
103+
removes UTF-8 byte order marker
104+
105105
#### `fix-encoding-pragma`
106106
Add `# -*- coding: utf-8 -*-` to the top of python files.
107107
- To remove the coding pragma pass `--remove` (useful in a python3-only codebase)
@@ -183,6 +183,7 @@ Trims trailing whitespace.
183183
[mirrors-autopep8](https://github.com/pre-commit/mirrors-autopep8)
184184
- `pyflakes`: instead use `flake8`
185185
- `flake8`: instead use [upstream flake8](https://gitlab.com/pycqa/flake8)
186+
- `check-byte-order-marker`: instead use fix-byte-order-marker
186187

187188
### As a standalone package
188189

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import argparse
2+
from typing import Optional
3+
from typing import Sequence
4+
5+
6+
def main(argv: Optional[Sequence[str]] = None) -> int:
7+
parser = argparse.ArgumentParser()
8+
parser.add_argument('filenames', nargs='*', help='Filenames to check')
9+
args = parser.parse_args(argv)
10+
11+
retv = 0
12+
13+
for filename in args.filenames:
14+
with open(filename, 'rb') as f_b:
15+
bts = f_b.read(3)
16+
17+
if bts == b'\xef\xbb\xbf':
18+
with open(filename, newline='', encoding='utf-8-sig') as f:
19+
contents = f.read()
20+
with open(filename, 'w', newline='', encoding='utf-8') as f:
21+
f.write(contents)
22+
23+
print(f'{filename}: removed byte-order marker')
24+
retv = 1
25+
26+
return retv
27+
28+
29+
if __name__ == '__main__':
30+
exit(main())

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ console_scripts =
4848
double-quote-string-fixer = pre_commit_hooks.string_fixer:main
4949
end-of-file-fixer = pre_commit_hooks.end_of_file_fixer:main
5050
file-contents-sorter = pre_commit_hooks.file_contents_sorter:main
51+
fix-byte-order-marker = pre_commit_hooks.fix_byte_order_marker:main
5152
fix-encoding-pragma = pre_commit_hooks.fix_encoding_pragma:main
5253
forbid-new-submodules = pre_commit_hooks.forbid_new_submodules:main
5354
mixed-line-ending = pre_commit_hooks.mixed_line_ending:main
Lines changed: 13 additions & 0 deletions

0 commit comments

Comments
 (0)