Skip to content

Commit

Permalink
Merge pull request #27 from Ensembl/jalvarez/subparser_fix
Browse files Browse the repository at this point in the history
Bugfix: do not rely on self to store knowledge
  • Loading branch information
JAlvarezJarreta authored Oct 7, 2024
2 parents 2a00819 + b0406da commit 373801c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ disable = [
"too-few-public-methods",
"too-many-arguments",
"too-many-locals",
"too-many-positional-arguments",
"too-many-statements",
"unspecified-encoding",
"wildcard-import",
Expand Down
2 changes: 1 addition & 1 deletion src/ensembl/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.
"""Ensembl Python general-purpose utils library."""

__version__ = "0.5.0"
__version__ = "0.5.1"

__all__ = [
"StrPath",
Expand Down
27 changes: 16 additions & 11 deletions src/ensembl/utils/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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

0 comments on commit 373801c

Please sign in to comment.