Skip to content

Commit

Permalink
Merge pull request #199 from GavinHuttley/develop
Browse files Browse the repository at this point in the history
Maintenance patches
  • Loading branch information
GavinHuttley authored Mar 3, 2025
2 parents 4398740 + b0cda45 commit 6c938d5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 22 deletions.
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ select = ["ALL"]
# ignoring checks on the number of arguments to a functions
# numpy should NOT always be imported as np
# turning of SQL injectiuon risk warnings, we are querying public data only
ignore = ["EXE002", "FA100", "E501", "D", "ICN001", "FBT001", "FBT002", "PLR0913", "S608"]
# turning off checking using open instead of pathlib.Path.open
ignore = ["EXE002", "FA100", "E501", "D", "ICN001", "FBT001", "FBT002",
"PLR0913", "S608", "PTH123"]

# Allow fix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
Expand Down
9 changes: 8 additions & 1 deletion src/ensembl_tui/_storage_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,17 @@ def __del__(self) -> None:

def close(self) -> None:
"""closes the hdf5 file"""
# during garbage collection at shutdown, the open function is
# not available
try:
open # noqa: B018
except NameError:
return

# hdf5 dumps content to stdout if resource already closed, so
# we trap that here, and capture expected exceptions raised in the process
with (
open(os.devnull, "w") as devnull, # noqa: PTH123
open(os.devnull, "w") as devnull,
contextlib.redirect_stderr(devnull),
contextlib.redirect_stdout(devnull),
):
Expand Down
52 changes: 35 additions & 17 deletions src/ensembl_tui/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pathlib
import shutil
import sys
import typing

import click
import trogon
Expand All @@ -15,22 +16,39 @@
from ensembl_tui import _species as eti_species
from ensembl_tui import _util as eti_util

if typing.TYPE_CHECKING:
from click.core import Context, Option

def _get_coord_names(ctx, param, coord_names) -> list[str] | None:
"""returns a list of chrom/coord names"""
if coord_names is None:

def _values_from_csv_or_file(
ctx: "Context", # noqa: ARG001
param: "Option", # noqa: ARG001
value: str | None,
) -> list[str] | None:
"""extract values from command line or a file
Notes
-----
converts either comma separated values or a file with one value per line
into values
"""
if not value:
return None

path = pathlib.Path(coord_names)
path = pathlib.Path(value)
if path.is_file():
return [l.strip() for l in path.read_text().splitlines()]

return [f.strip() for f in coord_names.split(",")]
return [f.strip() for f in value.split(",")]


def _get_installed_config_path(ctx, param, path) -> pathlib.Path:
def _get_installed_config_path(
ctx: "Context", # noqa: ARG001
param: "Option", # noqa: ARG001
path: pathlib.Path | str | None,
) -> pathlib.Path:
"""path to installed.cfg"""
path = pathlib.Path(path)
path = pathlib.Path(path or ".")
if path.name == eti_config.INSTALLED_CONFIG_NAME:
return path

Expand All @@ -41,18 +59,18 @@ def _get_installed_config_path(ctx, param, path) -> pathlib.Path:
return path


def _values_from_csv(ctx, param, value) -> list[str] | None:
return None if value is None else [f.strip() for f in value.split(",")]


def _species_names_from_csv(ctx, param, species) -> list[str] | None:
def _species_names_from_csv(
ctx: "Context",
param: "Option",
species: str,
) -> list[str] | None:
"""returns species names"""
species = _values_from_csv(ctx, param, species)
if species is None:
species_names = _values_from_csv_or_file(ctx, param, species)
if species_names is None:
return None

db_names = []
for name in species:
for name in species_names:
try:
db_name = eti_species.Species.get_ensembl_db_prefix(name)
except ValueError:
Expand Down Expand Up @@ -163,13 +181,13 @@ def _species_names_from_csv(ctx, param, species) -> list[str] | None:
)
_mask_features = click.option(
"--mask_features",
callback=_values_from_csv,
callback=_values_from_csv_or_file,
help="Biotypes to mask (comma separated).",
)
_coord_names = click.option(
"--coord_names",
default=None,
callback=_get_coord_names,
callback=_values_from_csv_or_file,
help="Comma separated list of ref species chrom/coord names or a path leading to names, one per line.",
)

Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def tmp_config(tmp_dir):
parser.set("local path", "staging_path", value=str(tmp_dir / "staging"))
parser.set("local path", "install_path", value=str(tmp_dir / "install"))
download_cfg = tmp_dir / "download.cfg"
with open(download_cfg, "w") as out: # noqa: PTH123
with open(download_cfg, "w") as out:
parser.write(out)

return download_cfg
Expand Down
4 changes: 2 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def cfg_just_aligns(empty_cfg):
parser.add_section("compara")
parser.set("compara", "align_names", value="10_primates.epo")
download_cfg = tmp_dir / "download.cfg"
with open(download_cfg, "w") as out: # noqa: PTH123
with open(download_cfg, "w") as out:
parser.write(out)

return download_cfg
Expand Down Expand Up @@ -136,7 +136,7 @@ def cfg_just_genomes(empty_cfg):
parser.add_section(name)
parser.set(name, "db", value="core")

with open(download_cfg, "w") as out: # noqa: PTH123
with open(download_cfg, "w") as out:
parser.write(out)

return download_cfg
Expand Down

0 comments on commit 6c938d5

Please sign in to comment.