Skip to content

Commit b1798df

Browse files
committed
Refactor extract messages
1 parent be651b8 commit b1798df

File tree

2 files changed

+67
-66
lines changed

2 files changed

+67
-66
lines changed

babel/messages/frontend.py

Lines changed: 27 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -222,56 +222,35 @@ def finalize_options(self):
222222

223223
def run(self):
224224
mappings = self._get_mappings()
225-
outfile = open(self.output_file, 'wb')
225+
catalog = Catalog(project=self.distribution.get_name(),
226+
version=self.distribution.get_version(),
227+
msgid_bugs_address=self.msgid_bugs_address,
228+
copyright_holder=self.copyright_holder,
229+
charset=self.charset)
230+
226231
try:
227-
catalog = Catalog(project=self.distribution.get_name(),
228-
version=self.distribution.get_version(),
229-
msgid_bugs_address=self.msgid_bugs_address,
230-
copyright_holder=self.copyright_holder,
231-
charset=self.charset)
232-
233-
for dirname, (method_map, options_map) in mappings.items():
234-
def callback(filename, method, options):
235-
if method == 'ignore':
236-
return
237-
filepath = os.path.normpath(os.path.join(dirname, filename))
238-
optstr = ''
239-
if options:
240-
optstr = ' (%s)' % ', '.join(['%s="%s"' % (k, v) for
241-
k, v in options.items()])
242-
log.info('extracting messages from %s%s', filepath, optstr)
243-
244-
extracted = extract_from_dir(dirname, method_map, options_map,
245-
keywords=self._keywords,
246-
comment_tags=self._add_comments,
247-
callback=callback,
248-
strip_comment_tags=
249-
self.strip_comments)
250-
for filename, lineno, message, comments, context in extracted:
251-
filepath = os.path.normpath(os.path.join(dirname, filename))
252-
catalog.add(message, None, [(filepath, lineno)],
253-
auto_comments=comments, context=context)
232+
operations.extract_to_catalog(catalog, mappings, self._keywords,
233+
self._add_comments,
234+
self.strip_comments, log=log)
235+
except operations.ConfigureError as e:
236+
raise DistutilsOptionError(e.message)
254237

238+
with open(self.output_file, 'wb') as outfile:
255239
log.info('writing PO template file to %s' % self.output_file)
256240
write_po(outfile, catalog, width=self.width,
257241
no_location=self.no_location,
258242
omit_header=self.omit_header,
259243
sort_output=self.sort_output,
260244
sort_by_file=self.sort_by_file)
261-
finally:
262-
outfile.close()
263245

264246
def _get_mappings(self):
265-
mappings = {}
247+
mappings = []
266248

267249
if self.mapping_file:
268-
fileobj = open(self.mapping_file, 'U')
269-
try:
250+
with open(self.mapping_file, 'U') as fileobj:
270251
method_map, options_map = parse_mapping(fileobj)
271252
for dirname in self.input_dirs:
272-
mappings[dirname] = method_map, options_map
273-
finally:
274-
fileobj.close()
253+
mappings.append((dirname, method_map, options_map))
275254

276255
elif getattr(self.distribution, 'message_extractors', None):
277256
message_extractors = self.distribution.message_extractors
@@ -283,11 +262,11 @@ def _get_mappings(self):
283262
for pattern, method, options in mapping:
284263
method_map.append((pattern, method))
285264
options_map[pattern] = options or {}
286-
mappings[dirname] = method_map, options_map
265+
mappings.append((dirname, method_map, options_map))
287266

288267
else:
289268
for dirname in self.input_dirs:
290-
mappings[dirname] = DEFAULT_MAPPING, {}
269+
mappings.append((dirname, DEFAULT_MAPPING, {}))
291270

292271
return mappings
293272

@@ -686,11 +665,8 @@ def extract(self, argv):
686665
keywords.update(parse_keywords(options.keywords))
687666

688667
if options.mapping_file:
689-
fileobj = open(options.mapping_file, 'U')
690-
try:
668+
with open(options.mapping_file, 'U') as fileobj:
691669
method_map, options_map = parse_mapping(fileobj)
692-
finally:
693-
fileobj.close()
694670
else:
695671
method_map = DEFAULT_MAPPING
696672
options_map = {}
@@ -710,30 +686,15 @@ def extract(self, argv):
710686
copyright_holder=options.copyright_holder,
711687
charset=options.charset)
712688

713-
for dirname in args:
714-
if not os.path.isdir(dirname):
715-
parser.error('%r is not a directory' % dirname)
716-
717-
def callback(filename, method, options):
718-
if method == 'ignore':
719-
return
720-
filepath = os.path.normpath(os.path.join(dirname, filename))
721-
optstr = ''
722-
if options:
723-
optstr = ' (%s)' % ', '.join(['%s="%s"' % (k, v) for
724-
k, v in options.items()])
725-
self.log.info('extracting messages from %s%s', filepath,
726-
optstr)
727-
728-
extracted = extract_from_dir(dirname, method_map, options_map,
729-
keywords, options.comment_tags,
730-
callback=callback,
731-
strip_comment_tags=
732-
options.strip_comment_tags)
733-
for filename, lineno, message, comments, context in extracted:
734-
filepath = os.path.normpath(os.path.join(dirname, filename))
735-
catalog.add(message, None, [(filepath, lineno)],
736-
auto_comments=comments, context=context)
689+
data_iter = ((dirname, method_map, options_map) for dirname in args)
690+
691+
try:
692+
operations.extract_to_catalog(catalog, data_iter, keywords,
693+
options.comment_tags,
694+
options.strip_comment_tags,
695+
log=self.log)
696+
except operations.ConfigureError as e:
697+
parser.error(e.message)
737698

738699
catalog_charset = catalog.charset
739700
if options.output not in (None, '-'):

babel/messages/operations.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from babel.messages.pofile import read_po, write_po
1919
from babel.messages.mofile import write_mo
20+
from babel.messages.extract import extract_from_dir
2021
from babel.util import LOCALTZ
2122
from babel import Locale
2223

@@ -225,3 +226,42 @@ def update_catalog(input_file, output_dir=None, output_file=None, locale=None,
225226
update_po_files(input_file, po_files, domain=domain, no_fuzzy=no_fuzzy,
226227
ignore_obsolete=ignore_obsolete, previous=previous,
227228
width=width, log=log)
229+
230+
231+
def _gen_optstr(options):
232+
optstr = ''
233+
if options:
234+
opt_couplestr = ('%s="%s"' % (k, v) for k, v in options.iteritems())
235+
optstr = ' (%s)' % ', '.join(opt_couplestr)
236+
return optstr
237+
238+
239+
def _gen_filepath(dirname, filename):
240+
return os.path.normpath(os.path.join(dirname, filename))
241+
242+
243+
def extract_to_catalog(catalog, data_iterator, keywords, comment_tags,
244+
strip_comment_tags, log=log):
245+
246+
for dirname, method_map, options_map in data_iterator:
247+
if not os.path.isdir(dirname):
248+
raise ConfigureError('%r is not a directory' % dirname)
249+
250+
def callback(filename, method, options):
251+
if method == 'ignore':
252+
return
253+
filepath = _gen_filepath(dirname, filename)
254+
optstr = _gen_optstr(options)
255+
log.info('extracting messages from %s%s', filepath, optstr)
256+
257+
extracted = extract_from_dir(dirname, method_map, options_map,
258+
keywords=keywords,
259+
comment_tags=comment_tags,
260+
callback=callback,
261+
strip_comment_tags=strip_comment_tags)
262+
263+
# Add extracted strings to catalog
264+
for filename, lineno, message, comments, context in extracted:
265+
filepath = _gen_filepath(dirname, filename)
266+
catalog.add(message, None, [(filepath, lineno)],
267+
auto_comments=comments, context=context)

0 commit comments

Comments
 (0)