Skip to content

Commit d18bd5b

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

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

.pre-commit-hooks.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
language: python
1818
types: [python]
1919
- id: check-byte-order-marker
20-
name: Check for byte-order marker
21-
description: Forbid files which have a UTF-8 byte-order marker
20+
name: 'check BOM - deprecated: use fix-byte-order-marker'
21+
description: forbid files which have a UTF-8 byte-order marker
2222
entry: check-byte-order-marker
2323
language: python
2424
types: [text]
@@ -131,6 +131,12 @@
131131
entry: file-contents-sorter
132132
language: python
133133
files: '^$'
134+
- id: fix-byte-order-marker
135+
name: fix UTF-8 byte order marker
136+
description: removes UTF-8 byte order marker
137+
entry: fix-byte-order-marker
138+
language: python
139+
types: [text]
134140
- id: fix-encoding-pragma
135141
name: Fix python encoding pragma
136142
language: python

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

tests/fix_byte_order_marker_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from pre_commit_hooks import fix_byte_order_marker
2+
3+
4+
def test_failure(tmpdir):
5+
f = tmpdir.join('f.txt')
6+
f.write_text('ohai', encoding='utf-8-sig')
7+
assert fix_byte_order_marker.main((str(f),)) == 1
8+
9+
10+
def test_success(tmpdir):
11+
f = tmpdir.join('f.txt')
12+
f.write_text('ohai', encoding='utf-8')
13+
assert fix_byte_order_marker.main((str(f),)) == 0

0 commit comments

Comments
 (0)