Skip to content

[Feature] Implement Settings Management via settings.py #1074

@davidebizzocchi

Description

@davidebizzocchi

Problem Description

  1. Inflexible Class Configuration
    Currently, it's impossible to configure core classes (e.g., CheshireCat/StrayCat) with custom implementations.

  2. .env Limitations
    Complex or dynamic configurations can't be properly handled through .env files alone.

Proposed Solution

Overview

Use a settings.py file(s) to configure a setting object (alà django).
Then reference this object to retrieve variables when needed.

'settings.py' Structure:

  1. Core
    • Location: cat/settings/settings.py
    • Purpose: mountable in Docker (like plugins)
  2. In Plugins
    • Location: settings.py in plugin folder
    • Purpose: Manage plugin and Cat configurations

Implementation Details

1. Folder Structure

  • Create cat/settings/ directory containing:
    • settings.py - User-configurable file (mountable in Docker)
    • default.py - Contains default configurations

2. Settings Object

  • Load defaults in startup.py's lifespan function
  • Ensure type safety: Default values enforce type checking for user-provided values
    (Example: If default is a class, users can't set a list)

3. Variable Access Methods

Two proposed approaches:

First Solution
Direct access via settings object:

app.state.ccat = cat_settings.get(CheshireCat)(cheshire_cat_api)
# or
app.state.ccat = cat_settings.CheshireCat(cheshire_cat_api)

Second Solution
Special setting-aware classes:

app.state.ccat = CheshireCatSetting(cheshire_cat_api)
# or
app.state.ccat = CheshireCat(cheshire_cat_api)  # Imported from settings-aware module

Comparison Table

Solution Pros Cons
First - Simpler implementation - Requires codebase-wide updates for new variables
- Potentially harder to maintain
Second - Cleaner implementation
- Minimal codebase changes needed
- Folder structure
- Slightly more complex initial setup
- Folder structure

Personal recommendation: The second solution is preferable as it avoids cluttering the code with explicit settings calls.

Plugin Challenges

  1. Settings File Conflict

    • settings.py is already used for plugin initialization
    • Solution: Allow coexistence by filtering variables (e.g., ignore _prefix names)
  2. Loading Timing

    • Option A: Load with defaults in startup.py
      • Pros: Consistent initialization
      • Cons: Requires duplicate plugin validation logic
    • Option B: Load during plugin.activate()
      • Pros: Proper plugin state validation
      • Cons: Can't override pre-instantiated classes (e.g., CheshireCat)
    • I vote for Option B
  3. Deactivation Handling

    • Deactivated plugins require full restart to reset settings (not mandatory to implement)
      NOTE: plugin toggling is rare during normal Cat usage.
    • Solution: Add admin panel restart button for convenience

Additional Context

Initial implementation available here:
https://github.com/davidebizzocchi/cheschire-cat-core/tree/setting-file

Other references on this topic

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions