44import glob
55import logging
66import sys
7+ from pathlib import Path
78
89from . import __version__ , log
910from .sanitize import sanitize_file
@@ -19,6 +20,7 @@ def _parsed_args(args=None):
1920 # However, when passing multiple files this results in
2021 # ResourceWarning: unclosed file <_io.TextIOWrapper...>.
2122 nargs = "+" ,
23+ type = Path ,
2224 help = 'PO file(s) to sanitize; use glob patterns to process multiple files; use "**" for recursive scans' ,
2325 )
2426 return parser .parse_args (args )
@@ -27,13 +29,20 @@ def _parsed_args(args=None):
2729def main_without_logging_setup (args = None ) -> int :
2830 result = 0
2931 po_path = None
32+ actual_args = [(str (arg ) if isinstance (arg , Path ) else arg ) for arg in args ]
3033 sanitized_file_count = 0
31- arguments = _parsed_args (args )
34+ arguments = _parsed_args (actual_args )
3235 try :
3336 for po_path_pattern in arguments .po_paths :
34- for po_path in glob . iglob ( po_path_pattern , recursive = True ):
35- sanitize_file (po_path )
37+ if po_path_pattern . is_file ( ):
38+ sanitize_file (po_path_pattern )
3639 sanitized_file_count += 1
40+ else :
41+ for po_path in glob .iglob (str (po_path_pattern ), recursive = True ): # noqa: PTH207
42+ # There does not seem to be a way for `path.glob(pattern)` to separate `path` from the `pattern`,
43+ # while `iglob(path_with_pattern)` can process a single combined path and pattern.
44+ sanitize_file (po_path )
45+ sanitized_file_count += 1
3746 except Exception as error :
3847 log .error ('cannot sanitize "%s": %s' , po_path , error )
3948 result = 1
0 commit comments