Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ jobs:
package-manager: uv
packages: gvm tests
python-version: ${{ matrix.python-version }}
linter: ruff check
linter: ruff check --diff
formatter: ruff format --check --diff

test:
name: Run tests
Expand Down
9 changes: 4 additions & 5 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.
#
Expand All @@ -14,20 +13,20 @@
#
# pylint: disable=invalid-name,redefined-builtin,wrong-import-position

import os
import sys
from datetime import datetime
from pathlib import Path

sys.path.insert(0, os.path.abspath(".."))
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

import gvm # noqa: E402
import gvm

# -- Project information -----------------------------------------------------

year = datetime.now().year

project = "python-gvm"
copyright = f"2018 - {year}, Greenbone AG"
copyright = f"2018 - {year}, Greenbone AG" # noqa: A001
author = "Greenbone AG"

# The short X.Y version
Expand Down
10 changes: 5 additions & 5 deletions gvm/_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#

from enum import Enum as PythonEnum
from typing import Any, Optional, Type, TypeVar
from typing import Any, TypeVar

from gvm.errors import InvalidArgument

Expand All @@ -17,16 +17,16 @@ class Enum(PythonEnum):
"""

@classmethod
def _missing_(cls: Type[Self], value: Any) -> Optional[Self]:
def _missing_(cls: type[Self], value: Any) -> Self | None:
if isinstance(value, PythonEnum):
return cls.from_string(value.name)
return cls.from_string(str(value) if value else None)

@classmethod
def from_string(
cls: Type[Self],
value: Optional[str],
) -> Optional[Self]:
cls: type[Self],
value: str | None,
) -> Self | None:
"""
Convert a string value into an Enum instance

Expand Down
6 changes: 3 additions & 3 deletions gvm/connections/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
from ._unix import DEFAULT_UNIX_SOCKET_PATH, UnixSocketConnection

__all__ = (
"DEFAULT_TIMEOUT",
"DEFAULT_UNIX_SOCKET_PATH",
"DEFAULT_GVM_PORT",
"DEFAULT_HOSTNAME",
"DEFAULT_KNOWN_HOSTS_FILE",
"DEFAULT_SSH_PASSWORD",
"DEFAULT_SSH_USERNAME",
"DEFAULT_SSH_PORT",
"DEFAULT_SSH_USERNAME",
"DEFAULT_TIMEOUT",
"DEFAULT_UNIX_SOCKET_PATH",
"DebugConnection",
"GvmConnection",
"SSHConnection",
Expand Down
6 changes: 3 additions & 3 deletions gvm/connections/_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import socket as socketlib
from abc import ABC, abstractmethod
from time import time
from typing import Optional, Protocol, Union, runtime_checkable
from typing import Protocol, runtime_checkable

from gvm.errors import GvmError

Expand Down Expand Up @@ -62,8 +62,8 @@ class AbstractGvmConnection(ABC):
wait indefinitely
"""

def __init__(self, timeout: Optional[Union[int, float]] = DEFAULT_TIMEOUT):
self._socket: Optional[socketlib.SocketType] = None
def __init__(self, timeout: int | float | None = DEFAULT_TIMEOUT):
self._socket: socketlib.SocketType | None = None
self._timeout = timeout if timeout is not None else DEFAULT_TIMEOUT

def _read(self) -> bytes:
Expand Down
2 changes: 1 addition & 1 deletion gvm/connections/_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DebugConnection:

logging.basicConfig(level=logging.DEBUG)

socket_connection = UnixSocketConnection(path='/var/run/gvm.sock')
socket_connection = UnixSocketConnection(path="/var/run/gvm.sock")
connection = DebugConnection(socket_connection)
gmp = GMP(connection=connection)
"""
Expand Down
29 changes: 14 additions & 15 deletions gvm/connections/_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import logging
import socket as socketlib
import sys
from collections.abc import Callable
from os import PathLike
from pathlib import Path
from time import time
from typing import Any, Callable, Optional, TextIO, Union
from typing import Any, TextIO

import paramiko
import paramiko.ssh_exception
Expand Down Expand Up @@ -39,16 +40,16 @@ class SSHConnection:
def __init__(
self,
*,
timeout: Optional[Union[int, float]] = DEFAULT_TIMEOUT,
hostname: Optional[str] = DEFAULT_HOSTNAME,
port: Optional[int] = DEFAULT_SSH_PORT,
username: Optional[str] = DEFAULT_SSH_USERNAME,
password: Optional[str] = DEFAULT_SSH_PASSWORD,
known_hosts_file: Optional[Union[str, PathLike]] = None,
auto_accept_host: Optional[bool] = None,
timeout: int | float | None = DEFAULT_TIMEOUT,
hostname: str | None = DEFAULT_HOSTNAME,
port: int | None = DEFAULT_SSH_PORT,
username: str | None = DEFAULT_SSH_USERNAME,
password: str | None = DEFAULT_SSH_PASSWORD,
known_hosts_file: str | PathLike | None = None,
auto_accept_host: bool | None = None,
file: TextIO = sys.stdout,
input: Callable[[], str] = input,
exit: Callable[[str], Any] = sys.exit,
input: Callable[[], str] = input, # noqa: A002
exit: Callable[[str], Any] = sys.exit, # noqa: A002
) -> None:
"""
Create a new SSH connection instance.
Expand All @@ -61,7 +62,7 @@ def __init__(
username: Username to use for SSH login. Default is "gmp".
password: Password to use for SSH login. Default is "".
"""
self._client: Optional[paramiko.SSHClient] = None
self._client: paramiko.SSHClient | None = None
self.hostname = hostname if hostname is not None else DEFAULT_HOSTNAME
self.port = int(port) if port is not None else DEFAULT_SSH_PORT
self.username = (
Expand Down Expand Up @@ -118,8 +119,7 @@ def _auto_accept_host(
key_type = key.get_name().replace("ssh-", "").upper()

logger.info(
"Warning: Permanently added '%s' (%s) to "
"the list of known hosts.",
"Warning: Permanently added '%s' (%s) to the list of known hosts.",
self.hostname,
key_type,
)
Expand All @@ -135,8 +135,7 @@ def _ssh_authentication_input_loop(
key_type = key.get_name().replace("ssh-", "").upper()

print(
f"The authenticity of host '{self.hostname}' can't "
"be established.",
f"The authenticity of host '{self.hostname}' can't be established.",
file=self._file,
)
print(
Expand Down
15 changes: 7 additions & 8 deletions gvm/connections/_tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import logging
import socket as socketlib
import ssl
from typing import Optional, Union

from ._connection import DEFAULT_TIMEOUT, AbstractGvmConnection

Expand All @@ -25,13 +24,13 @@ class TLSConnection(AbstractGvmConnection):
def __init__(
self,
*,
certfile: Optional[str] = None,
cafile: Optional[str] = None,
keyfile: Optional[str] = None,
hostname: Optional[str] = DEFAULT_HOSTNAME,
port: Optional[int] = DEFAULT_GVM_PORT,
password: Optional[str] = None,
timeout: Optional[Union[int, float]] = DEFAULT_TIMEOUT,
certfile: str | None = None,
cafile: str | None = None,
keyfile: str | None = None,
hostname: str | None = DEFAULT_HOSTNAME,
port: int | None = DEFAULT_GVM_PORT,
password: str | None = None,
timeout: int | float | None = DEFAULT_TIMEOUT,
) -> None:
"""
Create a new TLSConnection instance.
Expand Down
5 changes: 2 additions & 3 deletions gvm/connections/_unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import socket as socketlib
from os import PathLike, fspath
from typing import Optional, Union

from gvm.errors import GvmError

Expand All @@ -22,8 +21,8 @@ class UnixSocketConnection(AbstractGvmConnection):
def __init__(
self,
*,
path: Optional[Union[str, PathLike[str]]] = DEFAULT_UNIX_SOCKET_PATH,
timeout: Optional[Union[int, float]] = DEFAULT_TIMEOUT,
path: str | PathLike[str] | None = DEFAULT_UNIX_SOCKET_PATH,
timeout: int | float | None = DEFAULT_TIMEOUT,
) -> None:
"""
Create a new UnixSocketConnection instance.
Expand Down
34 changes: 14 additions & 20 deletions gvm/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
Module for GVM errors
"""

from typing import Optional


class GvmError(Exception):
"""An exception for gvm errors

Base class for all exceptions originating in python-gvm.
"""

def __init__(self, message: Optional[str], *args):
def __init__(self, message: str | None, *args):
super().__init__(message, *args)
self.message = message

Expand Down Expand Up @@ -44,9 +42,7 @@ class GvmServerError(GvmError):
and function
"""

def __init__(
self, status: Optional[str] = None, message: Optional[str] = None
):
def __init__(self, status: str | None = None, message: str | None = None):
super().__init__(message, status)
self.status = status

Expand All @@ -71,9 +67,7 @@ class GvmResponseError(GvmClientError):
and function
"""

def __init__(
self, status: Optional[str] = None, message: Optional[str] = None
):
def __init__(self, status: str | None = None, message: str | None = None):
super().__init__(message, status)
self.status = status

Expand All @@ -87,7 +81,7 @@ def __repr__(self):
)


class InvalidArgument(GvmError):
class InvalidArgument(GvmError): # noqa: N818
"""Raised if an invalid argument/parameter is passed

Derives from :py:class:`GvmError`
Expand All @@ -101,10 +95,10 @@ class InvalidArgument(GvmError):

def __init__(
self,
message: Optional[str] = None,
message: str | None = None,
*,
argument: Optional[str] = None,
function: Optional[str] = None,
argument: str | None = None,
function: str | None = None,
):
super().__init__(message, argument, function)
self.argument = argument
Expand All @@ -123,7 +117,7 @@ def __str__(self):
return f"Invalid argument {self.argument} for {self.function}"


class InvalidArgumentType(GvmError):
class InvalidArgumentType(GvmError): # noqa: N818
"""Raised if a passed argument has an invalid type

Derives from :py:class:`GvmError`
Expand All @@ -138,8 +132,8 @@ def __init__(
self,
argument: str,
*,
arg_type: Optional[str] = None,
function: Optional[str] = None,
arg_type: str | None = None,
function: str | None = None,
):
super().__init__(None)
self.argument = argument
Expand All @@ -165,7 +159,7 @@ def __str__(self):
return f"Invalid argument type for argument {self.argument}."


class RequiredArgument(GvmError):
class RequiredArgument(GvmError): # noqa: N818
"""Raised if a required argument/parameter is missing

Derives from :py:class:`GvmError`
Expand All @@ -179,10 +173,10 @@ class RequiredArgument(GvmError):

def __init__(
self,
message: Optional[str] = None,
message: str | None = None,
*,
argument: Optional[str] = None,
function: Optional[str] = None,
argument: str | None = None,
function: str | None = None,
):
super().__init__(message, argument, function)
self.argument = argument
Expand Down
11 changes: 6 additions & 5 deletions gvm/protocols/_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later

from collections.abc import Callable
from types import TracebackType
from typing import Callable, Generic, Optional, Type, TypeVar
from typing import Generic, TypeVar

from gvm.connections import GvmConnection

Expand Down Expand Up @@ -50,9 +51,9 @@ def __enter__(self: Self) -> Self:

def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_value: Optional[BaseException],
traceback: Optional[TracebackType],
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
) -> None:
self.disconnect()

Expand Down Expand Up @@ -126,7 +127,7 @@ def _send_request(self, request: Request) -> Response:
try:
send_data = self._protocol.send(request)
self._send(send_data)
response: Optional[Response] = None
response: Response | None = None
while not response:
received_data = self._read()
response = self._protocol.receive_data(received_data)
Expand Down
Loading
Loading