Skip to content

Architecture overview

S.O.K is one Python package, sok, split into layers. Each layer only calls the layers below it.

ui PySide6 interface: window, pages, components, workers
server S.O.K Server: FastAPI application, database, login (no Qt)
│
core media manager, interfaces, adapters, exceptions, updater
│
├── apis one class per online database
├── media data models (movie, episode, album, book, game…)
├── file_operations scanning, renaming and moving files
└── config settings, API keys and sessions, service registry

Only sok.ui and sok.main import Qt. PySide6 and pypresence are in the desktop extra, so the layers below run without them, as S.O.K Server does; tests/unit/test_core_without_qt.py imports them with the desktop dependencies blocked.

Built in code with PySide6; no Qt Designer file is loaded at run time.

  • main_window.py: the window, the navigation (components/nav.py: the media types as words, “● S.O.K” for home, History and Settings icons) and the page stack. On Windows and Linux the window is frameless, with no title row: its keys (minimize, maximize, close) sit in the top right corner on the line of the navigation, whose empty parts move the window (its words and icons take the press themselves, so a click on them never starts a move). The keys are placed when the column under them takes its size, not at the window’s own resize. On Windows the window is opaque and Windows 11 rounds its corners (round_native_corners() in platform.py): see-through, it made the animations stutter. The maximize key uses the system’s own switch, at once: an animated geometry laid the window out again at each step. On Linux it is see-through and draws its own rounded corners. On macOS it keeps the native title bar, and the windows are tagged sRGB (use_srgb_windows() in platform.py, called before the application is created) so that macOS converts the theme colors for each screen instead of showing them raw on a wide-gamut one.
  • pages/: home, the batch pages (batch_page.py and its movies, TV shows, music, books and games subclasses), settings and the history of renames (history_page.py).
  • components/: reusable widgets (search results, preview, settings sections…).
  • controllers/: logic shared by the pages (source scanning, preview, window chrome, WorkerRunner).
  • workers/: background tasks (search, details, organize, folder creation, batch search and rename, detection of dropped files, undo of a rename).
  • dialogs/: the update dialog.
  • errors.py: the messages shown for API errors.
  • theme.py, platform.py, i18n.py: colors and fonts, per-system behavior, translations. The colors come from tokens.py, generated from the art direction’s tokens.json by scripts/make_theme_tokens.py.
  • message_box.py: message boxes, shown as sheets on macOS.
  • macos_menu.py: the native macOS menu bar.
  • media_manager.py: UniversalMediaManager, the single entry point to the online databases. It creates the APIs that have a key, keeps the one selected for each media type and normalizes their responses.
  • interfaces.py: MediaAPI, the contract every API implements, and the MediaType and ContentType enums.
  • adapters/media_adapters/: one adapter per kind of content (movie, TV, music, book, game) that turns a service response into the fields the app uses.
  • batch.py: the batch engine. It reads file names, searches many files at once and decides which ones are Ready (see The path of a rename). No Qt dependency.
  • name_patterns.py: the input patterns of the settings, which the file operations try before their own rules to read a file name. No Qt dependency.
  • history.py: the rename history and its undo (see Rename history). No Qt dependency.
  • exceptions.py: the exception hierarchy (see below).
  • updater.py: checks GitHub releases and installs updates.
  • desktop_entry.py: adds S.O.K to the Linux applications menu.

One class per service, all derived from BaseAPI (base_api.py), which holds the HTTP session and the error handling:

Folder Services
video/ TMDB, TVDB, OMDb (imdb_api.py)
music/ Last.fm, Spotify, Deezer, MusicBrainz
books/ Google Books, Open Library
games/ IGDB, RAWG
images/ Fanart.tv
  • sok.media: data classes for each kind of item (Movie, Episode, Album, Track, Book, Game…), all derived from BaseMedia. sok.media.factory.create_media_item builds them from a search result, for the pages and for S.O.K Server.
  • sok.file_operations: reads names (season and episode numbers, years), builds the new names from the user’s formats and moves the files. One module per media type, with shared mixins, organize.py for the batches and detection.py, which sorts the files dropped on the home page by type.
  • sok.config: config_manager.py (settings and folders), session_manager.py (keys and tokens), api_registry.py (the list of services, their media type and how they authenticate).

A FastAPI application over the same layers as the desktop app: it scans and sorts the files with detection.py, searches them with the batch engine, renames them with file_operations and records them in the rename history, kept in its SQLite database. Its web interface (web/, Svelte) is built to static files that it serves. See S.O.K Server.

  1. The user drops files on a media page, or types a search on its pick panel. The page starts a BatchSearchWorker with a WorkerRunner, which moves it to a QThread.
  2. The worker creates its own asyncio event loop and searches each item with BatchMatcher, through get_media_manager().search(query, content_type).
  3. The manager sends the query to the API selected for that media type.
  4. The API calls the service with aiohttp and returns its response.
  5. The manager passes it through the adapter of the content type.
  6. The worker emits a Qt signal; the page shows the results in the GUI thread.

Renaming follows a similar path, through the file operations instead of the APIs: see The path of a rename.

Each worker has its own event loop, and several run at once: a drop on the home page fills Movies and TV Shows, which both search TMDB in their own thread. So an API keeps one HTTP session per event loop (BaseAPI._ensure_session(), behind a lock): a session can only be used on the loop that created it, and a worker never closes the session of another one. Before closing its loop, the worker closes the sessions opened on it (close_loop_sessions() in BaseWorker.run); the sessions of loops already closed are forgotten.

UniversalMediaManager reads api_registry.py at startup. For each media type it creates every service that has its key, then selects the one in the preferred_api_<type> setting, or the first one available. set_current_api_for_media_type() changes it while the app runs.

IGDB takes a Twitch token, not its keys: get_igdb_token() (sok.config.session_manager) asks Twitch for one with igdb_client_id and igdb_client_secret (client credentials), keeps it in memory until an hour before it expires (about 60 days), and IGDBApi asks for a new one and tries again once when IGDB refuses it. Nothing is saved: the server and the desktop app each ask for their own.

All exceptions derive from SOKBaseError (sok.core.exceptions):

SOKBaseError
├── APIError
│ ├── APIAuthenticationError
│ ├── APIConnectionError
│ ├── APINotFoundError
│ ├── APIRateLimitError
│ ├── APIResponseError
│ └── APITimeoutError
├── ConfigurationError
│ └── MissingConfigurationError
├── FileOperationError
│ ├── FileNotFoundError
│ └── FilePermissionError
└── MediaError
├── MediaNotFoundError
└── UnsupportedMediaTypeError

sok.core.exceptions.FileNotFoundError is not the built-in one: import it explicitly.

Built with Kiln