Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Feb 7, 2025
1 parent 2ab0757 commit 7f360fa
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 55 deletions.
4 changes: 1 addition & 3 deletions src/pleiades/core/data_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ def get_file_path(self, category: DataCategory, filename: str) -> Path:

file_path = Path(filename)
if file_path.suffix not in self._VALID_EXTENSIONS[category]:
raise ValueError(
f"Invalid file extension for {category}. " f"Allowed extensions: {self._VALID_EXTENSIONS[category]}"
)
raise ValueError(f"Invalid file extension for {category}. " f"Allowed extensions: {self._VALID_EXTENSIONS[category]}")

try:
with resources.path(f"pleiades.data.{self._get_category_path(category)}", filename) as path:
Expand Down
4 changes: 1 addition & 3 deletions src/pleiades/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,7 @@ def areal_density(self) -> float:
if self.thickness_unit == "mm":
thickness_cm /= 10.0

return (
thickness_cm * self.density * CONSTANTS.avogadro_number / self.atomic_mass.value / 1e24
) # Convert to atoms/barn
return thickness_cm * self.density * CONSTANTS.avogadro_number / self.atomic_mass.value / 1e24 # Convert to atoms/barn


# Unit conversion functions
Expand Down
4 changes: 1 addition & 3 deletions src/pleiades/sammy/backends/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ def prepare_environment(self, files: SammyFiles) -> None:
raise EnvironmentPreparationError("Docker not found in PATH")

# Verify docker image exists
result = subprocess.run(
["docker", "image", "inspect", self.config.image_name], capture_output=True, text=True
)
result = subprocess.run(["docker", "image", "inspect", self.config.image_name], capture_output=True, text=True)
if result.returncode != 0:
raise EnvironmentPreparationError(f"Docker image not found: {self.config.image_name}")

Expand Down
5 changes: 1 addition & 4 deletions src/pleiades/sammy/backends/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ def execute_sammy(self, files: SammyFiles) -> SammyExecutionResult:

if not success:
logger.error(f"SAMMY execution failed for {execution_id}")
error_message = (
f"SAMMY execution failed with return code {process.returncode}. "
"Check console output for details."
)
error_message = f"SAMMY execution failed with return code {process.returncode}. " "Check console output for details."
else:
logger.info(f"SAMMY execution completed successfully for {execution_id}")
error_message = None
Expand Down
16 changes: 4 additions & 12 deletions src/pleiades/sammy/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ def list_available_backends() -> Dict[BackendType, bool]:
return available

@classmethod
def create_runner(
cls, backend_type: str, working_dir: Path, output_dir: Optional[Path] = None, **kwargs
) -> SammyRunner:
def create_runner(cls, backend_type: str, working_dir: Path, output_dir: Optional[Path] = None, **kwargs) -> SammyRunner:
"""
Create a SAMMY runner with the specified backend and configuration.
Expand Down Expand Up @@ -295,9 +293,7 @@ def process_config(cfg):
backend_config = config.get(backend_type, {})

# Create runner using create_runner
return cls.create_runner(
backend_type=backend_type, working_dir=working_dir, output_dir=output_dir, **backend_config
)
return cls.create_runner(backend_type=backend_type, working_dir=working_dir, output_dir=output_dir, **backend_config)

except Exception as e:
if not isinstance(e, (ConfigurationError, BackendNotAvailableError)):
Expand Down Expand Up @@ -347,9 +343,7 @@ def auto_select(
preferred = BackendType(preferred_backend.lower())
if available[preferred]:
logger.info(f"Using preferred backend: {preferred.value}")
return cls.create_runner(
backend_type=preferred.value, working_dir=working_dir, output_dir=output_dir, **kwargs
)
return cls.create_runner(backend_type=preferred.value, working_dir=working_dir, output_dir=output_dir, **kwargs)
else:
logger.warning(f"Preferred backend {preferred.value} not available, " "trying alternatives")
except ValueError:
Expand All @@ -367,9 +361,7 @@ def auto_select(
if available[backend]:
try:
logger.info(f"Attempting to use {backend.value} backend")
return cls.create_runner(
backend_type=backend.value, working_dir=working_dir, output_dir=output_dir, **kwargs
)
return cls.create_runner(backend_type=backend.value, working_dir=working_dir, output_dir=output_dir, **kwargs)
except Exception as e:
logger.warning(f"Failed to configure {backend.value} backend: {str(e)}")
errors.append(f"{backend.value}: {str(e)}")
Expand Down
8 changes: 2 additions & 6 deletions tests/unit/pleiades/sammy/backends/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ def test_execute_sammy_success(self, docker_config, mock_sammy_files, mock_subpr
assert "Normal finish to SAMMY" in result.console_output
assert result.error_message is None

def test_execute_sammy_failure(
self, docker_config, mock_sammy_files, mock_subprocess_docker_fail, mock_docker_command
):
def test_execute_sammy_failure(self, docker_config, mock_sammy_files, mock_subprocess_docker_fail, mock_docker_command):
"""Should handle SAMMY execution failure in container."""
_ = mock_docker_command # implicitly used via fixture
_ = mock_subprocess_docker_fail # implicitly used via fixture
Expand All @@ -139,9 +137,7 @@ def test_execute_sammy_failure(
assert not result.success
assert "Docker execution failed" in result.error_message

def test_collect_outputs(
self, docker_config, mock_sammy_files, mock_subprocess_docker, mock_docker_command, mock_sammy_results
):
def test_collect_outputs(self, docker_config, mock_sammy_files, mock_subprocess_docker, mock_docker_command, mock_sammy_results):
"""Should collect output files from container."""
_ = mock_docker_command # implicitly used via fixture
_ = mock_subprocess_docker # implicitly used via fixture
Expand Down
16 changes: 4 additions & 12 deletions tests/unit/pleiades/sammy/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ def test_create_with_valid_paths(self, temp_working_dir):

def test_create_with_defaults(self, temp_working_dir):
"""Should create config with default values."""
config = LocalSammyConfig(
working_dir=temp_working_dir, output_dir=temp_working_dir / "output", sammy_executable=Path("sammy")
)
config = LocalSammyConfig(working_dir=temp_working_dir, output_dir=temp_working_dir / "output", sammy_executable=Path("sammy"))
assert config.shell_path == Path("/bin/bash")

def test_validate_sammy_in_path(self, temp_working_dir, monkeypatch):
Expand All @@ -37,18 +35,14 @@ def mock_which(path):

monkeypatch.setattr("shutil.which", mock_which)

config = LocalSammyConfig(
working_dir=temp_working_dir, output_dir=temp_working_dir / "output", sammy_executable=Path("sammy")
)
config = LocalSammyConfig(working_dir=temp_working_dir, output_dir=temp_working_dir / "output", sammy_executable=Path("sammy"))
assert config.validate()

def test_validate_sammy_not_in_path(self, temp_working_dir, monkeypatch):
"""Should raise error if SAMMY not in PATH."""
monkeypatch.setattr("shutil.which", lambda _: None)

config = LocalSammyConfig(
working_dir=temp_working_dir, output_dir=temp_working_dir / "output", sammy_executable=Path("sammy")
)
config = LocalSammyConfig(working_dir=temp_working_dir, output_dir=temp_working_dir / "output", sammy_executable=Path("sammy"))
with pytest.raises(ConfigurationError) as exc:
config.validate()
assert "not found" in str(exc.value)
Expand Down Expand Up @@ -171,9 +165,7 @@ def test_validate_invalid_url(self, temp_working_dir):

def test_empty_url(self, temp_working_dir):
"""Should raise error for empty URL."""
config = NovaSammyConfig(
working_dir=temp_working_dir, output_dir=temp_working_dir / "output", url="", api_key="valid_api_key"
)
config = NovaSammyConfig(working_dir=temp_working_dir, output_dir=temp_working_dir / "output", url="", api_key="valid_api_key")
with pytest.raises(ConfigurationError) as exc:
config.validate()
assert "NOVA service URL cannot be empty" in str(exc.value)
Expand Down
16 changes: 4 additions & 12 deletions tests/unit/pleiades/sammy/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ def _mock_run(*args, **kwargs):
@pytest.fixture
def mock_subprocess_run_docker_fail(monkeypatch):
"""Mock subprocess.run to simulate docker info failure."""
monkeypatch.setattr(
subprocess, "run", lambda *args, **kwargs: subprocess.CompletedProcess(args=args, returncode=1, **kwargs)
)
monkeypatch.setattr(subprocess, "run", lambda *args, **kwargs: subprocess.CompletedProcess(args=args, returncode=1, **kwargs))


# Mock SammyRunner creation for runner tests
Expand All @@ -100,9 +98,7 @@ def test_list_available_backends_all_available(self, mock_which, mock_subprocess
BackendType.NOVA: True,
}

def test_list_available_backends_none_available(
self, monkeypatch, mock_which_unavailable, mock_subprocess_run_docker_fail
):
def test_list_available_backends_none_available(self, monkeypatch, mock_which_unavailable, mock_subprocess_run_docker_fail):
"""No backends should be available."""
_ = mock_which_unavailable, mock_subprocess_run_docker_fail # implicitly used by the fixture
# remove NOVA env vars to simulate unavailability
Expand Down Expand Up @@ -218,9 +214,7 @@ def test_auto_select_local(self, mock_which, mock_subprocess_run, mock_sammy_run
assert isinstance(runner, LocalSammyRunner)
assert "Attempting to use local backend" in caplog.text

def test_auto_select_docker(
self, monkeypatch, mock_which_unavailable, mock_subprocess_run, mock_sammy_runner, tmp_path, caplog
):
def test_auto_select_docker(self, monkeypatch, mock_which_unavailable, mock_subprocess_run, mock_sammy_runner, tmp_path, caplog):
"""Should auto-select docker backend if local unavailable."""
_ = mock_which_unavailable, mock_subprocess_run, mock_sammy_runner # implicitly used by the fixture
# remove NOVA env vars to simulate unavailability
Expand All @@ -240,9 +234,7 @@ def test_auto_select_preferred(self, mock_which, mock_subprocess_run, mock_sammy
assert isinstance(runner, DockerSammyRunner)
assert "Using preferred backend: docker" in caplog.text

def test_auto_select_none_available(
self, monkeypatch, mock_which_unavailable, mock_subprocess_run_docker_fail, tmp_path, caplog
):
def test_auto_select_none_available(self, monkeypatch, mock_which_unavailable, mock_subprocess_run_docker_fail, tmp_path, caplog):
"""Should raise error if no backends available."""
_ = mock_which_unavailable, mock_subprocess_run_docker_fail # implicitly used by the fixture
# remove NOVA env vars to simulate unavailability
Expand Down

0 comments on commit 7f360fa

Please sign in to comment.