commands.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from __future__ import annotations
  2. import json
  3. from functools import lru_cache
  4. from pathlib import Path
  5. from .models import PortingBacklog, PortingModule
  6. SNAPSHOT_PATH = Path(__file__).resolve().parent / 'reference_data' / 'commands_snapshot.json'
  7. @lru_cache(maxsize=1)
  8. def load_command_snapshot() -> tuple[PortingModule, ...]:
  9. raw_entries = json.loads(SNAPSHOT_PATH.read_text())
  10. return tuple(
  11. PortingModule(
  12. name=entry['name'],
  13. responsibility=entry['responsibility'],
  14. source_hint=entry['source_hint'],
  15. status='mirrored',
  16. )
  17. for entry in raw_entries
  18. )
  19. PORTED_COMMANDS = load_command_snapshot()
  20. def build_command_backlog() -> PortingBacklog:
  21. return PortingBacklog(title='Command surface', modules=list(PORTED_COMMANDS))
  22. def command_names() -> list[str]:
  23. return [module.name for module in PORTED_COMMANDS]
  24. def get_command(name: str) -> PortingModule | None:
  25. needle = name.lower()
  26. for module in PORTED_COMMANDS:
  27. if module.name.lower() == needle:
  28. return module
  29. return None
  30. def find_commands(query: str, limit: int = 20) -> list[PortingModule]:
  31. needle = query.lower()
  32. matches = [module for module in PORTED_COMMANDS if needle in module.name.lower() or needle in module.source_hint.lower()]
  33. return matches[:limit]
  34. def render_command_index(limit: int = 20, query: str | None = None) -> str:
  35. modules = find_commands(query, limit) if query else list(PORTED_COMMANDS[:limit])
  36. lines = [f'Command entries: {len(PORTED_COMMANDS)}', '']
  37. if query:
  38. lines.append(f'Filtered by: {query}')
  39. lines.append('')
  40. lines.extend(f'- {module.name} — {module.source_hint}' for module in modules)
  41. return '\n'.join(lines)
备用站点 当前处于降级运行的备用站点,仅供应急访问,数据和功能可能不是最新。