diff --git a/pg_backup_api/pg_backup_api/logic/utility_controller.py b/pg_backup_api/pg_backup_api/logic/utility_controller.py index b84c788..dff02a1 100644 --- a/pg_backup_api/pg_backup_api/logic/utility_controller.py +++ b/pg_backup_api/pg_backup_api/logic/utility_controller.py @@ -40,6 +40,8 @@ if TYPE_CHECKING: # pragma: no cover from flask import Request, Response + from barman.config import Config as BarmanConfig + from pg_backup_api.server_operation import Operation @app.route("/diagnose", methods=["GET"]) @@ -53,11 +55,15 @@ def diagnose() -> 'Response': """ # Reload the barman config so that any changes are picked up load_barman_config() + + if TYPE_CHECKING: # pragma: no cover + assert isinstance(barman.__config__, BarmanConfig) + # Get every server (both inactive and temporarily disabled) servers = barman.__config__.server_names() server_dict = {} - for server in servers: + for server in servers: # pyright: ignore conf = barman.__config__.get_server(server) if conf is None: # Unknown server @@ -214,6 +220,9 @@ def servers_operations_post(server_name: str, ) subprocess.Popen(cmd.split()) + if TYPE_CHECKING: # pragma: no cover + assert isinstance(operation, Operation) + return {"operation_id": operation.id} diff --git a/pg_backup_api/pg_backup_api/server_operation.py b/pg_backup_api/pg_backup_api/server_operation.py index b79c143..52cf028 100644 --- a/pg_backup_api/pg_backup_api/server_operation.py +++ b/pg_backup_api/pg_backup_api/server_operation.py @@ -30,7 +30,7 @@ import os import subprocess import sys -from typing import (Any, Callable, Dict, List, Optional, Set, Tuple, +from typing import (Any, Callable, Dict, List, Optional, Set, Tuple, Union, TYPE_CHECKING) from datetime import datetime @@ -494,7 +494,8 @@ def get_status(self) -> str: return self.server.get_operation_status(self.id) @staticmethod - def _run_subprocess(cmd: List[str]) -> Tuple[Optional[str], int]: + def _run_subprocess(cmd: List[str]) -> \ + Tuple[Union[str, bytearray, memoryview], Union[int, Any]]: """ Run *cmd* as a subprocess. @@ -512,7 +513,8 @@ def _run_subprocess(cmd: List[str]) -> Tuple[Optional[str], int]: return stdout, process.returncode @abstractmethod - def _run_logic(self) -> Tuple[Optional[str], int]: + def _run_logic(self) -> \ + Tuple[Union[str, bytearray, memoryview], Union[int, Any]]: """ Logic to be ran when executing the operation. @@ -526,7 +528,7 @@ def _run_logic(self) -> Tuple[Optional[str], int]: """ pass - def run(self) -> Tuple[Optional[str], int]: + def run(self) -> Tuple[Union[str, bytearray, memoryview], Union[int, Any]]: """ Run the operation. @@ -621,7 +623,8 @@ def _get_args(self) -> List[str]: remote_ssh_command, ] - def _run_logic(self) -> Tuple[Optional[str], int]: + def _run_logic(self) -> \ + Tuple[Union[str, bytearray, memoryview], Union[int, Any]]: """ Logic to be ran when executing the recovery operation. diff --git a/pg_backup_api/pg_backup_api/utils.py b/pg_backup_api/pg_backup_api/utils.py index ce1ec7b..736c3e4 100644 --- a/pg_backup_api/pg_backup_api/utils.py +++ b/pg_backup_api/pg_backup_api/utils.py @@ -34,7 +34,8 @@ if TYPE_CHECKING: # pragma: no cover import flask.app - from barman.config import ServerConfig + from barman.config import Config as BarmanConfig, ServerConfig + import barman.server CONFIG_FILENAME = "/etc/barman.conf" @@ -98,7 +99,10 @@ def get_server_by_name(server_name: str) -> Optional['ServerConfig']: :return: configuration of Barman server *server_name* if that server exists, ``None`` otherwise. """ - for server in barman.__config__.server_names(): + if TYPE_CHECKING: # pragma: no cover + assert isinstance(barman.__config__, BarmanConfig) + + for server in barman.__config__.server_names(): # pyright: ignore conf = barman.__config__.get_server(server) if server == server_name: return conf @@ -120,11 +124,13 @@ def parse_backup_id(server: barman.server.Server, :return: information about the backup, if *backup_id* can be satisfied, ``None`` otherwise. """ + parsed_backup_id = backup_id + if backup_id in ("latest", "last"): - backup_id = server.get_last_backup_id() + parsed_backup_id = server.get_last_backup_id() elif backup_id in ("oldest", "first"): - backup_id = server.get_first_backup_id() + parsed_backup_id = server.get_first_backup_id() elif backup_id in ("last-failed"): - backup_id = server.get_last_backup_id([BackupInfo.FAILED]) + parsed_backup_id = server.get_last_backup_id([BackupInfo.FAILED]) - return server.get_backup(backup_id) + return server.get_backup(parsed_backup_id)