Skip to content

Commit 6fc7d0c

Browse files
committed
Slightly tidy up frontend.py
1 parent 4039563 commit 6fc7d0c

1 file changed

Lines changed: 39 additions & 32 deletions

File tree

babel/messages/frontend.py

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,34 @@
99
:license: BSD, see LICENSE for more details.
1010
"""
1111
from __future__ import print_function
12-
try:
13-
from ConfigParser import RawConfigParser
14-
except ImportError:
15-
from configparser import RawConfigParser
16-
from datetime import datetime
17-
from distutils import log as distutils_log
18-
from distutils.cmd import Command as _Command
19-
from distutils.errors import DistutilsOptionError, DistutilsSetupError
20-
from locale import getpreferredencoding
12+
2113
import logging
22-
from optparse import OptionParser
14+
import optparse
2315
import os
2416
import re
2517
import shutil
2618
import sys
2719
import tempfile
20+
from datetime import datetime
21+
from locale import getpreferredencoding
2822

2923
from babel import __version__ as VERSION
3024
from babel import Locale, localedata
25+
from babel._compat import StringIO, string_types
3126
from babel.core import UnknownLocaleError
3227
from babel.messages.catalog import Catalog
33-
from babel.messages.extract import extract_from_dir, DEFAULT_KEYWORDS, \
34-
DEFAULT_MAPPING
28+
from babel.messages.extract import DEFAULT_KEYWORDS, DEFAULT_MAPPING, extract_from_dir
3529
from babel.messages.mofile import write_mo
3630
from babel.messages.pofile import read_po, write_po
37-
from babel.util import odict, LOCALTZ
38-
from babel._compat import string_types, StringIO, PY2
31+
from babel.util import LOCALTZ, odict
32+
from distutils import log as distutils_log
33+
from distutils.cmd import Command as _Command
34+
from distutils.errors import DistutilsOptionError, DistutilsSetupError
35+
36+
try:
37+
from ConfigParser import RawConfigParser
38+
except ImportError:
39+
from configparser import RawConfigParser
3940

4041

4142
class Command(_Command):
@@ -162,21 +163,24 @@ def run(self):
162163
translated = 0
163164
for message in list(catalog)[1:]:
164165
if message.string:
165-
translated +=1
166+
translated += 1
166167
percentage = 0
167168
if len(catalog):
168169
percentage = translated * 100 // len(catalog)
169-
self.log.info('%d of %d messages (%d%%) translated in %r',
170-
translated, len(catalog), percentage, po_file)
170+
self.log.info(
171+
'%d of %d messages (%d%%) translated in %r',
172+
translated, len(catalog), percentage, po_file
173+
)
171174

172175
if catalog.fuzzy and not self.use_fuzzy:
173176
self.log.info('catalog %r is marked as fuzzy, skipping', po_file)
174177
continue
175178

176179
for message, errors in catalog.check():
177180
for error in errors:
178-
self.log.error('error: %s:%d: %s', po_file, message.lineno,
179-
error)
181+
self.log.error(
182+
'error: %s:%d: %s', po_file, message.lineno, error
183+
)
180184

181185
self.log.info('compiling catalog %r to %r', po_file, mo_file)
182186

@@ -300,7 +304,8 @@ def finalize_options(self):
300304
if isinstance(self.input_dirs, string_types):
301305
self.input_dirs = re.split(',\s*', self.input_dirs)
302306
else:
303-
self.input_dirs = dict.fromkeys([k.split('.',1)[0]
307+
self.input_dirs = dict.fromkeys([
308+
k.split('.', 1)[0]
304309
for k in (self.distribution.packages or ())
305310
]).keys()
306311

@@ -339,12 +344,13 @@ def callback(filename, method, options):
339344
k, v in options.items()])
340345
self.log.info('extracting messages from %s%s', filepath, optstr)
341346

342-
extracted = extract_from_dir(dirname, method_map, options_map,
343-
keywords=self._keywords,
344-
comment_tags=self.add_comments,
345-
callback=callback,
346-
strip_comment_tags=
347-
self.strip_comments)
347+
extracted = extract_from_dir(
348+
dirname, method_map, options_map,
349+
keywords=self._keywords,
350+
comment_tags=self.add_comments,
351+
callback=callback,
352+
strip_comment_tags=self.strip_comments
353+
)
348354
for filename, lineno, message, comments, context in extracted:
349355
filepath = os.path.normpath(os.path.join(dirname, filename))
350356
catalog.add(message, None, [(filepath, lineno)],
@@ -477,8 +483,9 @@ def finalize_options(self):
477483
self.width = int(self.width)
478484

479485
def run(self):
480-
self.log.info('creating catalog %r based on %r', self.output_file,
481-
self.input_file)
486+
self.log.info(
487+
'creating catalog %r based on %r', self.output_file, self.input_file
488+
)
482489

483490
infile = open(self.input_file, 'rb')
484491
try:
@@ -674,8 +681,8 @@ def run(self, argv=None):
674681
if argv is None:
675682
argv = sys.argv
676683

677-
self.parser = OptionParser(usage=self.usage % ('command', '[args]'),
678-
version=self.version)
684+
self.parser = optparse.OptionParser(usage=self.usage % ('command', '[args]'),
685+
version=self.version)
679686
self.parser.disable_interspersed_args()
680687
self.parser.print_help = self._help
681688
self.parser.add_option('--list-locales', dest='list_locales',
@@ -750,7 +757,7 @@ def _dispatch(self, cmdname, argv):
750757
assert isinstance(cmdinst, Command)
751758
cmdinst.initialize_options()
752759

753-
parser = OptionParser(
760+
parser = optparse.OptionParser(
754761
usage=self.usage % (cmdname, ''),
755762
description=self.commands[cmdname]
756763
)
@@ -843,7 +850,7 @@ def parse_mapping(fileobj, filename=None):
843850
options_map = {}
844851

845852
parser = RawConfigParser()
846-
parser._sections = odict(parser._sections) # We need ordered sections
853+
parser._sections = odict(parser._sections) # We need ordered sections
847854
parser.readfp(fileobj, filename)
848855
for section in parser.sections():
849856
if section == 'extractors':

0 commit comments

Comments
 (0)