Skip to content

Commit d688cf7

Browse files
authored
Using pre-commit built-in text files detection feature (#72)
1 parent 5802f39 commit d688cf7

File tree

10 files changed

+20
-84
lines changed

10 files changed

+20
-84
lines changed

.pre-commit-config.yaml

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,10 @@ repos:
4343
args:
4444
- --py37-plus
4545
exclude: ^tests/resources/.*
46-
- repo: https://github.com/pre-commit/mirrors-pylint
47-
rev: v3.0.0a5
48-
hooks:
49-
- id: pylint
50-
args:
51-
- --rcfile=.pylintrc
52-
- --reports=no
53-
- --py-version=3.7
54-
additional_dependencies: [pytest, rapidfuzz]
55-
exclude: ^tests/resources/.*(init_with_license\.py|todo).*$
5646
- repo: https://github.com/psf/black
5747
rev: 23.3.0
5848
hooks:
5949
- id: black
60-
args: [--quiet]
6150
exclude: ^tests/resources/
6251
- repo: https://github.com/Lucas-C/pre-commit-hooks-bandit
6352
rev: v1.0.6
@@ -77,8 +66,16 @@ repos:
7766
- --show-error-context
7867
- repo: local
7968
hooks:
80-
- id: py.test
81-
name: py.test
69+
- id: pylint
70+
name: pylint
71+
# 3x faster than the official pylint hook, and has no issue with imports
72+
# (tested with: time pre-commit run pylint --all-files)
73+
language: system
74+
entry: pylint
75+
files: \.py$
76+
exclude: ^tests/resources/.*(init_with_license|todo)
77+
- id: pytest
78+
name: pytest
8279
language: python
8380
additional_dependencies: [pytest, pytest-cov, coverage, rapidfuzz]
8481
entry: pytest -sv

.pre-commit-hooks.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@
33
description: "Forbid files containing CRLF end-lines to be committed"
44
entry: forbid_crlf
55
language: python
6-
files: ''
6+
types: [text]
77
- id: remove-crlf
88
name: CRLF end-lines remover
99
description: "Replace CRLF end-lines by LF ones before committing"
1010
entry: remove_crlf
1111
language: python
12-
files: ''
12+
types: [text]
1313
- id: forbid-tabs
1414
name: No-tabs checker
1515
description: "Forbid files containing tabs to be committed"
1616
entry: forbid_tabs
1717
language: python
18-
files: ''
18+
types: [text]
1919
exclude: (Makefile|debian/rules|.gitmodules)(\.in)?$
2020
- id: remove-tabs
2121
name: Tabs remover
2222
description: "Replace tabs by whitespaces before committing"
2323
entry: remove_tabs
2424
language: python
2525
args: [ --whitespaces-count, '4' ]
26-
files: ''
26+
types: [text]
2727
exclude: (Makefile|debian/rules|.gitmodules)(\.in)?$
2828
- id: chmod
2929
name: Set file permissions
@@ -34,4 +34,4 @@
3434
description: "Insert a short license disclaimer as a header comment in source files"
3535
entry: insert_license
3636
language: python
37-
files: '.*/.*'
37+
types: [text]

pre_commit_hooks/forbid_crlf.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import argparse, sys
2-
from .utils import is_textfile
32

43

54
def contains_crlf(filename):
@@ -14,8 +13,7 @@ def main(argv=None):
1413
parser = argparse.ArgumentParser()
1514
parser.add_argument("filenames", nargs="*", help="filenames to check")
1615
args = parser.parse_args(argv)
17-
text_files = [f for f in args.filenames if is_textfile(f)]
18-
files_with_crlf = [f for f in text_files if contains_crlf(f)]
16+
files_with_crlf = [f for f in args.filenames if contains_crlf(f)]
1917
return_code = 0
2018
for file_with_crlf in files_with_crlf:
2119
print(f"CRLF end-lines detected in file: {file_with_crlf}")

pre_commit_hooks/forbid_tabs.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import argparse, sys
2-
from .utils import is_textfile
32

43

54
def contains_tabs(filename):
@@ -11,8 +10,7 @@ def main(argv=None):
1110
parser = argparse.ArgumentParser()
1211
parser.add_argument("filenames", nargs="*", help="filenames to check")
1312
args = parser.parse_args(argv)
14-
text_files = [f for f in args.filenames if is_textfile(f)]
15-
files_with_tabs = [f for f in text_files if contains_tabs(f)]
13+
files_with_tabs = [f for f in args.filenames if contains_tabs(f)]
1614
return_code = 0
1715
for file_with_tabs in files_with_tabs:
1816
print(f"Tabs detected in file: {file_with_tabs}")

pre_commit_hooks/remove_crlf.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import argparse, sys
2-
from .utils import is_textfile
32

43

54
def contains_crlf(filename):
@@ -23,8 +22,7 @@ def main(argv=None):
2322
parser = argparse.ArgumentParser()
2423
parser.add_argument("filenames", nargs="*", help="filenames to check")
2524
args = parser.parse_args(argv)
26-
text_files = [f for f in args.filenames if is_textfile(f)]
27-
files_with_crlf = [f for f in text_files if contains_crlf(f)]
25+
files_with_crlf = [f for f in args.filenames if contains_crlf(f)]
2826
for file_with_crlf in files_with_crlf:
2927
print(f"Removing CRLF end-lines in: {file_with_crlf}")
3028
removes_crlf_in_file(file_with_crlf)

pre_commit_hooks/remove_tabs.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import argparse, sys
2-
from .utils import is_textfile
32

43

54
def contains_tabs(filename):
@@ -26,8 +25,7 @@ def main(argv=None):
2625
)
2726
parser.add_argument("filenames", nargs="*", help="filenames to check")
2827
args = parser.parse_args(argv)
29-
text_files = [f for f in args.filenames if is_textfile(f)]
30-
files_with_tabs = [f for f in text_files if contains_tabs(f)]
28+
files_with_tabs = [f for f in args.filenames if contains_tabs(f)]
3129
for file_with_tabs in files_with_tabs:
3230
print(
3331
f"Substituting tabs in: {file_with_tabs} by {args.whitespaces_count} whitespaces"

pre_commit_hooks/utils.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
-e .
33
# Install development specific dependencies
44
pre-commit
5+
pylint
56
pytest
67
pytest-cov
78
coverage

tests/remove_crlf_test.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,6 @@ def test_remove_crlf(input_s, expected, tmpdir):
1919
assert input_file.read_bytes() == bytes(expected, "UTF-8")
2020

2121

22-
@pytest.mark.parametrize(
23-
("input_s", "expected"),
24-
(
25-
("foo\r\nbar", "foo\r\nbar"),
26-
("bar\nbaz\r\n", "bar\nbaz\r\n"),
27-
),
28-
)
29-
def test_noremove_crlf(input_s, expected, tmpdir):
30-
input_file = Path(tmpdir.join("file.pdf"))
31-
input_file.write_bytes(bytes(input_s, "UTF-8"))
32-
assert remove_crlf([str(input_file)]) == 0
33-
assert input_file.read_bytes() == bytes(expected, "UTF-8")
34-
35-
3622
@pytest.mark.parametrize(("arg"), ("", "a.b", "a/b"))
3723
def test_badopt(arg):
3824
with pytest.raises(

tests/text_utils_test.py

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)