From 4155a685f2ac2cb4aaecd89d1be420e58c9e6bb6 Mon Sep 17 00:00:00 2001 From: Kate Case Date: Fri, 13 Dec 2024 12:14:13 -0500 Subject: [PATCH] Introduce oxford comma function --- src/molecule/config.py | 2 +- src/molecule/util.py | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/molecule/config.py b/src/molecule/config.py index 35aa5dd68c..1e56ebdf3a 100644 --- a/src/molecule/config.py +++ b/src/molecule/config.py @@ -281,7 +281,7 @@ def collection(self) -> CollectionData | None: LOG.warning( "The detected galaxy.yml file (%s) is incomplete, missing %s", galaxy_file, - missing_keys, + util.oxford_comma(missing_keys), ) return None diff --git a/src/molecule/util.py b/src/molecule/util.py index a57abc61ea..0d1d72664b 100644 --- a/src/molecule/util.py +++ b/src/molecule/util.py @@ -597,3 +597,25 @@ def print_as_yaml(data: object) -> None: # https://github.com/Textualize/rich/discussions/990#discussioncomment-342217 result = Syntax(code=safe_dump(data), lexer="yaml", background_color="default") console.print(result) + + +def oxford_comma(listed: Iterable[bool | str | Path], condition: str = "and") -> str: + """Format a list into a sentence. + + Args: + listed: List of string entries to modify + condition: String to splice into string, usually 'and' + + Returns: + Modified string + """ + match [f"'{entry!s}'" for entry in listed]: + case []: + return "" + case [one]: + return one + case [one, two]: + return f"{one} {condition} {two}" + case [*front, back]: + return f"{', '.join(s for s in front)}, {condition} {back}" + return ""