summaryrefslogtreecommitdiff
path: root/babel
diff options
context:
space:
mode:
authorAarni Koskela <akx@iki.fi>2016-11-21 10:43:23 +0200
committerGitHub <noreply@github.com>2016-11-21 10:43:23 +0200
commit15365e2cc99d0fe05b90aeb2fddec44c6ffef762 (patch)
tree75daf0e610425f8f687551ec6f30aca3056575d7 /babel
parent9af0821eb47e135faa18b3da143096eea301160a (diff)
parent2ff0f4843d427102260fe88ddab67ce6d95903bd (diff)
downloadbabel-15365e2cc99d0fe05b90aeb2fddec44c6ffef762.tar.gz
Merge pull request #438 from rrader/add_location_option
Add option 'add_location' for location line formatting
Diffstat (limited to 'babel')
-rw-r--r--babel/messages/frontend.py27
-rw-r--r--babel/messages/pofile.py5
2 files changed, 27 insertions, 5 deletions
diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py
index 3c94bf3..da4323c 100644
--- a/babel/messages/frontend.py
+++ b/babel/messages/frontend.py
@@ -104,6 +104,10 @@ class Command(_Command):
#: that are usable with optparse.
option_aliases = {}
+ #: Choices for options that needed to be restricted to specific
+ #: list of choices.
+ option_choices = {}
+
#: Log object. To allow replacement in the script command line runner.
log = distutils_log
@@ -273,6 +277,11 @@ class extract_messages(Command):
'path to the mapping configuration file'),
('no-location', None,
'do not include location comments with filename and line number'),
+ ('add-location', None,
+ 'location lines format. If it is not given or "full", it generates '
+ 'the lines with both file name and line number. If it is "file", '
+ 'the line number part is omitted. If it is "never", it completely '
+ 'suppresses the lines (same as --no-location).'),
('omit-header', None,
'do not include msgid "" entry in header'),
('output-file=', 'o',
@@ -317,6 +326,9 @@ class extract_messages(Command):
'output-file': ('--output',),
'strip-comments': ('--strip-comment-tags',),
}
+ option_choices = {
+ 'add-location': ('full', 'file', 'never',),
+ }
def initialize_options(self):
self.charset = 'utf-8'
@@ -324,6 +336,7 @@ class extract_messages(Command):
self.no_default_keywords = False
self.mapping_file = None
self.no_location = False
+ self.add_location = None
self.omit_header = False
self.output_file = None
self.input_dirs = None
@@ -338,6 +351,7 @@ class extract_messages(Command):
self.version = None
self.add_comments = None
self.strip_comments = False
+ self.include_lineno = True
def finalize_options(self):
if self.input_dirs:
@@ -401,6 +415,11 @@ class extract_messages(Command):
if not self.version:
self.version = self.distribution.get_version()
+ if self.add_location == 'never':
+ self.no_location = True
+ elif self.add_location == 'file':
+ self.include_lineno = False
+
def run(self):
mappings = self._get_mappings()
with open(self.output_file, 'wb') as outfile:
@@ -459,7 +478,8 @@ class extract_messages(Command):
no_location=self.no_location,
omit_header=self.omit_header,
sort_output=self.sort_output,
- sort_by_file=self.sort_by_file)
+ sort_by_file=self.sort_by_file,
+ include_lineno=self.include_lineno)
def _get_mappings(self):
mappings = []
@@ -859,14 +879,15 @@ class CommandLineInterface(object):
if short:
strs.append("-%s" % short)
strs.extend(cmdclass.option_aliases.get(name, ()))
+ choices = cmdclass.option_choices.get(name, None)
if name == as_args:
parser.usage += "<%s>" % name
elif name in cmdclass.boolean_options:
parser.add_option(*strs, action="store_true", help=help)
elif name in cmdclass.multiple_value_options:
- parser.add_option(*strs, action="append", help=help)
+ parser.add_option(*strs, action="append", help=help, choices=choices)
else:
- parser.add_option(*strs, help=help, default=default)
+ parser.add_option(*strs, help=help, default=default, choices=choices)
options, args = parser.parse_args(argv)
if as_args:
diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py
index e431fcd..741e25f 100644
--- a/babel/messages/pofile.py
+++ b/babel/messages/pofile.py
@@ -371,7 +371,7 @@ def normalize(string, prefix='', width=76):
def write_po(fileobj, catalog, width=76, no_location=False, omit_header=False,
sort_output=False, sort_by_file=False, ignore_obsolete=False,
- include_previous=False):
+ include_previous=False, include_lineno=True):
r"""Write a ``gettext`` PO (portable object) template file for a given
message catalog to the provided file-like object.
@@ -413,6 +413,7 @@ def write_po(fileobj, catalog, width=76, no_location=False, omit_header=False,
comments
:param include_previous: include the old msgid as a comment when
updating the catalog
+ :param include_lineno: include line number in the location comment
"""
def _normalize(key, prefix=''):
return normalize(key, prefix=prefix, width=width)
@@ -486,7 +487,7 @@ def write_po(fileobj, catalog, width=76, no_location=False, omit_header=False,
if not no_location:
locs = []
for filename, lineno in sorted(message.locations):
- if lineno:
+ if lineno and include_lineno:
locs.append(u'%s:%d' % (filename.replace(os.sep, '/'), lineno))
else:
locs.append(u'%s' % filename.replace(os.sep, '/'))