Satisfy flake8 so the lint job stops failing
Pure formatting, no behaviour change: strip trailing whitespace from blank lines, give top-level defs in system_cacher.py their two blank lines, wrap the long discord_radio.join signature, and split the duplicated active_config ternary in main.py and routers/api.py across lines. Verified clean with flake8 --max-line-length=120, matching CI. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
9d86304b8a
commit
7f1d09c753
@@ -27,7 +27,8 @@ class RadioBot:
|
|||||||
self._channel_id: Optional[int] = None
|
self._channel_id: Optional[int] = None
|
||||||
self._was_streaming: bool = False
|
self._was_streaming: bool = False
|
||||||
|
|
||||||
async def join(self, guild_id: int, channel_id: int, token: str, call_active: bool = False, system_name: str = None) -> bool:
|
async def join(self, guild_id: int, channel_id: int, token: str,
|
||||||
|
call_active: bool = False, system_name: str = None) -> bool:
|
||||||
# (Re)start the bot if the token changed or the bot isn't running
|
# (Re)start the bot if the token changed or the bot isn't running
|
||||||
if self._current_token != token or not self._is_bot_running():
|
if self._current_token != token or not self._is_bot_running():
|
||||||
if not await self._start_bot(token):
|
if not await self._start_bot(token):
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ from app.internal import credentials
|
|||||||
|
|
||||||
_CACHE_FILE = Path(settings.config_path) / "systems_cache.json"
|
_CACHE_FILE = Path(settings.config_path) / "systems_cache.json"
|
||||||
|
|
||||||
|
|
||||||
async def fetch_and_cache_systems() -> bool:
|
async def fetch_and_cache_systems() -> bool:
|
||||||
"""Fetch all systems from the C2 server and cache them locally."""
|
"""Fetch all systems from the C2 server and cache them locally."""
|
||||||
if not settings.c2_url:
|
if not settings.c2_url:
|
||||||
@@ -23,7 +24,7 @@ async def fetch_and_cache_systems() -> bool:
|
|||||||
r = await client.get(url, headers=headers)
|
r = await client.get(url, headers=headers)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
systems = r.json()
|
systems = r.json()
|
||||||
|
|
||||||
_CACHE_FILE.parent.mkdir(parents=True, exist_ok=True)
|
_CACHE_FILE.parent.mkdir(parents=True, exist_ok=True)
|
||||||
_CACHE_FILE.write_text(json.dumps(systems, indent=2))
|
_CACHE_FILE.write_text(json.dumps(systems, indent=2))
|
||||||
logger.info(f"Cached {len(systems)} systems from C2.")
|
logger.info(f"Cached {len(systems)} systems from C2.")
|
||||||
@@ -32,6 +33,7 @@ async def fetch_and_cache_systems() -> bool:
|
|||||||
logger.warning(f"Failed to fetch systems from C2: {e}. Offline cache will be used.")
|
logger.warning(f"Failed to fetch systems from C2: {e}. Offline cache will be used.")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def load_cached_systems() -> List[Dict[str, Any]]:
|
def load_cached_systems() -> List[Dict[str, Any]]:
|
||||||
"""Load cached systems from disk."""
|
"""Load cached systems from disk."""
|
||||||
if _CACHE_FILE.exists():
|
if _CACHE_FILE.exists():
|
||||||
@@ -41,6 +43,7 @@ def load_cached_systems() -> List[Dict[str, Any]]:
|
|||||||
logger.error(f"Failed to read systems cache: {e}")
|
logger.error(f"Failed to read systems cache: {e}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
def get_cached_system(system_id: str) -> Optional[Dict[str, Any]]:
|
def get_cached_system(system_id: str) -> Optional[Dict[str, Any]]:
|
||||||
"""Retrieve a single system config from the cache."""
|
"""Retrieve a single system config from the cache."""
|
||||||
systems = load_cached_systems()
|
systems = load_cached_systems()
|
||||||
|
|||||||
@@ -297,7 +297,11 @@ async def lifespan(app: FastAPI):
|
|||||||
initial_status = "online" if node_cfg.configured else "unconfigured"
|
initial_status = "online" if node_cfg.configured else "unconfigured"
|
||||||
await mqtt_manager.publish_status(initial_status)
|
await mqtt_manager.publish_status(initial_status)
|
||||||
|
|
||||||
active_config = node_cfg.override_config if (node_cfg.override_system_id and node_cfg.override_config) else node_cfg.system_config
|
active_config = (
|
||||||
|
node_cfg.override_config
|
||||||
|
if (node_cfg.override_system_id and node_cfg.override_config)
|
||||||
|
else node_cfg.system_config
|
||||||
|
)
|
||||||
if node_cfg.configured and active_config:
|
if node_cfg.configured and active_config:
|
||||||
from app.internal.op25_client import op25_client
|
from app.internal.op25_client import op25_client
|
||||||
logger.info("Node is configured — waiting for OP25 API then generating config.")
|
logger.info("Node is configured — waiting for OP25 API then generating config.")
|
||||||
|
|||||||
@@ -25,12 +25,16 @@ router = APIRouter(prefix="/api", tags=["api"], dependencies=[Depends(auth.requi
|
|||||||
async def get_status():
|
async def get_status():
|
||||||
node_cfg = load_node_config()
|
node_cfg = load_node_config()
|
||||||
op25_status = await op25_client.status()
|
op25_status = await op25_client.status()
|
||||||
|
|
||||||
active_tgid = metadata_watcher.current_tgid
|
active_tgid = metadata_watcher.current_tgid
|
||||||
active_tgid_name = metadata_watcher.current_tgid_name
|
active_tgid_name = metadata_watcher.current_tgid_name
|
||||||
system_name = None
|
system_name = None
|
||||||
|
|
||||||
active_config = node_cfg.override_config if (node_cfg.override_system_id and node_cfg.override_config) else node_cfg.system_config
|
active_config = (
|
||||||
|
node_cfg.override_config
|
||||||
|
if (node_cfg.override_system_id and node_cfg.override_config)
|
||||||
|
else node_cfg.system_config
|
||||||
|
)
|
||||||
if active_config:
|
if active_config:
|
||||||
system_name = active_config.name
|
system_name = active_config.name
|
||||||
if active_tgid:
|
if active_tgid:
|
||||||
@@ -126,7 +130,7 @@ async def set_override(
|
|||||||
):
|
):
|
||||||
node_cfg = load_node_config()
|
node_cfg = load_node_config()
|
||||||
config = None
|
config = None
|
||||||
|
|
||||||
if system_id:
|
if system_id:
|
||||||
from app.internal.system_cacher import get_cached_system
|
from app.internal.system_cacher import get_cached_system
|
||||||
cached = get_cached_system(system_id)
|
cached = get_cached_system(system_id)
|
||||||
@@ -139,19 +143,19 @@ async def set_override(
|
|||||||
config = SystemConfig(**system_config)
|
config = SystemConfig(**system_config)
|
||||||
else:
|
else:
|
||||||
raise HTTPException(400, "Must specify system_id or system_config.")
|
raise HTTPException(400, "Must specify system_id or system_config.")
|
||||||
|
|
||||||
node_cfg.override_system_id = config.system_id
|
node_cfg.override_system_id = config.system_id
|
||||||
node_cfg.override_config = config
|
node_cfg.override_config = config
|
||||||
save_node_config(node_cfg)
|
save_node_config(node_cfg)
|
||||||
|
|
||||||
from app.main import _generate_op25_config
|
from app.main import _generate_op25_config
|
||||||
if not await _generate_op25_config(config):
|
if not await _generate_op25_config(config):
|
||||||
raise HTTPException(500, f"Failed to generate OP25 config for override: {config.name}")
|
raise HTTPException(500, f"Failed to generate OP25 config for override: {config.name}")
|
||||||
|
|
||||||
await op25_client.stop()
|
await op25_client.stop()
|
||||||
await asyncio.sleep(2)
|
await asyncio.sleep(2)
|
||||||
await op25_client.start()
|
await op25_client.start()
|
||||||
|
|
||||||
await mqtt_manager._publish_checkin()
|
await mqtt_manager._publish_checkin()
|
||||||
return {"ok": True}
|
return {"ok": True}
|
||||||
|
|
||||||
@@ -161,20 +165,20 @@ async def revert_config():
|
|||||||
node_cfg = load_node_config()
|
node_cfg = load_node_config()
|
||||||
if not node_cfg.override_system_id:
|
if not node_cfg.override_system_id:
|
||||||
return {"ok": True, "message": "No override active."}
|
return {"ok": True, "message": "No override active."}
|
||||||
|
|
||||||
node_cfg.override_system_id = None
|
node_cfg.override_system_id = None
|
||||||
node_cfg.override_config = None
|
node_cfg.override_config = None
|
||||||
save_node_config(node_cfg)
|
save_node_config(node_cfg)
|
||||||
|
|
||||||
if node_cfg.system_config:
|
if node_cfg.system_config:
|
||||||
from app.main import _generate_op25_config
|
from app.main import _generate_op25_config
|
||||||
if not await _generate_op25_config(node_cfg.system_config):
|
if not await _generate_op25_config(node_cfg.system_config):
|
||||||
raise HTTPException(500, "Failed to regenerate original OP25 config.")
|
raise HTTPException(500, "Failed to regenerate original OP25 config.")
|
||||||
|
|
||||||
await op25_client.stop()
|
await op25_client.stop()
|
||||||
await asyncio.sleep(2)
|
await asyncio.sleep(2)
|
||||||
await op25_client.start()
|
await op25_client.start()
|
||||||
|
|
||||||
await mqtt_manager._publish_checkin()
|
await mqtt_manager._publish_checkin()
|
||||||
return {"ok": True}
|
return {"ok": True}
|
||||||
|
|
||||||
@@ -183,10 +187,10 @@ async def revert_config():
|
|||||||
async def ack_override(timeout_minutes: int = Body(1440)):
|
async def ack_override(timeout_minutes: int = Body(1440)):
|
||||||
if not settings.c2_url:
|
if not settings.c2_url:
|
||||||
raise HTTPException(400, "C2_URL not configured.")
|
raise HTTPException(400, "C2_URL not configured.")
|
||||||
|
|
||||||
api_key = credentials.get_api_key()
|
api_key = credentials.get_api_key()
|
||||||
headers = {"Authorization": f"Bearer {api_key}"} if api_key else {}
|
headers = {"Authorization": f"Bearer {api_key}"} if api_key else {}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with httpx.AsyncClient(timeout=10) as client:
|
async with httpx.AsyncClient(timeout=10) as client:
|
||||||
r = await client.post(
|
r = await client.post(
|
||||||
|
|||||||
Reference in New Issue
Block a user