Skip to content

Commit

Permalink
Merge pull request #809 from AFM-SPM/ns-rse/argparse-type-to-path
Browse files Browse the repository at this point in the history
  • Loading branch information
ns-rse authored Mar 2, 2024
2 parents 1a97e13 + 473f3c8 commit 8798977
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 28 deletions.
45 changes: 26 additions & 19 deletions tests/test_entry_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,42 +68,49 @@ def test_entry_point_subprocess_help(capsys, argument: str, option: str) -> None
@pytest.mark.parametrize(
("options", "expected_function", "expected_arg_name", "expected_arg_value"),
[
(
pytest.param(
[
"process",
"-c dummy/config/dir/config.yaml",
"-c",
"dummy/config/dir/config.yaml",
],
run_topostats,
"config_file",
" dummy/config/dir/config.yaml",
Path("dummy/config/dir/config.yaml"),
id="Process with config file argument",
),
(
pytest.param(
[
"process",
"-b /tmp/",
"-b",
"/tmp/", # noqa: S108
],
run_topostats,
"base_dir",
" /tmp/",
Path("/tmp/"), # noqa: S108
id="Process with base dir argument",
),
(
pytest.param(
[
"create-config",
"--filename",
"dummy/config/dir/config.yaml",
],
write_config_with_comments,
"filename",
"dummy/config/dir/config.yaml",
Path("dummy/config/dir/config.yaml"),
id="Create config with output filename",
),
(
pytest.param(
[
"summary",
"-l dummy/config/dir/var_to_label.yaml",
"-l",
"dummy/config/dir/var_to_label.yaml",
],
run_toposum,
"var_to_label",
" dummy/config/dir/var_to_label.yaml",
Path("dummy/config/dir/var_to_label.yaml"),
id="Summary with label file.",
),
],
)
Expand All @@ -114,10 +121,8 @@ def test_entry_point(
returned_args = entry_point(options, testing=True)
# convert argparse's Namespace object to dictionary
returned_args_dict = vars(returned_args)

# check that the correct function is collected
assert returned_args.func == expected_function

# check that the argument has successfully been passed through into the dictionary
assert returned_args_dict[expected_arg_name] == expected_arg_value

Expand All @@ -133,28 +138,30 @@ def test_entry_point_create_config_file(tmp_path: Path) -> None:
f"{tmp_path}",
]
)

assert Path(f"{tmp_path}/test_create_config.yaml").is_file()


# Test that the right functions are returned with the right arguments
@pytest.mark.parametrize(
("options", "expected_arg_name", "expected_arg_value"),
[
(
pytest.param(
[
"-c dummy/config/dir/config.yaml",
"-c",
"dummy/config/dir/config.yaml",
],
"config_file",
" dummy/config/dir/config.yaml",
Path("dummy/config/dir/config.yaml"),
id="Test using -c flag for config file",
),
(
pytest.param(
[
"--config",
"dummy/config/dir/config.yaml",
],
"config_file",
"dummy/config/dir/config.yaml",
Path("dummy/config/dir/config.yaml"),
id="Test using --config flag for config file",
),
],
)
Expand Down
36 changes: 28 additions & 8 deletions topostats/entry_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import argparse as arg
import sys
from pathlib import Path

from topostats import __version__
from topostats.io import write_config_with_comments
Expand Down Expand Up @@ -39,6 +40,7 @@ def create_parser() -> arg.ArgumentParser:
"-c",
"--config-file",
dest="config_file",
type=Path,
required=False,
help="Path to a YAML configuration file.",
)
Expand All @@ -52,14 +54,15 @@ def create_parser() -> arg.ArgumentParser:
process_parser.add_argument(
"--matplotlibrc",
dest="matplotlibrc",
type=Path,
required=False,
help="Path to a matplotlibrc file.",
)
process_parser.add_argument(
"-b",
"--base-dir",
dest="base_dir",
type=str,
type=Path,
required=False,
help="Base directory to scan for images.",
)
Expand Down Expand Up @@ -98,7 +101,7 @@ def create_parser() -> arg.ArgumentParser:
"-o",
"--output-dir",
dest="output_dir",
type=str,
type=Path,
required=False,
help="Output directory to write results to.",
)
Expand Down Expand Up @@ -151,27 +154,29 @@ def create_parser() -> arg.ArgumentParser:
"-c",
"--config-file",
dest="config_file",
type=Path,
required=False,
help="Path to a YAML plotting dictionary that maps variable names to labels.",
)
toposum_parser.add_argument(
"-l",
"--var-to-label",
dest="var_to_label",
type=Path,
required=False,
help="Path to a YAML plotting dictionary that maps variable names to labels.",
)
toposum_parser.add_argument(
"--create-config-file",
dest="create_config_file",
type=str,
type=Path,
required=False,
help="Filename to write a sample YAML configuration file to (should end in '.yaml').",
)
toposum_parser.add_argument(
"--create-label-file",
dest="create_label_file",
type=str,
type=Path,
required=False,
help="Filename to write a sample YAML label file to (should end in '.yaml').",
)
Expand All @@ -193,6 +198,7 @@ def create_parser() -> arg.ArgumentParser:
"-c",
"--config-file",
dest="config_file",
type=Path,
required=False,
help="Path to a YAML configuration file.",
)
Expand All @@ -206,6 +212,7 @@ def create_parser() -> arg.ArgumentParser:
"-c",
"--config-file",
dest="config_file",
type=Path,
required=False,
help="Path to a YAML configuration file.",
)
Expand All @@ -219,6 +226,7 @@ def create_parser() -> arg.ArgumentParser:
"-c",
"--config-file",
dest="config_file",
type=Path,
required=False,
help="Path to a YAML configuration file.",
)
Expand All @@ -232,6 +240,7 @@ def create_parser() -> arg.ArgumentParser:
"-c",
"--config-file",
dest="config_file",
type=Path,
required=False,
help="Path to a YAML configuration file.",
)
Expand All @@ -245,6 +254,7 @@ def create_parser() -> arg.ArgumentParser:
"-c",
"--config-file",
dest="config_file",
type=Path,
required=False,
help="Path to a YAML configuration file.",
)
Expand All @@ -258,6 +268,7 @@ def create_parser() -> arg.ArgumentParser:
"-c",
"--config-file",
dest="config_file",
type=Path,
required=False,
help="Path to a YAML configuration file.",
)
Expand All @@ -271,6 +282,7 @@ def create_parser() -> arg.ArgumentParser:
"-f",
"--filename",
dest="filename",
type=Path,
required=False,
default="config.yaml",
help="Name of YAML file to save configuration to (default 'config.yaml').",
Expand All @@ -279,6 +291,7 @@ def create_parser() -> arg.ArgumentParser:
"-o",
"--output-dir",
dest="output_dir",
type=Path,
required=False,
default="./",
help="Path to where the YAML file should be saved (default './' the current directory).",
Expand All @@ -287,6 +300,7 @@ def create_parser() -> arg.ArgumentParser:
"-c",
"--config",
dest="config",
type=str,
default=None,
help="Configuration to use, currently only one is supported, the 'default'.",
)
Expand All @@ -301,6 +315,7 @@ def create_parser() -> arg.ArgumentParser:
"-f",
"--filename",
dest="filename",
type=Path,
required=False,
default="topostats.mplstyle",
help="Name of file to save Matplotlibrc configuration to (default 'topostats.mplstyle').",
Expand All @@ -309,6 +324,7 @@ def create_parser() -> arg.ArgumentParser:
"-o",
"--output-dir",
dest="output_dir",
type=Path,
required=False,
default="./",
help="Path to where the YAML file should be saved (default './' the current directory).",
Expand Down Expand Up @@ -354,21 +370,23 @@ def create_legacy_run_topostats_parser() -> arg.ArgumentParser:
"-c",
"--config_file",
dest="config_file",
type=Path,
required=False,
help="Path to a YAML configuration file.",
)
parser.add_argument(
"-s",
"--summary_config",
dest="summary_config",
type=Path,
required=False,
help="Path to a YAML configuration file for summary plots and statistics.",
)
parser.add_argument(
"-b",
"--base_dir",
dest="base_dir",
type=str,
type=Path,
required=False,
help="Base directory to scan for images.",
)
Expand Down Expand Up @@ -407,7 +425,7 @@ def create_legacy_run_topostats_parser() -> arg.ArgumentParser:
"-o",
"--output_dir",
dest="output_dir",
type=str,
type=Path,
required=False,
help="Output directory to write results to.",
)
Expand Down Expand Up @@ -448,27 +466,29 @@ def create_legacy_toposum_parser() -> arg.ArgumentParser:
"-c",
"--config_file",
dest="config_file",
type=Path,
required=False,
help="Path to a YAML configuration file.",
)
parser.add_argument(
"-l",
"--var_to_label",
dest="var_to_label",
type=Path,
required=False,
help="Path to a YAML plotting dictionary that maps variable names to labels.",
)
parser.add_argument(
"--create-config-file",
dest="create_config_file",
type=str,
type=Path,
required=False,
help="Filename to write a sample YAML configuration file to (should end in '.yaml').",
)
parser.add_argument(
"--create-label-file",
dest="create_label_file",
type=str,
type=Path,
required=False,
help="Filename to write a sample YAML label file to (should end in '.yaml').",
)
Expand Down
2 changes: 1 addition & 1 deletion topostats/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def write_config_with_comments(args=None) -> None:
except FileNotFoundError as e:
raise UserWarning(f"There is no configuration for samples of type : {args.config}") from e

if ".yaml" not in filename and ".yml" not in filename and ".mplstyle" not in filename:
if ".yaml" not in str(filename) and ".yml" not in str(filename) and ".mplstyle" not in str(filename):
create_config_path = output_dir / f"{filename}.yaml"
else:
create_config_path = output_dir / filename
Expand Down

0 comments on commit 8798977

Please sign in to comment.