Skip to content

Commit 9305d68

Browse files
committed
Add '--no-metadata' option to 'check-metadata' hook to check that files doesn't have metadata
1 parent de9d602 commit 9305d68

5 files changed

Lines changed: 88 additions & 10 deletions

File tree

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[bumpversion]
2-
current_version = 1.2.0
2+
current_version = 1.3.0
33

44
[bumpversion:file:setup.cfg]

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ Check that metadata fields matches a set of regular expressions.
6666
- `-v/--value REGEX`: Can be passed multiple times. Indicates the regular
6767
expression that the last header passed in the argument `-h/--header` must
6868
match in the checked PO files.
69+
- `-n/--no-metadata`: When this option is passed, the hook instead checks that
70+
there is no metadata in the files, so it will exit with code 1 if some
71+
metadata is found in a file or 0 if there is no metadata in any files.
6972

7073
[pypi-link]: https://pypi.org/project/pre-commit-po-hooks
7174
[pypi-version-badge-link]: https://img.shields.io/pypi/v/pre-commit-po-hooks

hooks/check_metadata.py

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import sys
1010

1111

12-
def check_metadata(filenames, headers_spec, quiet=False):
12+
def check_metadata(filenames, headers_spec, no_metadata=False, quiet=False):
1313
"""Check that metadata headers and values match a set of requirements.
1414
1515
Parameters
@@ -22,6 +22,11 @@ def check_metadata(filenames, headers_spec, quiet=False):
2222
Name of headers as keys and regular expressions as values to match
2323
in the metadata of each file.
2424
25+
no_metadata : bool, optional
26+
When this option is set to ``True``, the hook instead checks that there
27+
is no metadata in the files, so it will return 1 if metadata is found
28+
and 0 exitcode otherwise.
29+
2530
quiet : bool, optional
2631
Enabled, don't print output to stderr when a wrong metadata is found.
2732
@@ -44,21 +49,35 @@ def check_metadata(filenames, headers_spec, quiet=False):
4449
if line.startswith('msgid ""') and content_lines[i + 1].startswith(
4550
'msgstr ""'
4651
):
47-
if content_lines[i + 2].startswith('"'):
52+
if (len(content_lines) > i + 2) and content_lines[i + 2].startswith(
53+
'"'
54+
):
4855
_first_metadata_line = i + 2
56+
57+
if no_metadata:
58+
exitcode = 1
59+
if not quiet:
60+
sys.stderr.write(
61+
f"Found unexpected metadata at {filename}:{i +3}\n"
62+
)
4963
else:
50-
sys.stderr.write(f"No metadata found in the file {filename}\n")
51-
exitcode = 1
64+
if not no_metadata:
65+
exitcode = 1
66+
if not quiet:
67+
sys.stderr.write(
68+
f"No metadata found in the file {filename}\n"
69+
)
70+
5271
break
5372

54-
if _first_metadata_line is None:
73+
if (no_metadata and exitcode) or _first_metadata_line is None:
5574
continue
5675

5776
for i, line in enumerate(content_lines[_first_metadata_line:]):
5877
if not line.strip():
5978
break
6079

61-
header, value = line.split(": ")
80+
header, value = line.split(": ", maxsplit=1)
6281
header = header.lstrip('"')
6382

6483
if header in headers_spec_regex:
@@ -99,10 +118,31 @@ def main():
99118
parser.add_argument(
100119
"filenames", nargs="*", help="Filenames to check for obsolete messages"
101120
)
121+
parser.add_argument(
122+
"-n",
123+
"--no-metadata",
124+
action="store_true",
125+
dest="no_metadata",
126+
help=(
127+
"The files shouldn't have metadata. If a file has metadata"
128+
" information, exits with code 1."
129+
),
130+
)
102131
parser.add_argument("-q", "--quiet", action="store_true", help="Supress output")
103132
args = parser.parse_args()
104133

105-
return check_metadata(args.filenames, headers_spec, quiet=args.quiet)
134+
if args.no_metadata and len(headers_spec.keys()):
135+
raise ValueError(
136+
"You must pass either '--no-metadata' or headers regexes specification,"
137+
" but both can't be non false."
138+
)
139+
140+
return check_metadata(
141+
args.filenames,
142+
headers_spec,
143+
no_metadata=args.no_metadata,
144+
quiet=args.quiet,
145+
)
106146

107147

108148
if __name__ == "__main__":

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = pre_commit_po_hooks
3-
version = 1.2.0
3+
version = 1.3.0
44
description = pre-commit hooks for PO files
55
long_description = file: README.md
66
long_description_content_type = text/markdown

tests/test_check_metadata.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
(
1616
"contents",
1717
"headers_spec",
18+
"no_metadata",
1819
"n_printed_errors",
1920
"expected_exitcode",
2021
"expected_line_numbers",
@@ -32,16 +33,42 @@
3233
"Project-Id-Version": r"v\d+\.\d+\.\d+",
3334
"Report-Msgid-Bugs-To": "foobar",
3435
},
36+
False,
3537
1,
3638
1,
3739
[5],
3840
),
41+
(
42+
[
43+
('#\nmsgid ""\nmsgstr ""\n'),
44+
],
45+
{},
46+
True,
47+
0,
48+
0,
49+
None,
50+
),
51+
(
52+
[
53+
(
54+
'#\nmsgid ""\nmsgstr ""\n"Project-Id-Version: v0.240.1\\n"\n'
55+
'"Report-Msgid-Bugs-To: Álvaro Mondéjar'
56+
' <mondejar1994@gmail.com>\\n"\n'
57+
),
58+
],
59+
{},
60+
True,
61+
1,
62+
1,
63+
[4],
64+
),
3965
),
4066
)
4167
def test_check_obsolete_messages(
4268
quiet,
4369
contents,
4470
headers_spec,
71+
no_metadata,
4572
n_printed_errors,
4673
expected_exitcode,
4774
expected_line_numbers,
@@ -58,7 +85,15 @@ def test_check_obsolete_messages(
5885

5986
stderr = io.StringIO()
6087
with contextlib.redirect_stderr(stderr):
61-
assert check_metadata(filenames, headers_spec, quiet=quiet) == expected_exitcode
88+
assert (
89+
check_metadata(
90+
filenames,
91+
headers_spec,
92+
no_metadata=no_metadata,
93+
quiet=quiet,
94+
)
95+
== expected_exitcode
96+
)
6297

6398
stderr_lines = stderr.getvalue().splitlines()
6499
if quiet:

0 commit comments

Comments
 (0)