mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-01-30 23:08:59 -06:00
The websocket included script
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
# "rich",
|
# "rich",
|
||||||
# "ijson",
|
# "ijson",
|
||||||
# "typer-slim",
|
# "typer-slim",
|
||||||
|
# "websockets",
|
||||||
# ]
|
# ]
|
||||||
# ///
|
# ///
|
||||||
|
|
||||||
@@ -14,6 +15,7 @@ from pathlib import Path
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
from typing import TypedDict
|
from typing import TypedDict
|
||||||
|
|
||||||
|
import ijson
|
||||||
import typer
|
import typer
|
||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
from rich.progress import BarColumn
|
from rich.progress import BarColumn
|
||||||
@@ -22,14 +24,8 @@ from rich.progress import SpinnerColumn
|
|||||||
from rich.progress import TextColumn
|
from rich.progress import TextColumn
|
||||||
from rich.progress import TimeElapsedColumn
|
from rich.progress import TimeElapsedColumn
|
||||||
from rich.table import Table
|
from rich.table import Table
|
||||||
|
from websockets.sync.client import ClientConnection
|
||||||
try:
|
from websockets.sync.client import connect
|
||||||
import ijson # type: ignore
|
|
||||||
except ImportError as exc: # pragma: no cover - handled at runtime
|
|
||||||
raise SystemExit(
|
|
||||||
"ijson is required for migration transform. "
|
|
||||||
"Install dependencies (e.g., `uv pip install ijson`).",
|
|
||||||
) from exc
|
|
||||||
|
|
||||||
app = typer.Typer(add_completion=False)
|
app = typer.Typer(add_completion=False)
|
||||||
console = Console()
|
console = Console()
|
||||||
@@ -80,6 +76,8 @@ def migrate(
|
|||||||
"-o",
|
"-o",
|
||||||
callback=validate_output,
|
callback=validate_output,
|
||||||
),
|
),
|
||||||
|
ws_url: str | None = typer.Option(None, "--ws"),
|
||||||
|
update_frequency: int = typer.Option(100, "--freq"),
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
Process JSON fixtures with detailed summary and timing.
|
Process JSON fixtures with detailed summary and timing.
|
||||||
@@ -94,6 +92,15 @@ def migrate(
|
|||||||
total_processed: int = 0
|
total_processed: int = 0
|
||||||
start_time: float = time.perf_counter()
|
start_time: float = time.perf_counter()
|
||||||
|
|
||||||
|
ws: ClientConnection | None = None
|
||||||
|
if ws_url:
|
||||||
|
try:
|
||||||
|
ws = connect(ws_url)
|
||||||
|
except Exception as e:
|
||||||
|
console.print(
|
||||||
|
f"[yellow]Warning: Could not connect to WebSocket: {e}[/yellow]",
|
||||||
|
)
|
||||||
|
|
||||||
progress = Progress(
|
progress = Progress(
|
||||||
SpinnerColumn(),
|
SpinnerColumn(),
|
||||||
TextColumn("[bold blue]{task.description}"),
|
TextColumn("[bold blue]{task.description}"),
|
||||||
@@ -103,6 +110,7 @@ def migrate(
|
|||||||
console=console,
|
console=console,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
with (
|
with (
|
||||||
progress,
|
progress,
|
||||||
input_path.open("rb") as infile,
|
input_path.open("rb") as infile,
|
||||||
@@ -129,8 +137,23 @@ def migrate(
|
|||||||
json.dump(fixture, outfile, ensure_ascii=False)
|
json.dump(fixture, outfile, ensure_ascii=False)
|
||||||
progress.advance(task, 1)
|
progress.advance(task, 1)
|
||||||
|
|
||||||
|
if ws and (i % update_frequency == 0):
|
||||||
|
ws.send(
|
||||||
|
json.dumps(
|
||||||
|
{
|
||||||
|
"task": "processing",
|
||||||
|
"completed": total_processed,
|
||||||
|
"stats": dict(stats),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
outfile.write("\n]\n")
|
outfile.write("\n]\n")
|
||||||
|
|
||||||
|
finally:
|
||||||
|
if ws:
|
||||||
|
ws.close()
|
||||||
|
|
||||||
end_time: float = time.perf_counter()
|
end_time: float = time.perf_counter()
|
||||||
duration: float = end_time - start_time
|
duration: float = end_time - start_time
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user