From b902900fa69e20e017905404904ce10964e39c8c Mon Sep 17 00:00:00 2001 From: stan Date: Thu, 27 Feb 2025 18:56:24 +0000 Subject: [PATCH 1/5] Add --omit-header to pygettext --- Lib/test/test_tools/test_i18n.py | 16 ++++++++++++++ ...-02-27-19-00-00.gh-issue-130647.uwda2h.rst | 1 + Tools/i18n/pygettext.py | 22 +++++++++++++++---- 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Tools-Demos/2025-02-27-19-00-00.gh-issue-130647.uwda2h.rst diff --git a/Lib/test/test_tools/test_i18n.py b/Lib/test/test_tools/test_i18n.py index d73fcff4c9cb11..7a70c251dc8e63 100644 --- a/Lib/test/test_tools/test_i18n.py +++ b/Lib/test/test_tools/test_i18n.py @@ -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 = { diff --git a/Misc/NEWS.d/next/Tools-Demos/2025-02-27-19-00-00.gh-issue-130647.uwda2h.rst b/Misc/NEWS.d/next/Tools-Demos/2025-02-27-19-00-00.gh-issue-130647.uwda2h.rst new file mode 100644 index 00000000000000..6155c071b2a344 --- /dev/null +++ b/Misc/NEWS.d/next/Tools-Demos/2025-02-27-19-00-00.gh-issue-130647.uwda2h.rst @@ -0,0 +1 @@ +Add ``--omit-header`` option to :program:`pygettext`. \ No newline at end of file diff --git a/Tools/i18n/pygettext.py b/Tools/i18n/pygettext.py index 0f5f32c7d6c18f..36d334f913364a 100755 --- a/Tools/i18n/pygettext.py +++ b/Tools/i18n/pygettext.py @@ -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 @@ -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 = [ @@ -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: @@ -657,6 +668,7 @@ class Options: locationstyle = GNU verbose = 0 width = 78 + omit_header = False excludefilename = '' docstrings = 0 nodocstrings = {} @@ -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'): From f15971a9cf5b3d72ec4389aa80ffee799126b13d Mon Sep 17 00:00:00 2001 From: stan Date: Thu, 27 Feb 2025 19:13:26 +0000 Subject: [PATCH 2/5] Add /n to fix lint --- .../Tools-Demos/2025-02-27-19-00-00.gh-issue-130647.uwda2h.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Tools-Demos/2025-02-27-19-00-00.gh-issue-130647.uwda2h.rst b/Misc/NEWS.d/next/Tools-Demos/2025-02-27-19-00-00.gh-issue-130647.uwda2h.rst index 6155c071b2a344..4e8c1e3f883fb5 100644 --- a/Misc/NEWS.d/next/Tools-Demos/2025-02-27-19-00-00.gh-issue-130647.uwda2h.rst +++ b/Misc/NEWS.d/next/Tools-Demos/2025-02-27-19-00-00.gh-issue-130647.uwda2h.rst @@ -1 +1 @@ -Add ``--omit-header`` option to :program:`pygettext`. \ No newline at end of file +Add ``--omit-header`` option to :program:`pygettext`. From 13927ad50adf9095aafbf0d174e5bc1987d845c1 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Sun, 9 Mar 2025 22:11:03 +0000 Subject: [PATCH 3/5] Tomas's suggestions --- Lib/test/test_tools/i18n_data/noheader.pot | 8 ++++++++ Lib/test/test_tools/i18n_data/noheader.py | 5 +++++ Lib/test/test_tools/test_i18n.py | 17 +---------------- Tools/i18n/pygettext.py | 8 ++++---- 4 files changed, 18 insertions(+), 20 deletions(-) create mode 100644 Lib/test/test_tools/i18n_data/noheader.pot create mode 100644 Lib/test/test_tools/i18n_data/noheader.py diff --git a/Lib/test/test_tools/i18n_data/noheader.pot b/Lib/test/test_tools/i18n_data/noheader.pot new file mode 100644 index 00000000000000..834006e42732e5 --- /dev/null +++ b/Lib/test/test_tools/i18n_data/noheader.pot @@ -0,0 +1,8 @@ +#: noheader.py:3 +msgid "Foo" +msgstr "" + +#: noheader.py:5 +msgid "Bar" +msgstr "" + diff --git a/Lib/test/test_tools/i18n_data/noheader.py b/Lib/test/test_tools/i18n_data/noheader.py new file mode 100644 index 00000000000000..23971799af23c6 --- /dev/null +++ b/Lib/test/test_tools/i18n_data/noheader.py @@ -0,0 +1,5 @@ +from gettext import gettext as _ + +_('Foo') + +_('Bar') diff --git a/Lib/test/test_tools/test_i18n.py b/Lib/test/test_tools/test_i18n.py index 7a70c251dc8e63..33b4f05184ea45 100644 --- a/Lib/test/test_tools/test_i18n.py +++ b/Lib/test/test_tools/test_i18n.py @@ -516,22 +516,6 @@ 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 = { @@ -542,6 +526,7 @@ def extract_from_snapshots(): 'custom_keywords.py': ('--keyword=foo', '--keyword=nfoo:1,2', '--keyword=pfoo:1c,2', '--keyword=npfoo:1c,2,3'), + 'noheader.py': ('--omit-header',), } for filename, args in snapshots.items(): diff --git a/Tools/i18n/pygettext.py b/Tools/i18n/pygettext.py index 36d334f913364a..fdf00de02513c7 100755 --- a/Tools/i18n/pygettext.py +++ b/Tools/i18n/pygettext.py @@ -125,13 +125,13 @@ Set width of output to columns. --omit-header - Don’t write header to file. + Do not write header to file. This is useful for testing purposes because it eliminates a source of - variance for generated .gmo files. + variance for generated .mo files. - Note: Using this option will lead to an error if the resulting file - would not entirely be in ASCII. + Note: Using this option will lead to an error during compilation or other + manipulation if the resulting file is not entirely in ASCII. -x filename --exclude-file=filename From 766b84744cec1f0d7c7343ac1dfc6d0f8811d20d Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Sun, 9 Mar 2025 22:47:36 +0000 Subject: [PATCH 4/5] Fix conflict fix --- Lib/test/test_tools/test_i18n.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_tools/test_i18n.py b/Lib/test/test_tools/test_i18n.py index 1e39fe3f32b9e8..963a43c4c55dfb 100644 --- a/Lib/test/test_tools/test_i18n.py +++ b/Lib/test/test_tools/test_i18n.py @@ -525,7 +525,6 @@ def extract_from_snapshots(): 'comments.py': ('--add-comments=i18n:',), 'custom_keywords.py': ('--keyword=foo', '--keyword=nfoo:1,2', '--keyword=pfoo:1c,2', - '--keyword=npfoo:1c,2,3'), '--keyword=npfoo:1c,2,3', '--keyword=_:1,2'), 'noheader.py': ('--omit-header',), } From 3b33dd977960037030042fb423f1088eefe3c1be Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 1 May 2025 16:46:54 +0300 Subject: [PATCH 5/5] Update Tools/i18n/pygettext.py Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> --- Tools/i18n/pygettext.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tools/i18n/pygettext.py b/Tools/i18n/pygettext.py index 359495dcdc71d3..37bda4473194f8 100755 --- a/Tools/i18n/pygettext.py +++ b/Tools/i18n/pygettext.py @@ -130,7 +130,7 @@ This is useful for testing purposes because it eliminates a source of variance for generated .mo files. - Note: Using this option will lead to an error during compilation or other + Note: Using this option may lead to an error during compilation or other manipulation if the resulting file is not entirely in ASCII. -x filename