|
10 | 10 | """
|
11 | 11 | from __future__ import print_function
|
12 | 12 |
|
| 13 | +import fnmatch |
13 | 14 | import logging
|
14 | 15 | import optparse
|
15 | 16 | import os
|
@@ -256,6 +257,20 @@ def _run_domain(self, domain):
|
256 | 257 | return catalogs_and_errors
|
257 | 258 |
|
258 | 259 |
|
| 260 | +def _make_directory_filter(ignore_patterns): |
| 261 | + """ |
| 262 | + Build a directory_filter function based on a list of ignore patterns. |
| 263 | + """ |
| 264 | + def cli_directory_filter(dirname): |
| 265 | + basename = os.path.basename(dirname) |
| 266 | + return not any( |
| 267 | + fnmatch.fnmatch(basename, ignore_pattern) |
| 268 | + for ignore_pattern |
| 269 | + in ignore_patterns |
| 270 | + ) |
| 271 | + return cli_directory_filter |
| 272 | + |
| 273 | + |
259 | 274 | class extract_messages(Command):
|
260 | 275 | """Message extraction command for use in ``setup.py`` scripts.
|
261 | 276 |
|
@@ -320,13 +335,20 @@ class extract_messages(Command):
|
320 | 335 | 'files or directories with commas(,)'), # TODO: Support repetition of this argument
|
321 | 336 | ('input-dirs=', None, # TODO (3.x): Remove me.
|
322 | 337 | 'alias for input-paths (does allow files as well as directories).'),
|
| 338 | + ('ignore-dirs=', None, |
| 339 | + 'Patterns for directories to ignore when scanning for messages. ' |
| 340 | + 'Separate multiple patterns with spaces (default ".* ._")'), |
323 | 341 | ]
|
324 | 342 | boolean_options = [
|
325 | 343 | 'no-default-keywords', 'no-location', 'omit-header', 'no-wrap',
|
326 | 344 | 'sort-output', 'sort-by-file', 'strip-comments'
|
327 | 345 | ]
|
328 | 346 | as_args = 'input-paths'
|
329 |
| - multiple_value_options = ('add-comments', 'keywords') |
| 347 | + multiple_value_options = ( |
| 348 | + 'add-comments', |
| 349 | + 'keywords', |
| 350 | + 'ignore-dirs', |
| 351 | + ) |
330 | 352 | option_aliases = {
|
331 | 353 | 'keywords': ('--keyword',),
|
332 | 354 | 'mapping-file': ('--mapping',),
|
@@ -359,6 +381,7 @@ def initialize_options(self):
|
359 | 381 | self.add_comments = None
|
360 | 382 | self.strip_comments = False
|
361 | 383 | self.include_lineno = True
|
| 384 | + self.ignore_dirs = None |
362 | 385 |
|
363 | 386 | def finalize_options(self):
|
364 | 387 | if self.input_dirs:
|
@@ -427,6 +450,13 @@ def finalize_options(self):
|
427 | 450 | elif self.add_location == 'file':
|
428 | 451 | self.include_lineno = False
|
429 | 452 |
|
| 453 | + ignore_dirs = listify_value(self.ignore_dirs) |
| 454 | + if ignore_dirs: |
| 455 | + self.directory_filter = _make_directory_filter(self.ignore_dirs) |
| 456 | + else: |
| 457 | + self.directory_filter = None |
| 458 | + |
| 459 | + |
430 | 460 | def run(self):
|
431 | 461 | mappings = self._get_mappings()
|
432 | 462 | with open(self.output_file, 'wb') as outfile:
|
@@ -469,7 +499,8 @@ def callback(filename, method, options):
|
469 | 499 | keywords=self.keywords,
|
470 | 500 | comment_tags=self.add_comments,
|
471 | 501 | callback=callback,
|
472 |
| - strip_comment_tags=self.strip_comments |
| 502 | + strip_comment_tags=self.strip_comments, |
| 503 | + directory_filter=self.directory_filter, |
473 | 504 | )
|
474 | 505 | for filename, lineno, message, comments, context in extracted:
|
475 | 506 | if os.path.isfile(path):
|
|
0 commit comments