Skip to content

Commit e9d51fc

Browse files
committed
Scan entire directories with path given on command line
1 parent 70c69a3 commit e9d51fc

File tree

4 files changed

+39
-43
lines changed

4 files changed

+39
-43
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
1111
### Changed
1212

1313
- **Breaking**: improve typing annotations, Python 3.9 is now required
14+
- Scan entire directories with path given on command line
1415
- Use uv for packaging
1516
- Use ruff and ty linters
1617

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ $ uv run msgcheck
4646
Syntax:
4747

4848
```
49-
$ msgcheck [options] file.po [file.po...]
49+
$ msgcheck [options] files [files...]
5050
```
5151

5252
Options:

src/msgcheck/msgcheck.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ def msgcheck_parser() -> argparse.ArgumentParser:
152152
version=version,
153153
)
154154
parser.add_argument(
155-
"file",
155+
"files",
156156
nargs="+",
157-
help="gettext file(s) to check (*.po files)",
157+
help="files or directories with gettext files (*.po)",
158158
)
159159
return parser
160160

@@ -187,7 +187,7 @@ def msgcheck_check_files(args: argparse.Namespace) -> list[tuple[str, list[PoRep
187187
# check all files
188188
try:
189189
po_check.set_spelling_options(args.spelling, args.dicts, args.pwl)
190-
result = po_check.check_files(args.file)
190+
result = po_check.check_files(args.files)
191191
except (ImportError, OSError) as exc:
192192
print("FATAL:", exc, sep=" ")
193193
sys.exit(1)
@@ -240,12 +240,8 @@ def msgcheck_display_result(args: argparse.Namespace, result: list[tuple[str, li
240240
print(f"{filename}: {errors} errors ({str_result})")
241241

242242
# display total (if many files processed)
243-
if len(args.file) > 1:
244-
print("---")
245-
if files_with_errors == 0:
246-
print(f"TOTAL: {files_ok} files OK")
247-
else:
248-
print(f"TOTAL: {files_ok} files OK, {files_with_errors} files with {total_errors} errors")
243+
str_errors = f", {files_with_errors} files with {total_errors} errors" if files_with_errors > 0 else ""
244+
print(f"TOTAL: {files_ok} files OK{str_errors}")
249245

250246
return files_with_errors
251247

src/msgcheck/po.py

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
from __future__ import annotations
2727

28+
import os
2829
import re
2930
import subprocess
3031
import tempfile
@@ -614,42 +615,40 @@ def check_pofile(self, po_file: PoFile) -> list[PoReport]:
614615

615616
return reports
616617

617-
def check_files(self, files: list[str]) -> list[tuple[str, list[PoReport]]]:
618-
"""Check translations in PO files.
618+
def check_file(self, filename: str) -> list[PoReport]:
619+
"""Check compilation and translations in a PO file.
619620
620621
Return a list of tuples: (filename, [PoReport, PoReport, ...]).
621622
"""
622-
result = []
623+
po_file = PoFile(filename)
624+
# read the file
625+
try:
626+
po_file.read()
627+
except OSError as exc:
628+
return [PoReport(str(exc), "read", po_file.filename)]
629+
# compile the file (except if disabled)
630+
compile_rc = 0
631+
if self.checks["compile"]:
632+
compile_output, compile_rc = po_file.compile()
633+
if compile_rc != 0:
634+
# compilation failed
635+
return [PoReport(compile_output, "compile", po_file.filename)]
636+
# compilation OK
637+
return self.check_pofile(po_file)
623638

624-
for filename in files:
625-
po_file = PoFile(filename)
626-
# read the file
627-
try:
628-
po_file.read()
629-
except OSError as exc:
630-
result.append(
631-
(
632-
po_file.filename,
633-
[PoReport(str(exc), "read", po_file.filename)],
634-
),
635-
)
636-
continue
637-
# compile the file (except if disabled)
638-
compile_rc = 0
639-
if self.checks["compile"]:
640-
compile_output, compile_rc = po_file.compile()
641-
if compile_rc == 0:
642-
# compilation OK
643-
result.append((po_file.filename, self.check_pofile(po_file)))
644-
else:
645-
# compilation failed
646-
result.append(
647-
(
648-
po_file.filename,
649-
[
650-
PoReport(compile_output, "compile", po_file.filename),
651-
],
652-
),
653-
)
639+
def check_files(self, files: list[str]) -> list[tuple[str, list[PoReport]]]:
640+
"""Check compilation and translations in PO files.
654641
642+
Return a list of tuples: (filename, [PoReport, PoReport, ...]).
643+
"""
644+
result: list[tuple[str, list[PoReport]]] = []
645+
for path in files:
646+
if Path(path).is_dir():
647+
for root, _, filenames in os.walk(path):
648+
for filename in filenames:
649+
if filename.endswith(".po"):
650+
path_po = Path(root) / filename
651+
result.append((str(path_po), self.check_file(str(path_po))))
652+
else:
653+
result.append((path, self.check_file(path)))
655654
return result

0 commit comments

Comments
 (0)