72 lines
2 KiB
Python
72 lines
2 KiB
Python
from textual.widgets import DataTable, Button
|
|
from textual.containers import Vertical, Horizontal
|
|
from textual.message import Message
|
|
|
|
try:
|
|
import pyperclip
|
|
PYPERCLIP_OK = True
|
|
except Exception:
|
|
PYPERCLIP_OK = False
|
|
|
|
|
|
class CVETable(Vertical):
|
|
DEFAULT_CSS = """
|
|
CVETable {
|
|
padding: 1 2;
|
|
}
|
|
#cve-toolbar {
|
|
height: auto;
|
|
margin-bottom: 1;
|
|
}
|
|
DataTable {
|
|
height: 1fr;
|
|
}
|
|
"""
|
|
|
|
class BackPressed(Message):
|
|
pass
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.table = None
|
|
self.urls = {}
|
|
|
|
def compose(self):
|
|
with Horizontal(id="cve-toolbar"):
|
|
yield Button("⬅ Retour", id="cve-back")
|
|
self.table = DataTable(id="cve-table")
|
|
yield self.table
|
|
|
|
def on_mount(self):
|
|
self.table.add_columns("CVE-ID", "Paquet", "Lien")
|
|
self.table.cursor_type = "row"
|
|
|
|
def load_data(self, cves: list[dict]):
|
|
self.table.clear()
|
|
self.urls = {}
|
|
for i, cve in enumerate(cves):
|
|
cve_id = cve.get("id", "?")
|
|
pkg = cve.get("package", "?")
|
|
url = cve.get("url", "")
|
|
self.urls[i] = url
|
|
self.table.add_row(cve_id, pkg, url)
|
|
|
|
def on_data_table_row_selected(self, event: DataTable.RowSelected):
|
|
row_key = event.row_key
|
|
row_index = list(self.table.rows.keys()).index(row_key)
|
|
url = self.urls.get(row_index, "")
|
|
if not url:
|
|
return
|
|
if PYPERCLIP_OK:
|
|
try:
|
|
pyperclip.copy(url)
|
|
self.notify(f"Lien copié : {url}", severity="information")
|
|
return
|
|
except Exception:
|
|
pass
|
|
# Fallback : afficher le lien dans une notification
|
|
self.notify(f"Lien (copie manuelle) : {url}", severity="warning")
|
|
|
|
def on_button_pressed(self, event: Button.Pressed):
|
|
if event.button.id == "cve-back":
|
|
self.post_message(self.BackPressed())
|