feat: добавление классов для работы с target
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .xray_manager_config import XrayManagerConfig
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Target:
|
||||
id: str
|
||||
pretty: str
|
||||
route: str
|
||||
|
||||
|
||||
class TargetStorage:
|
||||
def __init__(self, xray_manager_config: XrayManagerConfig):
|
||||
self.xrmc = xray_manager_config
|
||||
|
||||
@property
|
||||
def target_dict(self) -> dict[str, dict[str, str]]:
|
||||
return self.xrmc.config.get("target", {})
|
||||
|
||||
def load_by_id(self, target_id: str) -> Target:
|
||||
if target_id not in self.target_dict:
|
||||
raise ValueError(f"Target not found: {target_id}")
|
||||
|
||||
target_data = self.target_dict[target_id]
|
||||
|
||||
if "route" not in target_data:
|
||||
raise ValueError(
|
||||
f"Missing required 'route' field for target: {target_id}"
|
||||
)
|
||||
|
||||
return Target(
|
||||
id=target_id,
|
||||
pretty=target_data.get("pretty_name", target_id),
|
||||
route=target_data["route"]
|
||||
)
|
||||
|
||||
def load_by_pretty(self, target_pretty_name: str) -> Target:
|
||||
for target_id, target_data in self.target_dict.items():
|
||||
if target_data.get("pretty_name", target_id) == target_pretty_name:
|
||||
return self.load_by_id(target_id)
|
||||
|
||||
raise ValueError(f"Target pretty name not found: {target_pretty_name}")
|
||||
|
||||
def load_all_targets(self) -> list[Target]:
|
||||
targets = [
|
||||
self.load_by_id(target_id) for target_id in self.target_dict.keys()
|
||||
]
|
||||
return targets
|
||||
|
||||
def save_target(self, target: Target):
|
||||
self.xrmc.set_target(target.id, target.pretty, target.route)
|
||||
|
||||
def delete_target(self, target: Target):
|
||||
if target not in self.load_all_targets():
|
||||
raise ValueError(f"Target not found: {target.id}")
|
||||
|
||||
self.xrmc.delete_target(target.id)
|
||||
Reference in New Issue
Block a user