3636import shlex
3737import sys
3838
39- from . po import PoCheck
39+ from .po import PoCheck
4040
4141
42- __version__ = ' 4.1.0-dev'
42+ __version__ = " 4.1.0-dev"
4343
4444
4545def msgcheck_version ():
@@ -51,58 +51,108 @@ def msgcheck_parser():
5151 """Return a command line parser for msgcheck (argparse.ArgumentParser)."""
5252 parser = argparse .ArgumentParser (
5353 formatter_class = argparse .RawDescriptionHelpFormatter ,
54- fromfile_prefix_chars = '@' ,
55- description = ' Gettext file checker.' ,
56- epilog = '''
54+ fromfile_prefix_chars = "@" ,
55+ description = " Gettext file checker." ,
56+ epilog = """
5757Environment variable "MSGCHECK_OPTIONS" can be set with default options.
5858Argument "@file.txt" can be used to read default options in a file.
5959
6060The script returns:
6161 0: all files checked are OK (or one of these options given:
6262 --extract, --only-misspelled or --ignore-errors given)
6363 n: number of files with errors (1 ≤ n ≤ 255)
64- ''' )
65- parser .add_argument ('-c' , '--no-compile' , action = 'store_true' ,
66- help = 'do not check compilation of file' )
67- parser .add_argument ('-f' , '--fuzzy' , action = 'store_true' ,
68- help = 'check fuzzy strings' )
69- parser .add_argument ('-n' , '--check-noqa' , action = 'store_true' ,
70- help = 'check "noqa"-commented lines (they are skipped '
71- 'by default)' )
72- parser .add_argument ('-l' , '--no-lines' , action = 'store_true' ,
73- help = 'do not check number of lines' )
74- parser .add_argument ('-p' , '--no-punct' , action = 'store_true' ,
75- help = 'do not check punctuation at end of strings' )
76- parser .add_argument ('-s' , '--spelling' , choices = ['id' , 'str' ],
77- help = 'check spelling' )
78- parser .add_argument ('-d' , '--dicts' ,
79- help = 'comma-separated list of extra dictionaries '
80- 'to use (in addition to file language)' )
81- parser .add_argument ('-P' , '--pwl' , action = 'append' ,
82- help = 'file(s) with personal list of words used when '
83- 'checking spelling (this option can be given multiple '
84- 'times)' )
85- parser .add_argument ('-m' , '--only-misspelled' , action = 'store_true' ,
86- help = 'display only misspelled words (no error, '
87- 'line number and translation)' )
88- parser .add_argument ('-w' , '--no-whitespace' , action = 'store_true' ,
89- help = 'do not check whitespace at beginning/end of '
90- 'strings' )
91- parser .add_argument ('-W' , '--no-whitespace-eol' , action = 'store_true' ,
92- help = 'do not check trailing whitespace at end of '
93- 'lines inside strings' )
94- parser .add_argument ('-e' , '--extract' , action = 'store_true' ,
95- help = 'display all translations and exit '
96- '(all checks except compilation are disabled in '
97- 'this mode)' )
98- parser .add_argument ('-i' , '--ignore-errors' , action = 'store_true' ,
99- help = 'display but ignore errors (always return 0)' )
100- parser .add_argument ('-q' , '--quiet' , action = 'store_true' ,
101- help = 'quiet mode: only display number of errors' )
102- parser .add_argument ('-v' , '--version' , action = 'version' ,
103- version = msgcheck_version ())
104- parser .add_argument ('file' , nargs = '+' ,
105- help = 'gettext file(s) to check (*.po files)' )
64+ """ ,
65+ )
66+ parser .add_argument (
67+ "-c" ,
68+ "--no-compile" ,
69+ action = "store_true" ,
70+ help = "do not check compilation of file" ,
71+ )
72+ parser .add_argument (
73+ "-f" , "--fuzzy" , action = "store_true" , help = "check fuzzy strings"
74+ )
75+ parser .add_argument (
76+ "-n" ,
77+ "--check-noqa" ,
78+ action = "store_true" ,
79+ help = "check \" noqa\" -commented lines (they are skipped by default)" ,
80+ )
81+ parser .add_argument (
82+ "-l" ,
83+ "--no-lines" ,
84+ action = "store_true" ,
85+ help = "do not check number of lines" ,
86+ )
87+ parser .add_argument (
88+ "-p" ,
89+ "--no-punct" ,
90+ action = "store_true" ,
91+ help = "do not check punctuation at end of strings" ,
92+ )
93+ parser .add_argument (
94+ "-s" , "--spelling" , choices = ["id" , "str" ], help = "check spelling"
95+ )
96+ parser .add_argument (
97+ "-d" ,
98+ "--dicts" ,
99+ help = "comma-separated list of extra dictionaries "
100+ "to use (in addition to file language)" ,
101+ )
102+ parser .add_argument (
103+ "-P" ,
104+ "--pwl" ,
105+ action = "append" ,
106+ help = "file(s) with personal list of words used when "
107+ "checking spelling (this option can be given multiple "
108+ "times)" ,
109+ )
110+ parser .add_argument (
111+ "-m" ,
112+ "--only-misspelled" ,
113+ action = "store_true" ,
114+ help = "display only misspelled words (no error, "
115+ "line number and translation)" ,
116+ )
117+ parser .add_argument (
118+ "-w" ,
119+ "--no-whitespace" ,
120+ action = "store_true" ,
121+ help = "do not check whitespace at beginning/end of " "strings" ,
122+ )
123+ parser .add_argument (
124+ "-W" ,
125+ "--no-whitespace-eol" ,
126+ action = "store_true" ,
127+ help = "do not check trailing whitespace at end of "
128+ "lines inside strings" ,
129+ )
130+ parser .add_argument (
131+ "-e" ,
132+ "--extract" ,
133+ action = "store_true" ,
134+ help = "display all translations and exit "
135+ "(all checks except compilation are disabled in "
136+ "this mode)" ,
137+ )
138+ parser .add_argument (
139+ "-i" ,
140+ "--ignore-errors" ,
141+ action = "store_true" ,
142+ help = "display but ignore errors (always return 0)" ,
143+ )
144+ parser .add_argument (
145+ "-q" ,
146+ "--quiet" ,
147+ action = "store_true" ,
148+ help = "quiet mode: only display number of errors" ,
149+ )
150+ parser .add_argument (
151+ "-v" , "--version" , action = "version" , version = msgcheck_version ()
152+ )
153+ parser .add_argument (
154+ "file" , nargs = "+" , help = "gettext file(s) to check (*.po files)"
155+ )
106156 return parser
107157
108158
@@ -112,26 +162,35 @@ def msgcheck_args(parser):
112162 parser .print_help ()
113163 sys .exit (1 )
114164 return parser .parse_args (
115- shlex .split (os .getenv ('MSGCHECK_OPTIONS' ) or '' ) + sys .argv [1 :])
165+ shlex .split (os .getenv ("MSGCHECK_OPTIONS" ) or "" ) + sys .argv [1 :]
166+ )
116167
117168
118169def msgcheck_check_files (args ):
119170 """Check files."""
120171 # create checker and set boolean options
121172 po_check = PoCheck ()
122- for option in ('no_compile' , 'fuzzy' , 'check_noqa' , 'no_lines' ,
123- 'no_punct' , 'no_whitespace' , 'no_whitespace_eol' ,
124- 'extract' ):
173+ for option in (
174+ "no_compile" ,
175+ "fuzzy" ,
176+ "check_noqa" ,
177+ "no_lines" ,
178+ "no_punct" ,
179+ "no_whitespace" ,
180+ "no_whitespace_eol" ,
181+ "extract" ,
182+ ):
125183 if args .__dict__ [option ]:
126- po_check .set_check (option .lstrip ('no_' ),
127- not option .startswith ('no_' ))
184+ po_check .set_check (
185+ option .lstrip ("no_" ), not option .startswith ("no_" )
186+ )
128187
129188 # check all files
130189 try :
131190 po_check .set_spelling_options (args .spelling , args .dicts , args .pwl )
132191 result = po_check .check_files (args .file )
133192 except (ImportError , IOError ) as exc :
134- print (' FATAL:' , exc , sep = ' ' )
193+ print (" FATAL:" , exc , sep = " " )
135194 sys .exit (1 )
136195
137196 return result
@@ -152,18 +211,18 @@ def msgcheck_display_errors(args, result):
152211 for report in reports :
153212 for word in report .get_misspelled_words ():
154213 words .append (word )
155- print ('\n ' .join (sorted (set (words ),
156- key = lambda s : s .lower ())))
214+ print ("\n " .join (sorted (set (words ), key = lambda s : s .lower ())))
157215 else :
158- print (' \n ' .join ([str (report ) for report in reports ]))
216+ print (" \n " .join ([str (report ) for report in reports ]))
159217 return files_ok , files_with_errors , total_errors
160218
161219
162220def msgcheck_display_result (args , result ):
163221 """Display result and return the number of files with errors."""
164222 # display errors
165- files_ok , files_with_errors , total_errors = \
166- msgcheck_display_errors (args , result )
223+ files_ok , files_with_errors , total_errors = msgcheck_display_errors (
224+ args , result
225+ )
167226
168227 # exit now if we extracted translations or if we displayed only
169228 # misspelled words
@@ -172,23 +231,25 @@ def msgcheck_display_result(args, result):
172231
173232 # display files with number of errors
174233 if total_errors > 0 and not args .quiet :
175- print ('=' * 70 )
234+ print ("=" * 70 )
176235 for filename , reports in result :
177236 errors = len (reports )
178237 if errors == 0 :
179- print (f' { filename } : OK' )
238+ print (f" { filename } : OK" )
180239 else :
181- result = ' almost good!' if errors <= 10 else ' uh oh... try again!'
182- print (f' { filename } : { errors } errors ({ result } )' )
240+ result = " almost good!" if errors <= 10 else " uh oh... try again!"
241+ print (f" { filename } : { errors } errors ({ result } )" )
183242
184243 # display total (if many files processed)
185244 if len (args .file ) > 1 :
186- print (' ---' )
245+ print (" ---" )
187246 if files_with_errors == 0 :
188- print (f' TOTAL: { files_ok } files OK' )
247+ print (f" TOTAL: { files_ok } files OK" )
189248 else :
190- print (f'TOTAL: { files_ok } files OK, { files_with_errors } files '
191- f'with { total_errors } errors' )
249+ print (
250+ f"TOTAL: { files_ok } files OK, { files_with_errors } files "
251+ f"with { total_errors } errors"
252+ )
192253
193254 return files_with_errors
194255
0 commit comments