Skip to content

Commit 380ac98

Browse files
committed
Add output formats extract and misspelled, make options -e/-m aliases on --output-format
1 parent f284a29 commit 380ac98

5 files changed

Lines changed: 34 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
1818

1919
### Added
2020

21-
- Add option `-o` (or `--output-format`) to customize output format
21+
- Add option `-o` (or `--output-format`) to customize output format, options `-e` and `-m` are now aliases on this option
2222
- Add official pre-commit hook
2323

2424
## Version 4.1.0 (2024-10-23)

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,16 @@ Options:
6060
- `-s id|str`, `--spelling id|str`: check spelling (`id` = source messages, `str` = translations)
6161
- `-d <dicts>`, `--dicts <dicts>`: comma-separated list of extra dictionaries to use (in addition to file language)
6262
- `-P <file>`, `--pwl <file>`: file(s) with personal list of words used when checking spelling (this option can be given multiple times)
63-
- `-m`, `--only-misspelled`: display only misspelled words (no error, line number and translation)
63+
- `-m`, `--only-misspelled`: display only misspelled words (alias of `--output-format=misspelled`)
6464
- `-w`, `--no-whitespace`: do not check whitespace at beginning/end of strings
6565
- `-W`, `--no-whitespace-eol`: do not check whitespace at end of lines inside strings
66-
- `-e`, `--extract`: display all translations and exit (all checks except compilation are disabled in this mode)
66+
- `-e`, `--extract`: display all translations and exit (alias of `--output-format=extract`)
6767
- `-i`, `--ignore-errors`: display but ignore errors (always return 0)
68-
- `-o`, `--output-format`: output format
68+
- `-o`, `--output-format`: output format:
69+
- `full`: complete output
70+
- `oneline`: one line output
71+
- `extract`: display all translations (all checks except compilation are disabled in this mode)
72+
- `misspelled`: display only misspelled words
6973
- `-q`, `--quiet`: quiet mode: only display number of errors
7074
- `-v`, `--version`: display version and exit
7175

src/msgcheck/msgcheck.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242

4343
from msgcheck.po import PoCheck, PoFileReport
4444

45+
HELP_OUTPUT_FORMATS = [
46+
"full = complete output",
47+
"oneline = one line output",
48+
"extract = display all translations (all checks except compilation are disabled in this mode)",
49+
"misspelled = display only misspelled words",
50+
]
51+
4552

4653
class CustomHelpFormatter(argparse.RawDescriptionHelpFormatter, argparse.ArgumentDefaultsHelpFormatter):
4754
"""Help formatter with raw description/epilog and default values."""
@@ -59,7 +66,7 @@ def msgcheck_parser() -> argparse.ArgumentParser:
5966
6067
The script returns:
6168
0: all files checked are OK (or one of these options given:
62-
--extract, --only-misspelled or --ignore-errors given)
69+
--output-format={extract|misspelled} or --ignore-errors given)
6370
n: number of files with errors (1 ≤ n ≤ 255)
6471
""",
6572
)
@@ -116,7 +123,7 @@ def msgcheck_parser() -> argparse.ArgumentParser:
116123
"-m",
117124
"--only-misspelled",
118125
action="store_true",
119-
help="display only misspelled words (no error, line number and translation)",
126+
help="display only misspelled words (alias of --output-format=misspelled)",
120127
)
121128
parser.add_argument(
122129
"-w",
@@ -134,7 +141,7 @@ def msgcheck_parser() -> argparse.ArgumentParser:
134141
"-e",
135142
"--extract",
136143
action="store_true",
137-
help="display all translations and exit (all checks except compilation are disabled in this mode)",
144+
help="display all translations and exit (alias of --output-format=extract)",
138145
)
139146
parser.add_argument(
140147
"-i",
@@ -145,9 +152,9 @@ def msgcheck_parser() -> argparse.ArgumentParser:
145152
parser.add_argument(
146153
"-o",
147154
"--output-format",
148-
choices=["full", "oneline"],
155+
choices=["full", "oneline", "extract", "misspelled"],
149156
default="full",
150-
help="output format: full = complete output, oneline = one line output",
157+
help=f"output format: {', '.join(HELP_OUTPUT_FORMATS)}",
151158
)
152159
parser.add_argument(
153160
"-q",
@@ -190,10 +197,15 @@ def msgcheck_check_files(args: argparse.Namespace) -> list[PoFileReport]:
190197
"no_punct",
191198
"no_whitespace",
192199
"no_whitespace_eol",
193-
"extract",
194200
):
195201
if args.__dict__[option]:
196202
po_check.set_check(option.lstrip("no_"), not option.startswith("no_"))
203+
if args.extract:
204+
args.output_format = "extract"
205+
elif args.only_misspelled:
206+
args.output_format = "misspelled"
207+
if args.output_format == "extract":
208+
po_check.set_check("extract")
197209

198210
# check all files
199211
try:
@@ -216,7 +228,7 @@ def msgcheck_display_errors(args: argparse.Namespace, result: list[PoFileReport]
216228
files_with_errors += 1
217229
total_errors += len(report)
218230
if not args.quiet:
219-
if args.only_misspelled:
231+
if args.output_format == "misspelled":
220232
words = []
221233
for error in report:
222234
words.extend(error.get_misspelled_words())
@@ -231,9 +243,8 @@ def msgcheck_display_result(args: argparse.Namespace, result: list[PoFileReport]
231243
# display errors
232244
files_ok, files_with_errors, total_errors = msgcheck_display_errors(args, result)
233245

234-
# exit now if we extracted translations or if we displayed only
235-
# misspelled words
236-
if args.extract or args.only_misspelled:
246+
# exit now if we extracted translations or if we displayed only misspelled words
247+
if args.output_format in ("extract", "misspelled"):
237248
sys.exit(0)
238249

239250
# display files with number of errors

src/msgcheck/po.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ def __repr__(self) -> str:
526526
"""Return PO checker as string."""
527527
return f"checks: {self.checks}, dicts: {self.dicts}, extra_checkers: {self.extra_checkers}"
528528

529-
def set_check(self, check: str, state: bool) -> None:
529+
def set_check(self, check: str, state: bool = True) -> None:
530530
"""Enable/disable a specific check."""
531531
if check in self.checks:
532532
self.checks[check] = state

tests/test_msgcheck.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test_read() -> None:
5959
def test_extract() -> None:
6060
"""Test extract on a gettext file."""
6161
po_check = PoCheck()
62-
po_check.set_check("extract", True)
62+
po_check.set_check("extract")
6363
result = po_check.check_files([local_path("fr.po")])
6464
assert len(result) == 1
6565
assert "fr.po" in result[0].filename
@@ -145,7 +145,7 @@ def test_checks() -> None:
145145
def test_checks_fuzzy() -> None:
146146
"""Test checks on a gettext file including fuzzy strings."""
147147
po_check = PoCheck()
148-
po_check.set_check("fuzzy", True)
148+
po_check.set_check("fuzzy")
149149
result = po_check.check_files([local_path("fr_errors.po")])
150150

151151
# be sure we have one file in result
@@ -158,7 +158,7 @@ def test_checks_fuzzy() -> None:
158158
def test_checks_noqa() -> None:
159159
"""Test checks on a gettext file including `noqa`-commented lines."""
160160
po_check = PoCheck()
161-
po_check.set_check("check_noqa", True)
161+
po_check.set_check("check_noqa")
162162
result = po_check.check_files([local_path("fr_errors.po")])
163163

164164
# be sure we have one file in result
@@ -409,7 +409,7 @@ def test_punct_full_stop_ja_zh(language: str, msgid: str, msgstr: str, error_mes
409409
def test_invalid_utf8() -> None:
410410
"""Test checks on a file with invalid UTF-8 chars."""
411411
po_check = PoCheck()
412-
po_check.set_check("fuzzy", True)
412+
po_check.set_check("fuzzy")
413413
result = po_check.check_files([local_path("fr_invalid_utf8.po")])
414414

415415
# be sure we have one file in result

0 commit comments

Comments
 (0)