Skip to content

Commit a0ef252

Browse files
bjoernricksgreenbonebot
authored andcommitted
Misc: Use ruff instead of black for code formatting
Replace black with ruff format which is a lot faster. Also it reduces the list of required dependencies for the project. Also Update the Python code for Python >= 3.10 syntax and format all code with ruff.
1 parent 01b5864 commit a0ef252

File tree

162 files changed

+1788
-2184
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

162 files changed

+1788
-2184
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ jobs:
2626
package-manager: uv
2727
packages: gvm tests
2828
python-version: ${{ matrix.python-version }}
29-
linter: ruff check
29+
linter: ruff check --diff
30+
formatter: ruff format --check --diff
3031

3132
test:
3233
name: Run tests

docs/conf.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
#
32
# Configuration file for the Sphinx documentation builder.
43
#
@@ -14,20 +13,20 @@
1413
#
1514
# pylint: disable=invalid-name,redefined-builtin,wrong-import-position
1615

17-
import os
1816
import sys
1917
from datetime import datetime
18+
from pathlib import Path
2019

21-
sys.path.insert(0, os.path.abspath(".."))
20+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
2221

23-
import gvm # noqa: E402
22+
import gvm
2423

2524
# -- Project information -----------------------------------------------------
2625

2726
year = datetime.now().year
2827

2928
project = "python-gvm"
30-
copyright = f"2018 - {year}, Greenbone AG"
29+
copyright = f"2018 - {year}, Greenbone AG" # noqa: A001
3130
author = "Greenbone AG"
3231

3332
# The short X.Y version

gvm/_enum.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55

66
from enum import Enum as PythonEnum
7-
from typing import Any, Optional, Type, TypeVar
7+
from typing import Any, TypeVar
88

99
from gvm.errors import InvalidArgument
1010

@@ -17,16 +17,16 @@ class Enum(PythonEnum):
1717
"""
1818

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

2525
@classmethod
2626
def from_string(
27-
cls: Type[Self],
28-
value: Optional[str],
29-
) -> Optional[Self]:
27+
cls: type[Self],
28+
value: str | None,
29+
) -> Self | None:
3030
"""
3131
Convert a string value into an Enum instance
3232

gvm/connections/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
from ._unix import DEFAULT_UNIX_SOCKET_PATH, UnixSocketConnection
1717

1818
__all__ = (
19-
"DEFAULT_TIMEOUT",
20-
"DEFAULT_UNIX_SOCKET_PATH",
2119
"DEFAULT_GVM_PORT",
2220
"DEFAULT_HOSTNAME",
2321
"DEFAULT_KNOWN_HOSTS_FILE",
2422
"DEFAULT_SSH_PASSWORD",
25-
"DEFAULT_SSH_USERNAME",
2623
"DEFAULT_SSH_PORT",
24+
"DEFAULT_SSH_USERNAME",
25+
"DEFAULT_TIMEOUT",
26+
"DEFAULT_UNIX_SOCKET_PATH",
2727
"DebugConnection",
2828
"GvmConnection",
2929
"SSHConnection",

gvm/connections/_connection.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import socket as socketlib
77
from abc import ABC, abstractmethod
88
from time import time
9-
from typing import Optional, Protocol, Union, runtime_checkable
9+
from typing import Protocol, runtime_checkable
1010

1111
from gvm.errors import GvmError
1212

@@ -62,8 +62,8 @@ class AbstractGvmConnection(ABC):
6262
wait indefinitely
6363
"""
6464

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

6969
def _read(self) -> bytes:

gvm/connections/_debug.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class DebugConnection:
2626
2727
logging.basicConfig(level=logging.DEBUG)
2828
29-
socket_connection = UnixSocketConnection(path='/var/run/gvm.sock')
29+
socket_connection = UnixSocketConnection(path="/var/run/gvm.sock")
3030
connection = DebugConnection(socket_connection)
3131
gmp = GMP(connection=connection)
3232
"""

gvm/connections/_ssh.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
import logging
99
import socket as socketlib
1010
import sys
11+
from collections.abc import Callable
1112
from os import PathLike
1213
from pathlib import Path
1314
from time import time
14-
from typing import Any, Callable, Optional, TextIO, Union
15+
from typing import Any, TextIO
1516

1617
import paramiko
1718
import paramiko.ssh_exception
@@ -39,16 +40,16 @@ class SSHConnection:
3940
def __init__(
4041
self,
4142
*,
42-
timeout: Optional[Union[int, float]] = DEFAULT_TIMEOUT,
43-
hostname: Optional[str] = DEFAULT_HOSTNAME,
44-
port: Optional[int] = DEFAULT_SSH_PORT,
45-
username: Optional[str] = DEFAULT_SSH_USERNAME,
46-
password: Optional[str] = DEFAULT_SSH_PASSWORD,
47-
known_hosts_file: Optional[Union[str, PathLike]] = None,
48-
auto_accept_host: Optional[bool] = None,
43+
timeout: int | float | None = DEFAULT_TIMEOUT,
44+
hostname: str | None = DEFAULT_HOSTNAME,
45+
port: int | None = DEFAULT_SSH_PORT,
46+
username: str | None = DEFAULT_SSH_USERNAME,
47+
password: str | None = DEFAULT_SSH_PASSWORD,
48+
known_hosts_file: str | PathLike | None = None,
49+
auto_accept_host: bool | None = None,
4950
file: TextIO = sys.stdout,
50-
input: Callable[[], str] = input,
51-
exit: Callable[[str], Any] = sys.exit,
51+
input: Callable[[], str] = input, # noqa: A002
52+
exit: Callable[[str], Any] = sys.exit, # noqa: A002
5253
) -> None:
5354
"""
5455
Create a new SSH connection instance.
@@ -61,7 +62,7 @@ def __init__(
6162
username: Username to use for SSH login. Default is "gmp".
6263
password: Password to use for SSH login. Default is "".
6364
"""
64-
self._client: Optional[paramiko.SSHClient] = None
65+
self._client: paramiko.SSHClient | None = None
6566
self.hostname = hostname if hostname is not None else DEFAULT_HOSTNAME
6667
self.port = int(port) if port is not None else DEFAULT_SSH_PORT
6768
self.username = (
@@ -118,8 +119,7 @@ def _auto_accept_host(
118119
key_type = key.get_name().replace("ssh-", "").upper()
119120

120121
logger.info(
121-
"Warning: Permanently added '%s' (%s) to "
122-
"the list of known hosts.",
122+
"Warning: Permanently added '%s' (%s) to the list of known hosts.",
123123
self.hostname,
124124
key_type,
125125
)
@@ -135,8 +135,7 @@ def _ssh_authentication_input_loop(
135135
key_type = key.get_name().replace("ssh-", "").upper()
136136

137137
print(
138-
f"The authenticity of host '{self.hostname}' can't "
139-
"be established.",
138+
f"The authenticity of host '{self.hostname}' can't be established.",
140139
file=self._file,
141140
)
142141
print(

gvm/connections/_tls.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import logging
66
import socket as socketlib
77
import ssl
8-
from typing import Optional, Union
98

109
from ._connection import DEFAULT_TIMEOUT, AbstractGvmConnection
1110

@@ -25,13 +24,13 @@ class TLSConnection(AbstractGvmConnection):
2524
def __init__(
2625
self,
2726
*,
28-
certfile: Optional[str] = None,
29-
cafile: Optional[str] = None,
30-
keyfile: Optional[str] = None,
31-
hostname: Optional[str] = DEFAULT_HOSTNAME,
32-
port: Optional[int] = DEFAULT_GVM_PORT,
33-
password: Optional[str] = None,
34-
timeout: Optional[Union[int, float]] = DEFAULT_TIMEOUT,
27+
certfile: str | None = None,
28+
cafile: str | None = None,
29+
keyfile: str | None = None,
30+
hostname: str | None = DEFAULT_HOSTNAME,
31+
port: int | None = DEFAULT_GVM_PORT,
32+
password: str | None = None,
33+
timeout: int | float | None = DEFAULT_TIMEOUT,
3534
) -> None:
3635
"""
3736
Create a new TLSConnection instance.

gvm/connections/_unix.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import socket as socketlib
66
from os import PathLike, fspath
7-
from typing import Optional, Union
87

98
from gvm.errors import GvmError
109

@@ -22,8 +21,8 @@ class UnixSocketConnection(AbstractGvmConnection):
2221
def __init__(
2322
self,
2423
*,
25-
path: Optional[Union[str, PathLike[str]]] = DEFAULT_UNIX_SOCKET_PATH,
26-
timeout: Optional[Union[int, float]] = DEFAULT_TIMEOUT,
24+
path: str | PathLike[str] | None = DEFAULT_UNIX_SOCKET_PATH,
25+
timeout: int | float | None = DEFAULT_TIMEOUT,
2726
) -> None:
2827
"""
2928
Create a new UnixSocketConnection instance.

gvm/errors.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@
66
Module for GVM errors
77
"""
88

9-
from typing import Optional
10-
119

1210
class GvmError(Exception):
1311
"""An exception for gvm errors
1412
1513
Base class for all exceptions originating in python-gvm.
1614
"""
1715

18-
def __init__(self, message: Optional[str], *args):
16+
def __init__(self, message: str | None, *args):
1917
super().__init__(message, *args)
2018
self.message = message
2119

@@ -44,9 +42,7 @@ class GvmServerError(GvmError):
4442
and function
4543
"""
4644

47-
def __init__(
48-
self, status: Optional[str] = None, message: Optional[str] = None
49-
):
45+
def __init__(self, status: str | None = None, message: str | None = None):
5046
super().__init__(message, status)
5147
self.status = status
5248

@@ -71,9 +67,7 @@ class GvmResponseError(GvmClientError):
7167
and function
7268
"""
7369

74-
def __init__(
75-
self, status: Optional[str] = None, message: Optional[str] = None
76-
):
70+
def __init__(self, status: str | None = None, message: str | None = None):
7771
super().__init__(message, status)
7872
self.status = status
7973

@@ -87,7 +81,7 @@ def __repr__(self):
8781
)
8882

8983

90-
class InvalidArgument(GvmError):
84+
class InvalidArgument(GvmError): # noqa: N818
9185
"""Raised if an invalid argument/parameter is passed
9286
9387
Derives from :py:class:`GvmError`
@@ -101,10 +95,10 @@ class InvalidArgument(GvmError):
10195

10296
def __init__(
10397
self,
104-
message: Optional[str] = None,
98+
message: str | None = None,
10599
*,
106-
argument: Optional[str] = None,
107-
function: Optional[str] = None,
100+
argument: str | None = None,
101+
function: str | None = None,
108102
):
109103
super().__init__(message, argument, function)
110104
self.argument = argument
@@ -123,7 +117,7 @@ def __str__(self):
123117
return f"Invalid argument {self.argument} for {self.function}"
124118

125119

126-
class InvalidArgumentType(GvmError):
120+
class InvalidArgumentType(GvmError): # noqa: N818
127121
"""Raised if a passed argument has an invalid type
128122
129123
Derives from :py:class:`GvmError`
@@ -138,8 +132,8 @@ def __init__(
138132
self,
139133
argument: str,
140134
*,
141-
arg_type: Optional[str] = None,
142-
function: Optional[str] = None,
135+
arg_type: str | None = None,
136+
function: str | None = None,
143137
):
144138
super().__init__(None)
145139
self.argument = argument
@@ -165,7 +159,7 @@ def __str__(self):
165159
return f"Invalid argument type for argument {self.argument}."
166160

167161

168-
class RequiredArgument(GvmError):
162+
class RequiredArgument(GvmError): # noqa: N818
169163
"""Raised if a required argument/parameter is missing
170164
171165
Derives from :py:class:`GvmError`
@@ -179,10 +173,10 @@ class RequiredArgument(GvmError):
179173

180174
def __init__(
181175
self,
182-
message: Optional[str] = None,
176+
message: str | None = None,
183177
*,
184-
argument: Optional[str] = None,
185-
function: Optional[str] = None,
178+
argument: str | None = None,
179+
function: str | None = None,
186180
):
187181
super().__init__(message, argument, function)
188182
self.argument = argument

0 commit comments

Comments
 (0)