-
Notifications
You must be signed in to change notification settings - Fork 61
Feature 198 custom overlay themes #201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TomHacker69
wants to merge
2
commits into
Devnil434:main
Choose a base branch
from
TomHacker69:feature-198-custom-overlay-themes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| """ | ||
| Auto-discover and register all routers in this package. | ||
| Each module may expose one or more APIRouter instances. | ||
| """ | ||
| from __future__ import annotations | ||
| import importlib | ||
| import pkgutil | ||
| import logging | ||
|
|
||
| from fastapi import APIRouter | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| _discovered_router = APIRouter() | ||
|
|
||
|
|
||
| def _autodiscover(): | ||
| package_dir = __path__[0] | ||
| for _, module_name, _ in pkgutil.iter_modules([package_dir]): | ||
| if module_name in ("__init__",): | ||
| continue | ||
| module = importlib.import_module(f"{__name__}.{module_name}") | ||
| for attr_name in dir(module): | ||
| obj = getattr(module, attr_name) | ||
| if isinstance(obj, APIRouter): | ||
| _discovered_router.include_router(obj) | ||
| logger.debug("Discovered router %s from %s", attr_name, module_name) | ||
|
|
||
|
|
||
| _autodiscover() | ||
|
|
||
| router = _discovered_router |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| """ | ||
| Detection Overlay Theme Management API | ||
|
|
||
| Endpoints: | ||
| GET /themes - List all themes (presets + custom) | ||
| GET /themes/{name} - Get a specific theme | ||
| POST /themes - Save a custom theme | ||
| DELETE /themes/{name} - Delete a custom theme | ||
| """ | ||
| from __future__ import annotations | ||
| import logging | ||
| import json | ||
| import os | ||
|
|
||
| from fastapi import APIRouter, HTTPException | ||
| from pydantic import BaseModel, Field | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| router = APIRouter(prefix="/themes", tags=["themes"]) | ||
|
|
||
| THEME_DIR = os.path.join(os.path.dirname(__file__), "..", "..", "config", "themes") | ||
| THEMES_FILE = os.path.join(THEME_DIR, "custom_themes.json") | ||
|
|
||
| os.makedirs(THEME_DIR, exist_ok=True) | ||
|
|
||
| PRESETS = { | ||
| "default": { | ||
| "name": "Default", | ||
| "description": "Standard surveillance overlay", | ||
| "boundingBoxColors": {"palette": ["#ef4444", "#3b82f6", "#22c55e", "#f59e0b", "#a855f7"], "opacity": 0.8, "borderWidth": 4}, | ||
| "label": {"visible": True, "fontSize": 12, "fontFamily": "monospace", "backgroundColor": "rgba(0,0,0,0.6)", "textColor": "#ffffff"}, | ||
| "confidence": {"visible": True, "mode": "percentage", "color": "#ffffff"}, | ||
| "trackingTrails": {"enabled": True, "maxLength": 20, "trailOpacity": 0.5, "trailWidth": 2}, | ||
| "accessibility": {"highContrast": False, "reducedMotion": False, "largeText": False}, | ||
| }, | ||
| "professional": { | ||
| "name": "Professional", | ||
| "description": "Clean, muted palette for long shifts", | ||
| "boundingBoxColors": {"palette": ["#f87171", "#60a5fa", "#4ade80", "#fbbf24", "#c084fc"], "opacity": 0.6, "borderWidth": 3}, | ||
| "label": {"visible": True, "fontSize": 11, "fontFamily": "sans-serif", "backgroundColor": "rgba(30,30,30,0.7)", "textColor": "#e0e0e0"}, | ||
| "confidence": {"visible": True, "mode": "percentage", "color": "#d1d5db"}, | ||
| "trackingTrails": {"enabled": True, "maxLength": 15, "trailOpacity": 0.4, "trailWidth": 2}, | ||
| "accessibility": {"highContrast": False, "reducedMotion": False, "largeText": False}, | ||
| }, | ||
| "highContrast": { | ||
| "name": "High Contrast", | ||
| "description": "Maximum visibility for accessibility", | ||
| "boundingBoxColors": {"palette": ["#ff0000", "#00ff00", "#0000ff", "#ffff00", "#ff00ff"], "opacity": 1.0, "borderWidth": 5}, | ||
| "label": {"visible": True, "fontSize": 14, "fontFamily": "sans-serif", "backgroundColor": "rgba(0,0,0,0.9)", "textColor": "#ffffff", "fontWeight": "bold"}, | ||
| "confidence": {"visible": True, "mode": "bar", "color": "#00ff00"}, | ||
| "trackingTrails": {"enabled": True, "maxLength": 25, "trailOpacity": 0.8, "trailWidth": 3}, | ||
| "accessibility": {"highContrast": True, "reducedMotion": False, "largeText": True}, | ||
| }, | ||
| "minimal": { | ||
| "name": "Minimal", | ||
| "description": "Subtle overlay with reduced visual clutter", | ||
| "boundingBoxColors": {"palette": ["#dc2626", "#2563eb", "#16a34a", "#d97706", "#9333ea"], "opacity": 0.4, "borderWidth": 2}, | ||
| "label": {"visible": True, "fontSize": 10, "fontFamily": "monospace", "backgroundColor": "transparent", "textColor": "#ffffff"}, | ||
| "confidence": {"visible": False, "mode": "off", "color": "#ffffff"}, | ||
| "trackingTrails": {"enabled": False, "maxLength": 10, "trailOpacity": 0.3, "trailWidth": 1}, | ||
| "accessibility": {"highContrast": False, "reducedMotion": True, "largeText": False}, | ||
| }, | ||
| "nightMode": { | ||
| "name": "Night Mode", | ||
| "description": "Dimmed palette for dark environments", | ||
| "boundingBoxColors": {"palette": ["#ff6b6b", "#74b9ff", "#55efc4", "#ffeaa7", "#a29bfe"], "opacity": 0.5, "borderWidth": 3}, | ||
| "label": {"visible": True, "fontSize": 12, "fontFamily": "monospace", "backgroundColor": "rgba(10,10,20,0.8)", "textColor": "#a0a0b0"}, | ||
| "confidence": {"visible": True, "mode": "percentage", "color": "#a0a0b0"}, | ||
| "trackingTrails": {"enabled": True, "maxLength": 15, "trailOpacity": 0.3, "trailWidth": 2}, | ||
| "accessibility": {"highContrast": False, "reducedMotion": False, "largeText": False}, | ||
| }, | ||
| "colorBlind": { | ||
| "name": "Color Blind Friendly", | ||
| "description": "Pattern-based cues for color vision deficiency", | ||
| "boundingBoxColors": {"palette": ["#e69f00", "#56b4e9", "#009e73", "#f0e442", "#0072b2"], "opacity": 0.85, "borderWidth": 4}, | ||
| "label": {"visible": True, "fontSize": 13, "fontFamily": "sans-serif", "backgroundColor": "rgba(0,0,0,0.7)", "textColor": "#ffffff"}, | ||
| "confidence": {"visible": True, "mode": "bar", "color": "#56b4e9"}, | ||
| "trackingTrails": {"enabled": True, "maxLength": 20, "trailOpacity": 0.6, "trailWidth": 3}, | ||
| "accessibility": {"highContrast": True, "reducedMotion": False, "largeText": True}, | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| class ThemeRequest(BaseModel): | ||
| name: str = Field(..., min_length=1, max_length=100) | ||
| boundingBoxColors: dict | ||
| label: dict | ||
| confidence: dict | ||
| trackingTrails: dict | ||
| accessibility: dict | ||
|
|
||
|
|
||
| class ThemeResponse(BaseModel): | ||
| name: str | ||
| boundingBoxColors: dict | ||
| label: dict | ||
| confidence: dict | ||
| trackingTrails: dict | ||
| accessibility: dict | ||
| isCustom: bool = False | ||
|
|
||
|
|
||
| def _load_custom_themes() -> dict: | ||
| if not os.path.exists(THEMES_FILE): | ||
| return {} | ||
| try: | ||
| with open(THEMES_FILE, "r", encoding="utf-8") as f: | ||
| return json.load(f) | ||
| except Exception: | ||
| return {} | ||
|
|
||
|
|
||
| def _save_custom_themes(themes: dict): | ||
| os.makedirs(os.path.dirname(THEMES_FILE), exist_ok=True) | ||
| with open(THEMES_FILE, "w", encoding="utf-8") as f: | ||
| json.dump(themes, f, indent=2) | ||
|
|
||
|
|
||
| @router.get("", response_model=dict[str, ThemeResponse]) | ||
| def list_themes(): | ||
| customs = _load_custom_themes() | ||
| result = {} | ||
| for k, v in PRESETS.items(): | ||
| result[k] = ThemeResponse(name=v.get("name", k), boundingBoxColors=v["boundingBoxColors"], label=v["label"], confidence=v["confidence"], trackingTrails=v["trackingTrails"], accessibility=v["accessibility"], isCustom=False) | ||
| for k, v in customs.items(): | ||
| result[k] = ThemeResponse(name=v.get("name", k), boundingBoxColors=v["boundingBoxColors"], label=v["label"], confidence=v["confidence"], trackingTrails=v["trackingTrails"], accessibility=v["accessibility"], isCustom=True) | ||
| return result | ||
|
|
||
|
|
||
| @router.get("/{theme_name}", response_model=ThemeResponse) | ||
| def get_theme(theme_name: str): | ||
| all_themes = {**PRESETS, **_load_custom_themes()} | ||
| if theme_name not in all_themes: | ||
| raise HTTPException(status_code=404, detail=f"Theme {theme_name!r} not found") | ||
| t = all_themes[theme_name] | ||
| is_custom = theme_name in _load_custom_themes() | ||
| return ThemeResponse(name=t.get("name", theme_name), boundingBoxColors=t["boundingBoxColors"], label=t["label"], confidence=t["confidence"], trackingTrails=t["trackingTrails"], accessibility=t["accessibility"], isCustom=is_custom) | ||
|
|
||
|
|
||
| @router.post("", response_model=ThemeResponse, status_code=201) | ||
| def save_theme(body: ThemeRequest): | ||
| customs = _load_custom_themes() | ||
| customs[body.name] = { | ||
| "name": body.name, | ||
| "boundingBoxColors": body.boundingBoxColors, | ||
| "label": body.label, | ||
| "confidence": body.confidence, | ||
| "trackingTrails": body.trackingTrails, | ||
| "accessibility": body.accessibility, | ||
| } | ||
| _save_custom_themes(customs) | ||
| logger.info("Custom theme saved name=%s", body.name) | ||
| return ThemeResponse(name=body.name, boundingBoxColors=body.boundingBoxColors, label=body.label, confidence=body.confidence, trackingTrails=body.trackingTrails, accessibility=body.accessibility, isCustom=True) | ||
|
|
||
|
|
||
| @router.delete("/{theme_name}") | ||
| def delete_theme(theme_name: str): | ||
| customs = _load_custom_themes() | ||
| if theme_name not in customs: | ||
| raise HTTPException(status_code=404, detail=f"Custom theme {theme_name!r} not found") | ||
| del customs[theme_name] | ||
| _save_custom_themes(customs) | ||
| logger.info("Custom theme deleted name=%s", theme_name) | ||
| return {"deleted": theme_name} | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🔴 Critical | 🏗️ Heavy lift
Race condition and data loss hazard during concurrent file operations.
The endpoints for reading and writing custom themes are standard synchronous
deffunctions, which FastAPI runs in a threadpool. Concurrent requests toPOST /themesorDELETE /themeswill cause race conditions because the file reads and writes are not synchronized.Furthermore, if a read occurs while a write is truncating the file,
json.load(f)will raise an exception. Because_load_custom_themescatches all exceptions and silently returns{}, the subsequent save operation will overwrite the file with just the single new or remaining theme, permanently wiping out all other custom themes.Please use a
threading.Lockto synchronize file access and remove the blanketexcept Exceptionclause so that file corruption fails loudly rather than causing silent data loss.🔒️ Proposed fix to synchronize file operations
🧰 Tools
🪛 ast-grep (0.44.1)
[warning] 107-107: File path is request-/variable-derived; validate and normalize to prevent path traversal.
Context: open(THEMES_FILE, "r", encoding="utf-8")
Note: [CWE-22] Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal').
(open-filename-from-request)
[warning] 115-115: File path is request-/variable-derived; validate and normalize to prevent path traversal.
Context: open(THEMES_FILE, "w", encoding="utf-8")
Note: [CWE-22] Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal').
(open-filename-from-request)
🤖 Prompt for AI Agents