diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py index e642a1a08..4a793af9e 100644 --- a/babel/messages/frontend.py +++ b/babel/messages/frontend.py @@ -47,6 +47,116 @@ log = logging.getLogger('babel') +def _parse_toml( + fileobj: BinaryIO, + *, + style: Literal["standalone", "pyproject.toml"], + filename: str = "(unknown)", + allow_missing_sections: bool = False, +) -> dict[str, Any]: + """Parse the Babel configuration from the given binary file object. + + :param fileobj: a readable file-like object containing the configuration text + to parse. + :param style: whether the file is in the style of a `pyproject.toml` file, + i.e. whether to look for `tool.babel`. + :param filename: the name of the file being parsed, for error messages. + :param allow_missing_sections: decides if a missing ``[tool.babel]`` section + results in an empty dictionary being returned or a + ``ConfigurationError`` being raised. + """ + try: + import tomllib + except ImportError: + try: + import tomli as tomllib + except ImportError as ie: # pragma: no cover + raise ImportError( + "tomli or tomllib is required to parse TOML files", + ) from ie + + try: + parsed_data = tomllib.load(fileobj) + except tomllib.TOMLDecodeError as e: + raise ConfigurationError(f"{filename}: Error parsing TOML file: {e}") from e + + if style == "pyproject.toml": + try: + babel_data = parsed_data["tool"]["babel"] + except (TypeError, KeyError) as e: + if not allow_missing_sections: + raise ConfigurationError( + f"{filename}: No 'tool.babel' section found in file", + ) from e + else: + return {} + + elif style == "standalone": + babel_data = parsed_data + if "babel" in babel_data: + raise ConfigurationError( + f"{filename}: 'babel' should not be present in a stand-alone configuration file", + ) + else: # pragma: no cover + raise ValueError(f"Unknown TOML style {style!r}") + + return babel_data + + +def _find_pyproject_toml( + path: pathlib.Path | None, max_depth: int = 10, +) -> pathlib.Path | None: + """Find the closest "pyproject.toml" file to the specified path. + + If the given path's directory contains a ``pyproject.toml``, return that. + Otherwise, walk up the directory hierarchy until one has been found or the + maximum search depth has been exhausted. + + :param path: The path to start the search for ``pyproject.toml`` from. + If set to ``None``, the current working directory will be used. + :param max_depth: The maximum number of steps up the file system for the search. + """ + if path is None: + path = pathlib.Path.cwd() + + path = path.resolve() # resolve symlinks and remove ".." components + if path.is_file(): + return path + + tries = 0 + while tries < max_depth: + pyproject_file = path / "pyproject.toml" + if pyproject_file.is_file(): + return pyproject_file + + parent = path.parent + if path == parent: + return None + + path = parent + tries += 1 + + return None + + +def _parse_pyproject_toml_config(path: pathlib.Path | None, table_name: str | None) -> dict: + """Parse the Babel config from a "pyproject.toml" file near the specified path.""" + if table_name is None: + return {} + + if path := _find_pyproject_toml(path): + with path.open("rb") as file_fd: + babel_data = _parse_toml( + file_fd, + style="pyproject.toml", + filename=path.name, + allow_missing_sections=True, + ) + return babel_data.get(table_name, {}) + else: + return {} + + class BaseError(Exception): pass @@ -134,6 +244,10 @@ class CommandMixin: #: list of choices. option_choices = {} + #: The name of the table containing the command's configuration in + #: a TOML file. + toml_config_table = None + #: Log object. To allow replacement in the script command line runner. log = log @@ -193,6 +307,7 @@ class CompileCatalog(CommandMixin): 'print statistics about translations'), ] # fmt: skip boolean_options = ['use-fuzzy', 'statistics'] + toml_config_table = "compile_catalog" def initialize_options(self): self.domain = 'messages' @@ -382,6 +497,7 @@ class ExtractMessages(CommandMixin): option_choices = { 'add-location': ('full', 'file', 'never'), } + toml_config_table = "extract_messages" def initialize_options(self): self.charset = 'utf-8' @@ -571,7 +687,7 @@ def _get_mappings(self): if os.path.basename(self.mapping_file) == "pyproject.toml" else "standalone" ) - method_map, options_map = _parse_mapping_toml( + method_map, options_map = _parse_mappings_from_toml( fileobj, filename=self.mapping_file, style=file_style, @@ -642,6 +758,7 @@ class InitCatalog(CommandMixin): 'into several lines'), ] # fmt: skip boolean_options = ['no-wrap'] + toml_config_table = "init_catalog" def initialize_options(self): self.output_dir = None @@ -734,6 +851,7 @@ class UpdateCatalog(CommandMixin): 'check', 'ignore-pot-creation-date', ] + toml_config_table = "update_catalog" def initialize_options(self): self.domain = 'messages' @@ -1245,6 +1363,34 @@ def _help(self): for name, description in sorted(self.commands.items()): print(f" {name:<{cmd_width}} {description}") + def _parse_config_from_pyproject_toml(self, cmdinst) -> dict: + pyproject_toml_path_override = os.getenv("BABEL_PYPROJECT_TOML", None) + + if pyproject_toml_path_override in ["none", "skip"]: + return {} + + elif pyproject_toml_path_override is None: + # The user is relying on default behavior; + # let's handle config parsing errors gracefully (by logging) + try: + return _parse_pyproject_toml_config(None, cmdinst.toml_config_table) + + except ImportError as ie: + self.log.error("Could not load TOML parsing library: %s", ie) + return {} + + except ConfigurationError as ce: + self.log.error("Unable to parse configuration: %s", ce) + return {} + + else: + # The user specified a pyproject.toml path explicitly; + # let's crash loudly in case of errors + return _parse_pyproject_toml_config( + pathlib.Path(pyproject_toml_path_override), cmdinst.toml_config_table, + ) + + def _configure_command(self, cmdname: str, argv: list[str]) -> CommandMixin: cmdclass = self.command_classes[cmdname] cmdinst = cmdclass() @@ -1253,6 +1399,7 @@ def _configure_command(self, cmdname: str, argv: list[str]) -> CommandMixin: assert isinstance(cmdinst, CommandMixin) cmdinst.initialize_options() + toml_config = self._parse_config_from_pyproject_toml(cmdinst) parser = optparse.OptionParser( usage=self.usage % (cmdname, ''), description=self.commands[cmdname], @@ -1260,7 +1407,8 @@ def _configure_command(self, cmdname: str, argv: list[str]) -> CommandMixin: as_args: str | None = getattr(cmdclass, "as_args", None) for long, short, help in cmdclass.user_options: name = long.strip("=") - default = getattr(cmdinst, name.replace("-", "_")) + normalized_name = name.replace("-", "_") + default = toml_config.get(normalized_name, getattr(cmdinst, normalized_name)) strs = [f"--{name}"] if short: strs.append(f"-{short}") @@ -1269,14 +1417,18 @@ def _configure_command(self, cmdname: str, argv: list[str]) -> CommandMixin: if name == as_args: parser.usage += f"<{name}>" elif name in cmdclass.boolean_options: - parser.add_option(*strs, action="store_true", help=help) + parser.add_option(*strs, action="store_true", default=default, help=help) elif name in cmdclass.multiple_value_options: - parser.add_option(*strs, action="append", help=help, choices=choices) + parser.add_option(*strs, action="append", default=listify_value(default), help=help, choices=choices) else: parser.add_option(*strs, help=help, default=default, choices=choices) + options, args = parser.parse_args(argv) if as_args: + # Read `args` from TOML; `as_args` is the key name, e.g. + # `input_paths = ...` in `[tool.babel.extract_messages]` + args = args or listify_value(toml_config.get(as_args.replace("-", "_"), args)) setattr(options, as_args.replace('-', '_'), args) for key, value in vars(options).items(): @@ -1438,7 +1590,7 @@ def _parse_config_object(config: dict, *, filename="(unknown)"): return method_map, options_map -def _parse_mapping_toml( +def _parse_mappings_from_toml( fileobj: BinaryIO, filename: str = "(unknown)", style: Literal["standalone", "pyproject.toml"] = "standalone", @@ -1451,36 +1603,8 @@ def _parse_mapping_toml( :param filename: the name of the file being parsed, for error messages :param style: whether the file is in the style of a `pyproject.toml` file, i.e. whether to look for `tool.babel`. """ - try: - import tomllib - except ImportError: - try: - import tomli as tomllib - except ImportError as ie: # pragma: no cover - raise ImportError("tomli or tomllib is required to parse TOML files") from ie - - try: - parsed_data = tomllib.load(fileobj) - except tomllib.TOMLDecodeError as e: - raise ConfigurationError(f"{filename}: Error parsing TOML file: {e}") from e - - if style == "pyproject.toml": - try: - babel_data = parsed_data["tool"]["babel"] - except (TypeError, KeyError) as e: - raise ConfigurationError( - f"{filename}: No 'tool.babel' section found in file", - ) from e - elif style == "standalone": - babel_data = parsed_data - if "babel" in babel_data: - raise ConfigurationError( - f"{filename}: 'babel' should not be present in a stand-alone configuration file", - ) - else: # pragma: no cover - raise ValueError(f"Unknown TOML style {style!r}") - - return _parse_config_object(babel_data, filename=filename) + parsed_data = _parse_toml(fileobj, style=style, filename=filename) + return _parse_config_object(parsed_data, filename=filename) def _parse_spec(s: str) -> tuple[int | None, tuple[int | tuple[int, str], ...]]: diff --git a/tests/messages/frontend/test_cli.py b/tests/messages/frontend/test_cli.py index a2b85bfba..ccf82eda8 100644 --- a/tests/messages/frontend/test_cli.py +++ b/tests/messages/frontend/test_cli.py @@ -14,6 +14,7 @@ import logging import os +import pathlib import shutil import sys import time @@ -44,6 +45,17 @@ def cli(monkeypatch, capsys): _remove_log_handlers() +@pytest.fixture +def pyproject_toml_file(tmp_path, monkeypatch) -> pathlib.Path: + """ + Fixture to create ``${tmp_path}/pyproject.toml`` and clean it after the test. + """ + pyproject_toml = (tmp_path / "pyproject.toml") + pyproject_toml.touch() + monkeypatch.setenv("BABEL_PYPROJECT_TOML", str(pyproject_toml)) + return pyproject_toml + + def _remove_log_handlers(): # Logging handlers will be reused if possible (#227). This breaks the # implicit assumption that our newly created StringIO for sys.stderr @@ -219,6 +231,105 @@ def test_extract_with_mapping_file(formatted_frozen_time, cli, pot_file): assert expected_content == pot_file.read_text() +def test_extract_with_mapping_file_from_pyproject_toml_config( + formatted_frozen_time, cli, pot_file, pyproject_toml_file, +): + """ + Test the same as ``test_extract_with_mapping_file()`` but with ``pyproject.toml`` + as the configuration source instead of args purely coming from the CLI. + """ + pyproject_toml_file.write_text( + '[tool.babel.extract_messages]\n' + 'copyright_holder = "FooBar, Inc."\n' + 'project = "TestProject"\n' + 'version = "0.1"\n' + 'msgid_bugs_address = "bugs.address@email.tld"\n' + f"mapping_file = '{os.path.join(data_dir, 'mapping.cfg')}'\n" + 'add_comments = "TRANSLATOR,TRANSLATORS:"\n' + f"output_file = '{pot_file}'\n", + ) + cli.run(['pybabel', 'extract', 'project']) + + expected_content = rf"""# Translations template for TestProject. +# Copyright (C) {time.strftime('%Y')} FooBar, Inc. +# This file is distributed under the same license as the TestProject +# project. +# FIRST AUTHOR , {time.strftime('%Y')}. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: TestProject 0.1\n" +"Report-Msgid-Bugs-To: bugs.address@email.tld\n" +"POT-Creation-Date: {formatted_frozen_time}\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=utf-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Generated-By: Babel {VERSION}\n" + +#. TRANSLATOR: This will be a translator coment, +#. that will include several lines +#: project/file1.py:8 +msgid "bar" +msgstr "" + +#: project/file2.py:9 +msgid "foobar" +msgid_plural "foobars" +msgstr[0] "" +msgstr[1] "" + +""" + assert expected_content == pot_file.read_text() + + # Second round, reusing the `expected_content` from above; + # specify `input_paths` as (listified) value from pyproject.toml instead of the CLI + # and specify multiple (listified) comments via pyproject.toml and CLI + pyproject_toml_file.write_text( + '[tool.babel.extract_messages]\n' + 'copyright_holder = "FooBar, Inc."\n' + 'project = "TestProject"\n' + 'version = "0.1"\n' + 'msgid_bugs_address = "bugs.address@email.tld"\n' + f"mapping_file = '{os.path.join(data_dir, 'mapping.cfg')}'\n" + 'add_comments = "TRANSLATOR"\n' + 'input_paths = "project"\n' + f"output_file = '{pot_file}'\n", + ) + cli.run(['pybabel', 'extract', '-c', 'TRANSLATORS:']) + assert expected_content == pot_file.read_text() + + +def test_extract_with_mapping_file_with_disabled_pyproject_toml_config( + formatted_frozen_time, cli, pot_file, pyproject_toml_file, monkeypatch, +): + """ + Test the same as ``test_extract_with_mapping_file()`` but with ``pyproject.toml`` + as the configuration source instead of CLI arguments. + """ + pyproject_toml_file.write_text( + '[tool.babel.extract_messages]\n' + 'copyright_holder = "FooBar, Inc."\n' + 'project = "TestProject"\n' + 'version = "0.1"\n' + 'msgid_bugs_address = "bugs.address@email.tld"\n' + f"mapping_file = '{os.path.join(data_dir, 'mapping.cfg')}'\n" + 'add_comments = "TRANSLATOR,TRANSLATORS:"\n' + f"output_file = '{pot_file}'\n", + ) + + # disable the pyproject.toml parsing + monkeypatch.setenv("BABEL_PYPROJECT_TOML", "skip") + + # optparse is expected to exit with code 2, since output-file is required + # but not provided (it's defined in pyproject.toml but that's disabled) + with pytest.raises(SystemExit, match="2"): + cli.run(['pybabel', 'extract', 'project']) + + def test_extract_with_exact_file(formatted_frozen_time, cli, pot_file): """Tests that we can call extract with a particular file and only strings from that file get extracted. (Note the absence of strings from file1.py) diff --git a/tests/messages/frontend/test_frontend.py b/tests/messages/frontend/test_frontend.py index c8b8d17d5..366357741 100644 --- a/tests/messages/frontend/test_frontend.py +++ b/tests/messages/frontend/test_frontend.py @@ -97,13 +97,13 @@ ), ( mapping_toml, - frontend._parse_mapping_toml, + frontend._parse_mappings_from_toml, None, True, ), ( mapping_toml, - partial(frontend._parse_mapping_toml, style="pyproject.toml"), + partial(frontend._parse_mappings_from_toml, style="pyproject.toml"), lambda s: re.sub(r"^(\[+)", r"\1tool.babel.", s, flags=re.MULTILINE), True, ), diff --git a/tests/messages/test_toml_config.py b/tests/messages/test_toml_config.py index 682728490..c3a2306b7 100644 --- a/tests/messages/test_toml_config.py +++ b/tests/messages/test_toml_config.py @@ -11,10 +11,22 @@ assert toml_test_cases_path.is_dir(), "toml-test-cases directory not found" +@pytest.fixture +def pyproject_toml_file(tmp_path, monkeypatch) -> pathlib.Path: + """ + Fixture to create ``${tmp_path}/pyproject.toml`` and clean it after the test. + """ + pyproject_toml = (tmp_path / "pyproject.toml") + pyproject_toml.touch() + pyproject_toml.write_text('name = "notbabel"') + monkeypatch.setenv("BABEL_PYPROJECT_TOML", str(pyproject_toml)) + return pyproject_toml + + def parse_toml(cfg: bytes | str): if isinstance(cfg, str): cfg = cfg.encode("utf-8") - return frontend._parse_mapping_toml(BytesIO(cfg)) + return frontend._parse_mappings_from_toml(BytesIO(cfg)) def test_toml_mapping_multiple_patterns(): @@ -78,8 +90,62 @@ def test_bad_toml_test_case(test_case: pathlib.Path): """ with pytest.raises(frontend.ConfigurationError): with test_case.open("rb") as f: - frontend._parse_mapping_toml( + frontend._parse_mappings_from_toml( f, filename=test_case.name, style="pyproject.toml" if "pyproject" in test_case.name else "standalone", ) + + +def test_parse_pyproject_toml_config_without_pyproject_toml_file(tmp_path): + """ + Test that trying to parse the config from a missing ``pyproject.toml`` doesn't crash. + """ + assert frontend._parse_pyproject_toml_config(tmp_path, "extract_messages") == {} + + +@pytest.mark.parametrize( + ("pyproject_toml_content", "expected"), + [ + ('[project]\nname = "test"\n', {}), # no [tool.babel] section + ('[tool.babel.compile_catalog]\ndomain = "test"\n', {}), # different command + ( + '[tool.babel.extract_messages]\noutput_file = "test.pot"\n', + {"output_file": "test.pot"}, + ), + ], +) +def test_parse_pyproject_toml_config(tmp_path, pyproject_toml_content, expected): + """ + Test different cases of Babel configuration in ``pyproject.toml``. + """ + (tmp_path / "pyproject.toml").write_text(pyproject_toml_content) + assert frontend._parse_pyproject_toml_config(tmp_path, "extract_messages") == expected + + +@pytest.mark.parametrize( + ("tmp_subpath", "expected", "expected_snippet"), + [ + # starting from CWD (with `None`) is expected to find babel's own pyproject.toml + (None, "pyproject.toml", 'name = "babel"'), + ("pyproject.toml", "pyproject.toml", 'name = "notbabel"'), + (".", "pyproject.toml", 'name = "notbabel"'), + ("..", None, None), + ("some/deep/subpath/structure", None, None), + ], +) +def test_find_pyproject_toml( + pyproject_toml_file, tmp_path, tmp_subpath, expected, expected_snippet, +): + """ + Test the logic for finding the ``pyproject.toml`` from various locations. + """ + search_start_path = (tmp_path / tmp_subpath) if tmp_subpath else None + result = frontend._find_pyproject_toml(search_start_path, max_depth=3) + + if expected is None: + assert result is None + else: + assert result.name == expected + assert result.is_file() + assert expected_snippet in result.read_text()