Skip to content

Instantly share code, notes, and snippets.

@mrprobert
Created January 11, 2026 13:44
Show Gist options
  • Select an option

  • Save mrprobert/77b655709bd044ae7cd5c20a23af8638 to your computer and use it in GitHub Desktop.

Select an option

Save mrprobert/77b655709bd044ae7cd5c20a23af8638 to your computer and use it in GitHub Desktop.
Feature Request: Session ID for Sigil Plugin Cache Invalidation

Session ID for Plugin Cache Invalidation

Problem

Plugins have no way to detect if Sigil has restarted between invocations. If it did, then plugins could create a persistent cache that would be available for successive plugin invocations in the same Sigil invocation.

Proposed Solution

Add a unique session identifier that Sigil generates once at startup and makes available to plugins.

Format: {timestamp_milliseconds}_{process_id}

Example: 1703261234567_3892

Implementation could be through one or both of these options:

Option 1 (sigil.cfg): Sigil writes the session ID to a file that the plugin reads Option 2 (BookContainer API): Sigil passes the session ID through the Python API directly

How Each Works Technically

Option 1: Via sigil.cfg

  1. Sigil generates session ID at startup: 1703261234567_3892

  2. For each plugin invocation, Sigil writes sigil.cfg:

OEBPS/content.opf
SIGIL_SESSION_ID=1703261234567_3892

Option 2: Via BookContainer API

  1. Sigil generates session ID at startup: 1703261234567_3892

  2. Sigil's plugin wrapper code adds a method to BookContainer:

class BookContainer:
	def get_sigil_session_id(self):
		return self._w.sigil_session_id # Passed from Sigil
  1. Plugin calls it directly:
def get_session_id(bk):
	return bk.get_sigil_session_id()

Implementation

Option 1:

1. In the class header (MainWindow.h):

class MainWindow : public QMainWindow {
    // ... other members ...
    
private:
    QString m_sessionId;  // Session ID for this Sigil instance
};

2. Main Window Initialization (MainWindow.cpp)

MainWindow::MainWindow() {
    // ... other initialization ...
    
    // Generate session ID once at startup
    m_sessionId = QString::number(QDateTime::currentMSecsSinceEpoch()) + 
                  "_" + QString::number(QCoreApplication::applicationPid());
}

3. Plugin Launcher (PluginDB.cpp)

// Add session ID line when writing sigil.cfg
// (Access m_sessionId via your existing MainWindow access pattern)
config_file << "OEBPS/content.opf\n";
config_file << "SIGIL_SESSION_ID=" << m_sessionId << "\n";

Option 2:

1. In the class header (MainWindow.h):

class MainWindow : public QMainWindow {
    // ... other members ...
    
private:
    QString m_sessionId;  // Session ID for this Sigil instance
};

2. Main Window Initialization (MainWindow.cpp)

MainWindow::MainWindow() {
    // ... other initialization ...
    
    // Generate session ID once at startup
    m_sessionId = QString::number(QDateTime::currentMSecsSinceEpoch()) + 
                  "_" + QString::number(QCoreApplication::applicationPid());
}

3. Plugin Launcher (PluginDB.cpp)

// Pass session ID via environment variable
// (Access m_sessionId via your existing MainWindow access pattern)
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("SIGIL_SESSION_ID", m_sessionId);
pluginProcess.setProcessEnvironment(env);

4. Python Wrapper (wrapper.py)

class Wrapper:
    def __init__(self):
        self.plugin_dir = ...
        self.plugin_name = ...
        self.sigil_session_id = os.environ.get('SIGIL_SESSION_ID', '')  # ADD THIS
        # etc.

class BookContainer:
    def __init__(self, wrapper):
        self._w = wrapper
    
    def readfile(self, id):
        # existing method
        ...
    
    def writefile(self, id, data):
        # existing method
        ...
    
    def get_sigil_session_id(self):  # ADD THIS METHOD
        """Returns unique session ID for this Sigil instance."""
        return self._w.sigil_session_id

Plugin Usage Example

def get_session_id(bk):
    # Option 2: Via API (if available)
    if hasattr(bk, 'get_sigil_session_id'):
        return bk.get_sigil_session_id()
    
    # Option 1: Via sigil.cfg
    cfg_path = os.path.join(os.getcwd(), 'sigil.cfg')
    with open(cfg_path) as f:
        for line in f:
            if line.startswith('SIGIL_SESSION_ID='):
                return line.split('=')[1].strip()
    return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment