From 110a65dc7562533f4c28e241f7b00eaeabb7fbb2 Mon Sep 17 00:00:00 2001 From: Jorge Alvarez Jarreta Date: Fri, 4 Oct 2024 17:32:40 +0100 Subject: [PATCH] bugfix: do not rely on self to store knowledge self changes when the server arguments are defined at the subparser level, so all arguments are parsed correctly but the list of server groups was lost, i.e. need to build URLs by pulling the host arguments available in the ArgumentParser --- src/ensembl/utils/__init__.py | 2 +- src/ensembl/utils/argparse.py | 27 ++++++++++++++++----------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/ensembl/utils/__init__.py b/src/ensembl/utils/__init__.py index 8f21517..7acb85a 100644 --- a/src/ensembl/utils/__init__.py +++ b/src/ensembl/utils/__init__.py @@ -14,7 +14,7 @@ # limitations under the License. """Ensembl Python general-purpose utils library.""" -__version__ = "0.5.0" +__version__ = "0.5.1" __all__ = [ "StrPath", diff --git a/src/ensembl/utils/argparse.py b/src/ensembl/utils/argparse.py index 4506f08..c2b9c0b 100644 --- a/src/ensembl/utils/argparse.py +++ b/src/ensembl/utils/argparse.py @@ -38,6 +38,7 @@ import argparse import os from pathlib import Path +import re from typing import Any, Callable from sqlalchemy.engine import make_url, URL @@ -61,7 +62,6 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: """Extends the base class to include the information about default argument values by default.""" super().__init__(*args, **kwargs) self.formatter_class = argparse.ArgumentDefaultsHelpFormatter - self.__server_groups: list[str] = [] def _validate_src_path(self, src_path: StrPath) -> Path: """Returns the path if exists and it is readable, raises an error through the parser otherwise. @@ -240,7 +240,6 @@ def add_server_arguments( default=argparse.SUPPRESS, help="database name", ) - self.__server_groups.append(prefix) def add_log_arguments(self, add_log_file: bool = False) -> None: """Adds the usual set of arguments required to set and initialise a logging system. @@ -312,17 +311,23 @@ def parse_args(self, *args: Any, **kwargs: Any) -> argparse.Namespace: # type: """ arguments = super().parse_args(*args, **kwargs) # Build and add an sqlalchemy.engine.URL object for every server group added - for prefix in self.__server_groups: + pattern = re.compile(r"([\w-]*)host$") + server_prefixes = [x.group(1) for x in map(pattern.match, vars(arguments)) if x] + for prefix in server_prefixes: # Raise an error rather than overwriting when the URL argument is already present if f"{prefix}url" in arguments: self.error(f"argument '{prefix}url' is already present") - server_url = URL.create( - "mysql", - getattr(arguments, f"{prefix}user"), - getattr(arguments, f"{prefix}password"), - getattr(arguments, f"{prefix}host"), - getattr(arguments, f"{prefix}port"), - getattr(arguments, f"{prefix}database", None), - ) + try: + server_url = URL.create( + "mysql", + getattr(arguments, f"{prefix}user"), + getattr(arguments, f"{prefix}password"), + getattr(arguments, f"{prefix}host"), + getattr(arguments, f"{prefix}port"), + getattr(arguments, f"{prefix}database", None), + ) + except AttributeError: + # Not a server host argument + continue setattr(arguments, f"{prefix}url", server_url) return arguments