add logging and docstrings

This commit is contained in:
2025-09-20 13:55:40 -05:00
parent c01d86c25d
commit 1aead1935b

View File

@@ -1,33 +1,55 @@
import pathlib
"""Page fetching functionality for Ren Browser.
Handles downloading pages from the Reticulum network using
the nomadnetwork protocol.
"""
import threading
import time
from dataclasses import dataclass
import RNS
from ren_browser.storage.storage import get_rns_config_directory
@dataclass
class PageRequest:
"""Represents a request for a page from the Reticulum network.
Contains the destination hash, page path, and optional field data.
"""
destination_hash: str
page_path: str
field_data: dict | None = None
class PageFetcher:
"""Fetcher to download pages from the Reticulum network.
"""
"""Fetcher to download pages from the Reticulum network."""
def __init__(self):
config_dir = pathlib.Path(__file__).resolve().parents[2] / "config"
"""Initialize the page fetcher and Reticulum connection."""
config_dir = get_rns_config_directory()
try:
RNS.Reticulum(str(config_dir))
from ren_browser.logs import setup_rns_logging
setup_rns_logging()
except (OSError, ValueError):
pass
def fetch_page(self, req: PageRequest) -> str:
"""Download page content for the given PageRequest.
Args:
req: PageRequest containing destination and path information.
Returns:
str: The downloaded page content.
Raises:
Exception: If no path to destination or identity not found.
"""
RNS.log(f"PageFetcher: starting fetch of {req.page_path} from {req.destination_hash}")
"""
Download page content for the given PageRequest.
"""
dest_bytes = bytes.fromhex(req.destination_hash)
if not RNS.Transport.has_path(dest_bytes):
RNS.Transport.request_path(dest_bytes)
@@ -38,25 +60,25 @@ class PageFetcher:
time.sleep(0.1)
identity = RNS.Identity.recall(dest_bytes)
if not identity:
raise Exception('Identity not found')
raise Exception("Identity not found")
destination = RNS.Destination(
identity,
RNS.Destination.OUT,
RNS.Destination.SINGLE,
'nomadnetwork',
'node',
"nomadnetwork",
"node",
)
link = RNS.Link(destination)
result = {'data': None}
result = {"data": None}
ev = threading.Event()
def on_response(receipt):
data = receipt.response
if isinstance(data, bytes):
result['data'] = data.decode('utf-8')
result["data"] = data.decode("utf-8")
else:
result['data'] = str(data)
result["data"] = str(data)
ev.set()
def on_failed(_):
@@ -66,6 +88,6 @@ class PageFetcher:
lambda link: link.request(req.page_path, req.field_data, response_callback=on_response, failed_callback=on_failed)
)
ev.wait(timeout=15)
data_str = result['data'] or 'No content received'
data_str = result["data"] or "No content received"
RNS.log(f"PageFetcher: received data for {req.destination_hash}:{req.page_path}")
return data_str