"""Read-only, allowlisted project code reader for Hermes; no arbitrary path access."""
from pathlib import Path
ROOT=Path('/var/www/html/NFL_ST').resolve()
ALLOW={"nfl.py","panel.html","hermes_api.py","hermes_brain.py","hermes_brain_v2.py","hermes_memory.py","hermes_tools.py","hermes_football.py","hermes_lmp_v5_runtime.py","hermes_lmp_coordinator.py","hermes_lmp_actions.py","hermes_epg_tool.py","hermes_research_v2.py"}
DOC=Path('/var/lib/hermes-nexus/knowledge/LMP_V5.md')
def leer_codigo(nombre, inicio=1, cantidad=100):
    if nombre not in ALLOW: raise ValueError('Archivo no autorizado')
    path=(ROOT/nombre).resolve()
    if path.parent != ROOT or not path.is_file(): raise ValueError('Archivo no disponible')
    inicio=max(1,int(inicio));cantidad=max(1,min(150,int(cantidad)))
    with path.open(encoding='utf-8',errors='replace') as f:
        from itertools import islice
        lineas=list(islice(f,inicio-1,inicio-1+cantidad))
    return {'archivo':nombre,'inicio':inicio,'lineas':[(inicio+i,s.rstrip('\n')) for i,s in enumerate(lineas)]}
def listar_codigo():
    return [name for name in sorted(ALLOW) if (ROOT/name).is_file()]
def leer_conocimiento_lmp():
    return DOC.read_text(encoding='utf-8') if DOC.is_file() else 'Documentación no instalada'
