Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Lib/test/test_tools/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,22 @@ def test_parse_keyword_spec(self):
parse_spec(spec)
self.assertEqual(str(cm.exception), message)

def test_omit_header(self):
"""Test that --omit-header removes the header."""
data = self.extract_from_str(dedent('''\
_("foo")
'''), args=('--omit-header',), raw=True)

self.assertNotIn("Project-Id-Version", data)
self.assertNotIn("POT-Creation-Date", data)
self.assertNotIn("PO-Revision-Date", data)
self.assertNotIn("Last-Translator", data)
self.assertNotIn("Language-Team", data)
self.assertNotIn("MIME-Version", data)
self.assertNotIn("Content-Type", data)
self.assertNotIn("Content-Transfer-Encoding", data)
self.assertNotIn("Generated-By", data)


def extract_from_snapshots():
snapshots = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``--omit-header`` option to :program:`pygettext`.
22 changes: 18 additions & 4 deletions Tools/i18n/pygettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@
--width=columns
Set width of output to columns.

--omit-header
Don’t write header to file.

This is useful for testing purposes because it eliminates a source of
variance for generated .gmo files.

Note: Using this option will lead to an error if the resulting file
would not entirely be in ASCII.

-x filename
--exclude-file=filename
Specify a file that contains a list of strings that are not be
Expand Down Expand Up @@ -574,9 +583,11 @@ def _is_string_const(self, node):
def write_pot_file(messages, options, fp):
timestamp = time.strftime('%Y-%m-%d %H:%M%z')
encoding = fp.encoding if fp.encoding else 'UTF-8'
print(pot_header % {'time': timestamp, 'version': __version__,
'charset': encoding,
'encoding': '8bit'}, file=fp)

if not options.omit_header:
print(pot_header % {'time': timestamp, 'version': __version__,
'charset': encoding,
'encoding': '8bit'}, file=fp)

# Sort locations within each message by filename and lineno
sorted_keys = [
Expand Down Expand Up @@ -636,7 +647,7 @@ def main():
['extract-all', 'add-comments=?', 'default-domain=', 'escape',
'help', 'keyword=', 'no-default-keywords',
'add-location', 'no-location', 'output=', 'output-dir=',
'style=', 'verbose', 'version', 'width=', 'exclude-file=',
'style=', 'verbose', 'version', 'width=', 'omit-header', 'exclude-file=',
'docstrings', 'no-docstrings',
])
except getopt.error as msg:
Expand All @@ -657,6 +668,7 @@ class Options:
locationstyle = GNU
verbose = 0
width = 78
omit_header = False
excludefilename = ''
docstrings = 0
nodocstrings = {}
Expand Down Expand Up @@ -709,6 +721,8 @@ class Options:
options.width = int(arg)
except ValueError:
usage(1, f'--width argument must be an integer: {arg}')
elif opt in ('--omit-header',):
options.omit_header = True
elif opt in ('-x', '--exclude-file'):
options.excludefilename = arg
elif opt in ('-X', '--no-docstrings'):
Expand Down
Loading