From 457e1c36621b1579ed5762bb5fedcf127444348d Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Mon, 17 Aug 2026 09:05:29 +0300 Subject: [PATCH] De-sextuplicate width/no-wrap code in commands --- babel/messages/frontend.py | 49 +++++++++++++------------------------- 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py index c80bcbeb8..89826e834 100644 --- a/babel/messages/frontend.py +++ b/babel/messages/frontend.py @@ -136,6 +136,9 @@ class CommandMixin: #: Log object. To allow replacement in the script command line runner. log = log + no_wrap: bool + width: int | None + def __init__(self, dist=None): # A less strict version of distutils' `__init__`. self.distribution = dist @@ -159,6 +162,15 @@ def finalize_options(self): f"abstract method -- subclass {self.__class__} must override", ) + def _finalize_width(self) -> None: + """Validate and normalize the `--width` / `--no-wrap` pair.""" + if self.no_wrap and self.width: + raise OptionError("'--no-wrap' and '--width' are mutually exclusive") + if not self.no_wrap and not self.width: + self.width = 76 + elif self.width is not None: + self.width = int(self.width) + class CompileCatalog(CommandMixin): description = 'compile message catalogs to binary MO files' @@ -418,14 +430,7 @@ def finalize_options(self): if not self.output_file: raise OptionError('no output file specified') - if self.no_wrap and self.width: - raise OptionError( - "'--no-wrap' and '--width' are mutually exclusive", - ) - if not self.no_wrap and not self.width: - self.width = 76 - elif self.width is not None: - self.width = int(self.width) + self._finalize_width() if self.sort_output and self.sort_by_file: raise OptionError( @@ -663,12 +668,7 @@ def finalize_options(self): lc_messages_path = pathlib.Path(self.output_dir) / self.locale / "LC_MESSAGES" self.output_file = str(lc_messages_path / f"{self.domain}.po") - if self.no_wrap and self.width: - raise OptionError("'--no-wrap' and '--width' are mutually exclusive") - if not self.no_wrap and not self.width: - self.width = 76 - elif self.width is not None: - self.width = int(self.width) + self._finalize_width() def run(self): self.log.info( @@ -772,12 +772,7 @@ def finalize_options(self): else: self._locale = None - if self.no_wrap and self.width: - raise OptionError("'--no-wrap' and '--width' are mutually exclusive") - if not self.no_wrap and not self.width: - self.width = 76 - elif self.width is not None: - self.width = int(self.width) + self._finalize_width() if self.no_fuzzy_matching and self.previous: self.previous = False @@ -942,12 +937,7 @@ def finalize_options(self): if not self.input_files: raise OptionError('you must specify the input files') - if self.no_wrap and self.width: - raise OptionError("'--no-wrap' and '--width' are mutually exclusive") - if not self.no_wrap and not self.width: - self.width = 76 - elif self.width is not None: - self.width = int(self.width) + self._finalize_width() if self.more_than is None: self.more_than = 0 @@ -1090,12 +1080,7 @@ def finalize_options(self): if not self.output_file and not self.update: raise OptionError('you must specify the output file or use --update') - if self.no_wrap and self.width: - raise OptionError("'--no-wrap' and '--width' are mutually exclusive") - if not self.no_wrap and not self.width: - self.width = 76 - elif self.width is not None: - self.width = int(self.width) + self._finalize_width() def _get_messages_from_compendiums(self, compendium_paths): for file_path in compendium_paths: