|
| 1 | +"""SpectraVR plugin core — REST endpoints, SQLite state, streaming.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +import sqlite3 |
| 6 | +import logging |
| 7 | + |
| 8 | +log = logging.getLogger(__name__) |
| 9 | + |
| 10 | +# VR projection suffix map (projection key → filename suffix, no extension) |
| 11 | +VR_TAGS = { |
| 12 | + "vr180_lr": "_180_lr", |
| 13 | + "vr180_tb": "_180_ou", |
| 14 | + "sphere_360_mono": "_360", |
| 15 | + "sphere_360_3d": "_360_3d", |
| 16 | + "fisheye": "_fisheye_180", |
| 17 | +} |
| 18 | + |
| 19 | +ALL_VR_SUFFIXES = list(VR_TAGS.values()) |
| 20 | + |
| 21 | + |
| 22 | +def strip_vr_tag(stem: str) -> str: |
| 23 | + """Remove any existing VR suffix from a filename stem (no extension).""" |
| 24 | + for suffix in ALL_VR_SUFFIXES: |
| 25 | + if stem.endswith(suffix): |
| 26 | + return stem[: -len(suffix)] |
| 27 | + return stem |
| 28 | + |
| 29 | + |
| 30 | +def apply_vr_tag_to_name(filename: str, projection: str | None) -> str: |
| 31 | + """Return new filename with VR tag applied (or removed if projection is None).""" |
| 32 | + if "." in filename: |
| 33 | + stem, ext = filename.rsplit(".", 1) |
| 34 | + ext = "." + ext |
| 35 | + else: |
| 36 | + stem, ext = filename, "" |
| 37 | + clean = strip_vr_tag(stem) |
| 38 | + if projection is None: |
| 39 | + return clean + ext |
| 40 | + suffix = VR_TAGS.get(projection, "") |
| 41 | + return clean + suffix + ext |
| 42 | + |
| 43 | + |
| 44 | +def db_connect(db_path: str) -> sqlite3.Connection: |
| 45 | + """Open (or create) the plugin SQLite database.""" |
| 46 | + conn = sqlite3.connect(db_path) |
| 47 | + conn.execute(""" |
| 48 | + CREATE TABLE IF NOT EXISTS file_metadata ( |
| 49 | + path TEXT PRIMARY KEY, |
| 50 | + favorited INTEGER DEFAULT 0, |
| 51 | + hidden INTEGER DEFAULT 0 |
| 52 | + ) |
| 53 | + """) |
| 54 | + conn.commit() |
| 55 | + return conn |
| 56 | + |
| 57 | + |
| 58 | +def db_toggle(conn: sqlite3.Connection, path: str, column: str) -> None: |
| 59 | + """Toggle a boolean column in file_metadata for the given path key.""" |
| 60 | + assert column in ("favorited", "hidden") |
| 61 | + conn.execute( |
| 62 | + f"INSERT INTO file_metadata (path, {column}) VALUES (?, 1) " |
| 63 | + f"ON CONFLICT(path) DO UPDATE SET {column} = 1 - {column}", |
| 64 | + (path,), |
| 65 | + ) |
| 66 | + conn.commit() |
| 67 | + |
| 68 | + |
| 69 | +def db_get(conn: sqlite3.Connection, path: str) -> dict: |
| 70 | + """Return {favorited, hidden} for path; defaults 0 if not present.""" |
| 71 | + row = conn.execute( |
| 72 | + "SELECT favorited, hidden FROM file_metadata WHERE path = ?", (path,) |
| 73 | + ).fetchone() |
| 74 | + return {"favorited": bool(row[0]), "hidden": bool(row[1])} if row else {"favorited": False, "hidden": False} |
| 75 | + |
| 76 | + |
| 77 | +class Core: |
| 78 | + """Deluge core plugin class — registered by setup.py entry point.""" |
| 79 | + |
| 80 | + def __init__(self, plugin_api, *args, **kwargs): |
| 81 | + self.plugin_api = plugin_api |
| 82 | + self.resource = None |
| 83 | + self.db = None |
| 84 | + |
| 85 | + def enable(self): |
| 86 | + """Called when plugin is enabled in Deluge preferences.""" |
| 87 | + import deluge.configmanager as cm |
| 88 | + config_dir = cm.get_config_dir() |
| 89 | + db_path = os.path.join(config_dir, "spectravr.db") |
| 90 | + conf_path = os.path.join(config_dir, "spectravr.conf") |
| 91 | + self.token = self._load_token(conf_path) |
| 92 | + self.db = db_connect(db_path) |
| 93 | + |
| 94 | + # Register REST resource on Deluge's Twisted web server. |
| 95 | + try: |
| 96 | + import deluge.component as component |
| 97 | + from .resource import SpectravRResource |
| 98 | + self.resource = SpectravRResource(self) |
| 99 | + component.get("DelugeWeb").top_level.putChild(b"spectravr", self.resource) |
| 100 | + log.info("SpectravR plugin enabled, resource registered at /spectravr/") |
| 101 | + except Exception as e: |
| 102 | + log.error("Failed to register SpectravR resource: %s", e) |
| 103 | + |
| 104 | + def disable(self): |
| 105 | + """Called when plugin is disabled.""" |
| 106 | + try: |
| 107 | + import deluge.component as component |
| 108 | + top = component.get("DelugeWeb").top_level |
| 109 | + if b"spectravr" in top.children: |
| 110 | + del top.children[b"spectravr"] |
| 111 | + except Exception as e: |
| 112 | + log.warning("Error deregistering SpectravR resource: %s", e) |
| 113 | + if self.db: |
| 114 | + self.db.close() |
| 115 | + self.db = None |
| 116 | + |
| 117 | + def update(self): |
| 118 | + pass |
| 119 | + |
| 120 | + @staticmethod |
| 121 | + def _load_token(conf_path: str) -> str: |
| 122 | + """Read token from spectravr.conf; return empty string if missing.""" |
| 123 | + if not os.path.exists(conf_path): |
| 124 | + return "" |
| 125 | + import configparser |
| 126 | + cfg = configparser.ConfigParser() |
| 127 | + cfg.read(conf_path) |
| 128 | + return cfg.get("spectravr", "token", fallback="") |
0 commit comments