Fixed #23840 -- Fixed makemessages find_files method

Changed the handling of extensions to be used for gettext. Now
obeying the --extension argument. find_files now only find the
given or default extensions and puts only these in the
TranslatableFiles. As a result there are no more confusing messages
for filetypes (extension) not handled by makemessages.
This commit is contained in:
Ilja Maas 2014-11-16 22:16:41 +01:00 committed by Claude Paroz
parent 580e9d0045
commit bb4a92d784
2 changed files with 32 additions and 7 deletions

View File

@ -61,8 +61,8 @@ class TranslatableFile(object):
if command.verbosity > 1: if command.verbosity > 1:
command.stdout.write('processing file %s in %s\n' % (self.file, self.dirpath)) command.stdout.write('processing file %s in %s\n' % (self.file, self.dirpath))
_, file_ext = os.path.splitext(self.file) file_ext = os.path.splitext(self.file)[1]
if domain == 'djangojs' and file_ext in command.extensions: if domain == 'djangojs':
orig_file = os.path.join(self.dirpath, self.file) orig_file = os.path.join(self.dirpath, self.file)
work_file = orig_file work_file = orig_file
is_templatized = command.gettext_version < (0, 18, 3) is_templatized = command.gettext_version < (0, 18, 3)
@ -85,10 +85,10 @@ class TranslatableFile(object):
'--output=-' '--output=-'
] + command.xgettext_options ] + command.xgettext_options
args.append(work_file) args.append(work_file)
elif domain == 'django' and (file_ext == '.py' or file_ext in command.extensions): elif domain == 'django':
orig_file = os.path.join(self.dirpath, self.file) orig_file = os.path.join(self.dirpath, self.file)
work_file = orig_file work_file = orig_file
is_templatized = file_ext in command.extensions is_templatized = file_ext != '.py'
if is_templatized: if is_templatized:
with io.open(orig_file, encoding=settings.FILE_CHARSET) as fp: with io.open(orig_file, encoding=settings.FILE_CHARSET) as fp:
src_data = fp.read() src_data = fp.read()
@ -250,8 +250,8 @@ class Command(BaseCommand):
if self.domain == 'djangojs': if self.domain == 'djangojs':
exts = extensions if extensions else ['js'] exts = extensions if extensions else ['js']
else: else:
exts = extensions if extensions else ['html', 'txt'] exts = extensions if extensions else ['html', 'txt', 'py']
self.extensions = handle_extensions(exts) self.extensions = handle_extensions(exts, ignored=())
if (locale is None and not exclude and not process_all) or self.domain is None: if (locale is None and not exclude and not process_all) or self.domain is None:
raise CommandError("Type '%s help %s' for usage information." % ( raise CommandError("Type '%s help %s' for usage information." % (
@ -394,7 +394,8 @@ class Command(BaseCommand):
self.locale_paths.insert(0, os.path.join(os.path.abspath(dirpath), dirname)) self.locale_paths.insert(0, os.path.join(os.path.abspath(dirpath), dirname))
for filename in filenames: for filename in filenames:
file_path = os.path.normpath(os.path.join(dirpath, filename)) file_path = os.path.normpath(os.path.join(dirpath, filename))
if is_ignored(file_path, self.ignore_patterns): file_ext = os.path.splitext(filename)[1]
if not (file_ext in self.extensions) or is_ignored(file_path, self.ignore_patterns):
if self.verbosity > 1: if self.verbosity > 1:
self.stdout.write('ignoring file %s in %s\n' % (filename, dirpath)) self.stdout.write('ignoring file %s in %s\n' % (filename, dirpath))
else: else:

View File

@ -344,6 +344,30 @@ class BasicExtractorTests(ExtractorTests):
self.assertMsgId("Translatable literal #9j", po_contents) self.assertMsgId("Translatable literal #9j", po_contents)
def test_makemessages_find_files(self):
"""
Test that find_files only discover files having the proper extensions.
"""
from django.core.management.commands.makemessages import Command
cmd = Command()
cmd.ignore_patterns = ['CVS', '.*', '*~', '*.pyc']
cmd.symlinks = False
cmd.domain = 'django'
cmd.extensions = ['html', 'txt', 'py']
cmd.verbosity = 0
cmd.locale_paths = []
cmd.default_locale_path = os.path.join(self.test_dir, 'locale')
found_files = cmd.find_files(self.test_dir)
found_exts = set([os.path.splitext(tfile.file)[1] for tfile in found_files])
self.assertEqual(found_exts.difference({'.py', '.html', '.txt'}), set())
cmd.extensions = ['js']
cmd.domain = 'djangojs'
found_files = cmd.find_files(self.test_dir)
found_exts = set([os.path.splitext(tfile.file)[1] for tfile in found_files])
self.assertEqual(found_exts.difference({'.js'}), set())
class JavascriptExtractorTests(ExtractorTests): class JavascriptExtractorTests(ExtractorTests):
PO_FILE = 'locale/%s/LC_MESSAGES/djangojs.po' % LOCALE PO_FILE = 'locale/%s/LC_MESSAGES/djangojs.po' % LOCALE