Update project configuration and dependencies
- Added `.vscode` to `.gitignore` to exclude Visual Studio Code settings. - Updated `pyproject.toml` to include `ruff` for linting and configured its settings. - Modified `uv.lock` to include `ruff` in both optional and development dependencies. - Refactored type hints in several files to use `str | None` for optional parameters. - Cleaned up unused imports and whitespace in various modules for improved code clarity.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
from .core import OxiAPI
|
||||
|
||||
|
||||
__all__ = [
|
||||
"OxiAPI",
|
||||
]
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
from typing import Optional
|
||||
from requests.adapters import HTTPAdapter
|
||||
from urllib3.util import Retry
|
||||
|
||||
@@ -6,7 +5,7 @@ from urllib3.util import Retry
|
||||
class OxiAdapter(HTTPAdapter):
|
||||
def __init__(
|
||||
self,
|
||||
timeout: Optional[int] = None,
|
||||
timeout: int | None = None,
|
||||
max_retries: int = 3,
|
||||
*args,
|
||||
**kwargs,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from functools import cached_property
|
||||
import json
|
||||
from typing import TYPE_CHECKING, Generic, Iterator, TypeVar
|
||||
from collections.abc import Iterator
|
||||
from functools import cached_property
|
||||
from typing import TYPE_CHECKING, Generic, TypeVar
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
10
oxi/core.py
10
oxi/core.py
@@ -1,8 +1,8 @@
|
||||
from typing import Optional
|
||||
from requests import HTTPError, Session
|
||||
|
||||
from oxi.adapter import OxiAdapter
|
||||
from oxi.exception import OxiAPIError
|
||||
|
||||
from .node import Node
|
||||
|
||||
|
||||
@@ -10,8 +10,8 @@ class OxiAPI:
|
||||
def __init__(
|
||||
self,
|
||||
url: str,
|
||||
username: Optional[str] = None,
|
||||
password: Optional[str] = None,
|
||||
username: str | None = None,
|
||||
password: str | None = None,
|
||||
verify: bool = True,
|
||||
):
|
||||
self.base_url = url.rstrip("/")
|
||||
@@ -20,8 +20,8 @@ class OxiAPI:
|
||||
|
||||
def __create_session(
|
||||
self,
|
||||
username: Optional[str] = None,
|
||||
password: Optional[str] = None,
|
||||
username: str | None = None,
|
||||
password: str | None = None,
|
||||
verify: bool = True,
|
||||
) -> Session:
|
||||
session = Session()
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from requests import HTTPError
|
||||
@@ -35,7 +35,7 @@ def _looks_like_node_not_found_html(e: "HTTPError") -> bool:
|
||||
|
||||
|
||||
class OxiAPIError(Exception):
|
||||
def __init__(self, message: str, status_code: Optional[int] = None):
|
||||
def __init__(self, message: str, status_code: int | None = None):
|
||||
super().__init__(message)
|
||||
self.status_code = status_code
|
||||
self.message = message
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Callable, Type
|
||||
from collections.abc import Callable
|
||||
|
||||
from .base import BaseDevice
|
||||
|
||||
@@ -7,7 +7,7 @@ device_registry = {}
|
||||
|
||||
def register_parser(
|
||||
name: list[str] | str,
|
||||
) -> Callable[[Type[BaseDevice]], Type[BaseDevice]]:
|
||||
) -> Callable[[type[BaseDevice]], type[BaseDevice]]:
|
||||
def wrapper(cls):
|
||||
name_list = []
|
||||
if isinstance(name, str):
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import xml.etree.ElementTree as ET
|
||||
from abc import ABC, abstractmethod
|
||||
from pathlib import Path
|
||||
|
||||
from ttp import ttp
|
||||
|
||||
from oxi.exception import OxiAPIError
|
||||
from oxi.interfaces.contract import Device
|
||||
import xml.etree.ElementTree as ET
|
||||
from oxi.interfaces.contract import Interfaces, System, Vlans
|
||||
from oxi.interfaces.contract import Device, Interfaces, System, Vlans
|
||||
|
||||
|
||||
class BaseDevice(ABC):
|
||||
@@ -40,7 +41,7 @@ class BaseDevice(ABC):
|
||||
|
||||
Raises:
|
||||
ValueError: if raw data cannot be validated by the contract.
|
||||
"""
|
||||
""" # noqa: E501
|
||||
return self.raw.get("vlans", [])
|
||||
|
||||
def interfaces(self) -> list[dict]:
|
||||
@@ -52,7 +53,7 @@ class BaseDevice(ABC):
|
||||
|
||||
Raises:
|
||||
ValueError: if raw data cannot be validated by the contract.
|
||||
"""
|
||||
""" # noqa: E501
|
||||
return self.raw.get("interfaces", [])
|
||||
|
||||
def system(self) -> dict:
|
||||
@@ -82,11 +83,7 @@ class BaseDevice(ABC):
|
||||
|
||||
def _validate_contract(self) -> dict:
|
||||
if self.raw is None:
|
||||
msg = (
|
||||
f"Node {self.name} not found"
|
||||
if self.name
|
||||
else "Node not found"
|
||||
)
|
||||
msg = f"Node {self.name} not found" if self.name else "Node not found"
|
||||
raise OxiAPIError(msg, status_code=404)
|
||||
system_data = self.system()
|
||||
interfaces_data = self._as_list(self.interfaces())
|
||||
@@ -99,8 +96,8 @@ class BaseDevice(ABC):
|
||||
if "vlans" in self._declared_sections:
|
||||
if "vlans" not in self.raw:
|
||||
raise ValueError(
|
||||
f"{self.__class__.__name__}: template '{self.template}' declares optional group "
|
||||
f"'vlans', but TTP did not return it."
|
||||
f"{self.__class__.__name__}: template '{self.template}' "
|
||||
f"declares optional group 'vlans', but TTP did not return it."
|
||||
)
|
||||
vlans_data = self._as_list(self.vlans())
|
||||
result["vlans"] = [Vlans(**item) for item in vlans_data]
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from ipaddress import IPv4Address
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
|
||||
|
||||
@@ -3,5 +3,5 @@ import pkgutil
|
||||
|
||||
package = __package__
|
||||
|
||||
for loader, module_name, is_pkg in pkgutil.iter_modules(__path__):
|
||||
for _, module_name, _ in pkgutil.iter_modules(__path__):
|
||||
importlib.import_module(f"{package}.{module_name}")
|
||||
|
||||
@@ -14,4 +14,4 @@ class H3C(BaseDevice):
|
||||
vlans.append(item)
|
||||
continue
|
||||
vlans.extend({"vlan_id": vlan_id} for vlan_id in vlan_ids)
|
||||
return vlans
|
||||
return vlans
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from ipaddress import ip_interface
|
||||
|
||||
from oxi.interfaces import register_parser
|
||||
from oxi.interfaces.base import BaseDevice
|
||||
from oxi.interfaces.utils import decode_utf
|
||||
|
||||
@@ -6,7 +6,6 @@ from oxi.exception import OxiAPIError
|
||||
|
||||
from .view import NodeView
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from requests import Session
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ from typing import TYPE_CHECKING
|
||||
|
||||
from .conf import NodeConfig
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from requests import Session
|
||||
|
||||
|
||||
Reference in New Issue
Block a user