Skip to content

Commit 5700324

Browse files
committed
Add 'standard-metadata' hook
1 parent 5e2c716 commit 5700324

5 files changed

Lines changed: 70 additions & 3 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.3.1
2+
current_version = 1.3.2
33

44
[bumpversion:file:setup.cfg]

.pre-commit-hooks.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@
2828
description: Checks that the metadata values of your PO files matchs against some regexes
2929
files: \.po$
3030
language: python
31+
- id: standard-metadata
32+
name: standard-metadata
33+
entry: check-metadata-hook --standard-headers
34+
description: Checks that the metadata of your PO files fits a set of standard requirements.
35+
files: \.po$
36+
language: python

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Replaces a matching string at the beginning of extracted comments.
5151

5252
Same as [`lreplace-extracted-comments`][lreplace-extracted-comments-link]
5353
passing `--match "Translators: " --replacement ""`. Useful to remove the string
54-
prepended by Django extracting messages with gettext (see more about this
54+
prepended by Django extracting messages with xgettext (see more about this
5555
problem in [django-rosetta#245][django-rosetta-lstrip]).
5656
5757
### **`check-metadata`**
@@ -69,6 +69,28 @@ Check that metadata fields matches a set of regular expressions.
6969
- `-n/--no-metadata`: When this option is passed, the hook instead checks that
7070
there is no metadata in the files, so it will exit with code 1 if some
7171
metadata is found in a file or 0 if there is no metadata in any files.
72+
73+
### **`standard-metadata`**
74+
75+
Check that the metadata of your PO files fits some standard requirements based
76+
on the next regular expressions:
77+
78+
- `Project-Id-Version`: `\d+\.\d+\.\d`
79+
- `Report-Msgid-Bugs-To`: `.+\s<.+@.+\..+>`
80+
- `Last-Translator`: `.+\s<.+@.+\..+>`
81+
- `Language-Team`: `.+\s<.+@.+\..+>`
82+
- `Language`: `\w\w_?\w?\w?(@\w+)?`
83+
- `Content-Type`: `text/plain; charset=[a-zA-Z\-]+`
84+
- `Content-Transfer-Encoding`: `\d+bits?`
85+
86+
If you need to replace some fields with other regular expressions, you can do
87+
it passing the `-h` and `-v` arguments of the
88+
[`check-metadata` hook][check-metadata-link].
89+
90+
For example, if your version
91+
includes the character `v` at the beginning:
92+
`-h "Project-Id-Version" -v "v\d+\.\d+\.\d"`
93+
7294

7395
[pypi-link]: https://pypi.org/project/pre-commit-po-hooks
7496
[pypi-version-badge-link]: https://img.shields.io/pypi/v/pre-commit-po-hooks
@@ -79,4 +101,5 @@ Check that metadata fields matches a set of regular expressions.
79101
[tests-link]: https://github.com/mondeja/pre-commit-po-hooks/actions?query=workflow%CI
80102

81103
[lreplace-extracted-comments-link]: https://github.com/mondeja/pre-commit-po-hooks#lreplace-extracted-comments
104+
[check-metadata-link]: https://github.com/mondeja/pre-commit-po-hooks#check-metadata
82105
[django-rosetta-lstrip]: https://github.com/mbi/django-rosetta/pull/245

hooks/check_metadata.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,23 @@ def main():
128128
" information, exits with code 1."
129129
),
130130
)
131+
parser.add_argument(
132+
"-s",
133+
"--standard-headers",
134+
action="store_true",
135+
dest="standard_headers",
136+
help=(
137+
"A common standard set of headers will be defined as specification."
138+
" Each value can be overwritten using '-h' and '-v' arguments."
139+
"\n\n- 'Project-Id-Version': \\d+\\.\\d+\\.\\d\n"
140+
"- 'Report-Msgid-Bugs-To': .+\\s<.+@.+\\..+>\n"
141+
"- 'Last-Translator': .+\\s<.+@.+\\..+>\n"
142+
"- 'Language-Team': .+\\s<.+@.+\\..+>\n"
143+
"- 'Language': \\w\\w_?\\w?\\w?(@\\w+)?\n"
144+
"- 'Content-Type': text/plain; charset=[A-Z\\-]+\n"
145+
r"- 'Content-Transfer-Encoding': \d+bits?"
146+
),
147+
)
131148
parser.add_argument("-q", "--quiet", action="store_true", help="Supress output")
132149
args = parser.parse_args()
133150

@@ -137,6 +154,27 @@ def main():
137154
" but both can't be non false."
138155
)
139156

157+
if args.no_metadata and args.standard_headers:
158+
raise ValueError(
159+
"You must pass either '--no-metadata' or standard headers regexes"
160+
" specification."
161+
)
162+
163+
if args.standard_headers:
164+
new_headers_spec = {
165+
"Project-Id-Version": r"\d+\.\d+\.\d",
166+
"Report-Msgid-Bugs-To": r".+\s<.+@.+\..+>",
167+
"Last-Translator": r".+\s<.+@.+\..+>",
168+
"Language-Team": r".+\s<.+@.+\..+>",
169+
"Language": r"\w\w_?\w?\w?(@\w+)?",
170+
"Content-Type": r"text/plain; charset=[a-zA-Z\-]+",
171+
"Content-Transfer-Encoding": r"\d+bits?",
172+
}
173+
if headers_spec:
174+
new_headers_spec.update(headers_spec)
175+
176+
headers_spec = new_headers_spec
177+
140178
return check_metadata(
141179
args.filenames,
142180
headers_spec,

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.3.1
3+
version = 1.3.2
44
description = pre-commit hooks for PO files
55
long_description = file: README.md
66
long_description_content_type = text/markdown

0 commit comments

Comments
 (0)