Skip to content

Commit

Permalink
Add 'max-lines' hook
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja committed Jun 7, 2021
1 parent f4a3980 commit ac61fb2
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 1.5.0
current_version = 1.6.0

[bumpversion:file:setup.cfg]

Expand Down
20 changes: 19 additions & 1 deletion .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,19 @@
- id: no-metadata
name: no-metadata
entry: check-metadata-hook --no-metadata
description: Checks that your PO don't contain metadata headers
description: Checks that your PO files don't contain metadata headers
files: \.po$
language: python
- id: remove-metadata
name: remove-metadata
entry: check-metadata-hook --no-metadata --remove-metadata
description: Removes metadata headers from your PO files
files: \.po$
language: python
- id: check-entries
name: check-entries
entry: check-entries-hook
description: Check that PO files entries match a set of requirements passed by parameters
files: \.po$
language: python
- id: max-messages
Expand All @@ -46,6 +58,12 @@
description: Checks that each one of your PO files don't contain more than X messages
files: \.po$
language: python
- id: max-lines
name: max-lines
entry: check-entries-hook --max-lines
description: Checks that each one of your PO files don't contain more than X lines
files: \.po$
language: python
- id: min-translated
name: min-translated
entry: untranslated-messages-hook --min
Expand Down
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ Hooks for pre-commit useful working with PO files.

```yaml
- repo: https://github.com/mondeja/pre-commit-po-hooks
rev: v1.5.0
rev: v1.6.0
hooks:
- id: obsolete-messages
- id: untranslated-messages
- id: remove-django-translators
- id: standard-metadata
- id: max-lines
args: ["10000"]
```
## Hooks
Expand Down Expand Up @@ -103,6 +105,11 @@ returning exit code 1.
- `-r/--remove/--remove-metadata`: When this option is passed the metadata will
be removed from the files instead of being treated as a lint error.

### **`remove-metadata`**

Remove metadata headers from your PO files. This is an alias for
[`no-metadata` hook][no-metadata-link] passing `--remove-metadata` argument.

### **`max-messages`**

Define a maximum number of entries for each PO file. Pass an interger in the
Expand All @@ -118,6 +125,21 @@ first argument:

- Maximum number of messages allowed for each PO file.

### **`max-lines`**

Define a maximum number of lines for each PO file. Pass an interger in the
first argument:

```yaml
- id: max-lines
args:
- "10000"
```

#### Parameters

- Maximum number of lines allowed for each PO file.

### **`min-translated`**

Define a minimum number of files that must be translated in order to pass.
Expand Down Expand Up @@ -146,4 +168,5 @@ against the percentage of translated files:

[lreplace-extracted-comments-link]: https://github.com/mondeja/pre-commit-po-hooks#lreplace-extracted-comments
[check-metadata-link]: https://github.com/mondeja/pre-commit-po-hooks#check-metadata
[no-metadata-link]: https://github.com/mondeja/pre-commit-po-hooks#no-metadata
[django-rosetta-lstrip]: https://github.com/mbi/django-rosetta/pull/245
71 changes: 66 additions & 5 deletions hooks/check_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,46 @@ def maximum_number_of_messages(filenames, max_messages=10000, quiet=False):
return exitcode


def maximum_number_of_lines(filenames, max_lines=10000, quiet=False):
"""Check if a set of PO files has more lines than allowed.
Parameters
----------
filenames : list
Set of file names to check.
max_lines : int, optional
Maximum number of lines in each PO file.
quiet : bool, optional
Enabled, don't print output to stderr when more lines than allowed
are found.
Returns
-------
int: 0 if no more than ``max_lines`` lines found for each file,
1 otherwise.
"""
exitcode = 0

for filename in filenames:
with open(filename) as f:
content_lines = f.readlines()

number_of_lines = len(content_lines)
if number_of_lines > max_lines:
exitcode = 1
if not quiet:
sys.stderr.write(
f"More lines ({number_of_lines}) than allowed ({max_lines})"
f" at file {os.path.abspath(filename)}\n"
)

return exitcode


def main():
parser = argparse.ArgumentParser()
parser.add_argument(
Expand All @@ -63,20 +103,41 @@ def main():
metavar="NUMBER",
required=False,
default=None,
dest="max_messages",
help=(
"Check the maximum number of messages in each PO file "
"is not greater than the number passed in this parameter."
),
)
parser.add_argument(
"--max-lines",
type=int,
metavar="NUMBER",
required=False,
default=None,
dest="max_lines",
help=(
"Check the maximum number of lines in each PO file is not"
"greater than the number passed in this parameter."
),
)
args = parser.parse_args()
if args.max_messages is not None:
return maximum_number_of_messages(
args.filenames, args.max_messages, quiet=args.quiet
)
else:

if not any([args.max_messages, args.max_lines]):
parser.print_help()
return 1

returncodes = [
maximum_number_of_messages(args.filenames, args.max_messages, quiet=args.quiet)
if args.max_messages is not None
else 0,
maximum_number_of_lines(args.filenames, args.max_lines, quiet=args.quiet)
if args.max_lines is not None
else 0,
]

return 1 if any(returncodes) else 0


if __name__ == "__main__":
exit(main())
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = pre_commit_po_hooks
version = 1.5.0
version = 1.6.0
description = pre-commit hooks for PO files
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down

0 comments on commit ac61fb2

Please sign in to comment.