# disqus/gutter architecture

> Architecture diagram of the GitHub repository disqus/gutter, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/disqus/gutter
- Repository: https://github.com/disqus/gutter

## Overview

##### Project type and purpose
- This is an open-source Python library (“Gutter”) for feature‐switch management (client for “Gargoyle 2”).
- It provides APIs to define switches, configure storage and autocreation, evaluate switch activation against inputs, hook signals, decorate Django views, and ship testing utilities.

##### File structure
- Top-level files: setup.py/Gemfile/Makefile and config files (.travis.yml, .arcconfig).
- `arguments/`: Container base and built-in argument types (Value, Boolean, String).
- `operators/`: Operator implementations (comparable, identity, string, misc).
- `models.py`: definitions of Manager, Switch, Condition.
- `registry.py`: switch registration logic.
- `signals.py`: Observer hooks (switch_registered, switch_updated, etc.).
- `testutils.py`: context-manager and decorator for deterministic testing.
- `tests/`: unit and integration tests covering arguments, operators, registry, signals, testutils.

##### Review the README for architecture insights
- Core concepts: Manager ↔ StorageEngine (default MemoryDict or pluggable RedisDict), Switch with Conditions, Arguments → Variables → Operators, Namespaces, Decorators, Signals, Testing utilities.
- Extension points: custom storage backends, custom Argument containers, new Operators, signal subscribers, namespaced managers.

a. Main components
- Manager (default & namespaced)
- StorageEngine (MemoryDict, RedisDict)
- Switch (states, hierarchy, concent flag)
- Condition (argument class, attribute, operator, negative flag)
- ArgumentContainer and Variable classes
- Operator implementations
- Registry module (register/update/unregister)
- Signals (switch_registered, switch_updated, switch_unregistered, condition_apply_error)
- Decorator (switch_active)
- TestingUtilities (switches context/decorator)
- External UIs (e.g. gutter-django) and external storage services (Redis)
b. Relationships & data flows
1. Configuration: application code sets storage_engine and autocreate flags via `settings.manager`.
2. Manager uses StorageEngine to persist/retrieve Switch objects.
3. Application defines ArgumentContainer subclasses; Manager stores them in its registry.
4. To evaluate `gutter.active("feature", inputs…)`:
– Manager fetches Switch from storage
– For each Condition in Switch, it:
- Matches input object → picks appropriate ArgumentContainer
- Calls Argument method → yields Variable
- Applies Operator to Variable → Boolean result
– Applies compounded/concent logic → final decision
5. Manager returns active/inactive; may fire `condition_apply_error` if exceptions occur.
6. Manager fires `switch_registered`/`switch_updated`/`switch_unregistered` signals on mutations.
7. `@switch_active` decorator wraps Django views, invoking `gutter.active`, raising 404 or redirecting.
8. `testutils.switches` stubs Manager behavior during tests.
c. Architectural patterns & design principles
– Modular components with single responsibility (Manager vs Storage vs Switch vs Argument vs Operator).
– Strategy pattern for Operators and StorageEngines.
– Observer pattern for Signals.
– Decorator pattern for view protection.
– Extensibility: plug in new storage, arguments, operators, signal handlers.
– Separation of concerns: no business logic clutter, translation layer via Arguments.
d. Key technologies, frameworks, libraries
– Python packaging: setup.py, pytest/unittest
– durabledict’s MemoryDict, RedisDict (optional)
– Redis (via redis-py) for persistence
– Django (integration via gutter-django and decorators)

##### Capturing essential architectural elements
- Lay out the `gutter/client/` package as the central hub.
- Surround it with the external environment: application code, storage backends, UI, tests.
- Extension points so the engineer sees where to add custom storage, arguments, or operators.
- Every component that appears in the file tree (arguments, operators, models, registry, signals, testutils) is represented.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External environment
    App["App Config"]:::external
    View["Django View<br/>@switch_active"]:::external
    RedServ["Redis Service"]:::external
    TestsExt["Application Tests"]:::external

    %% Core gutter/client package
    subgraph "gutter/client"
        subgraph "arguments"
            AC["ArgumentContainer"]:::extension
            VC["Variable"]:::core
        end
        subgraph "operators"
            COMP["Comparable Operators"]:::extension
            ID["Identity Operator"]:::extension
            STR["String Operators"]:::extension
            MISC["Miscellaneous Operators"]:::extension
        end
        MOD["Models<br/>Manager, Switch, Condition"]:::core
        REG["Registry"]:::core
        SIG["Signals"]:::core
        TEST["Testing Utilities"]:::test
    end

    %% Storage engines
    DBm["MemoryDict"]:::storage
    DBr["RedisDict"]:::storage

    %% Relationships
    App -->|"configures"| MOD
    App -->|"defines switches"| REG
    REG -->|"register/update"| MOD
    MOD -->|"uses"| DBm
    MOD -->|"uses"| DBr
    RedServ -->|"provides backend"| DBr
    App -->|"defines ArgumentContainers"| AC
    MOD -->|"fetch Switch"| MOD
    MOD -->|"evaluates via"| AC
    AC -->|"yields Variable"| VC
    VC -->|"processed by"| COMP
    VC -->|"processed by"| ID
    VC -->|"processed by"| STR
    VC -->|"processed by"| MISC
    COMP -->|"produces result"| MOD
    ID -->|"produces result"| MOD
    STR -->|"produces result"| MOD
    MISC -->|"produces result"| MOD
    MOD -->|"fires signals"| SIG
    View -->|"invokes active()"| MOD
    TestsExt -->|"uses"| TEST
    TEST -->|"stubs Manager"| MOD

    %% Click events
    click AC "https://github.com/disqus/gutter/blob/master/gutter/client/arguments/base.py"
    click VC "https://github.com/disqus/gutter/blob/master/gutter/client/arguments/variables.py"
    click COMP "https://github.com/disqus/gutter/blob/master/gutter/client/operators/comparable.py"
    click ID "https://github.com/disqus/gutter/blob/master/gutter/client/operators/identity.py"
    click STR "https://github.com/disqus/gutter/blob/master/gutter/client/operators/string.py"
    click MISC "https://github.com/disqus/gutter/blob/master/gutter/client/operators/misc.py"
    click MOD "https://github.com/disqus/gutter/blob/master/gutter/client/models.py"
    click REG "https://github.com/disqus/gutter/blob/master/gutter/client/registry.py"
    click SIG "https://github.com/disqus/gutter/blob/master/gutter/client/signals.py"
    click TEST "https://github.com/disqus/gutter/blob/master/gutter/client/testutils.py"

    %% Styles
    classDef core fill:#E8F1FA,stroke:#0366d6,stroke-width:1px
    classDef extension fill:#E8F4E8,stroke:#28a745,stroke-width:1px
    classDef storage fill:#FFF4E5,stroke:#db9d28,stroke-width:1px
    classDef test fill:#FFF5F5,stroke:#d73a49,stroke-width:1px
    classDef external fill:#F0F0F0,stroke:#6a737d,stroke-width:1px
```
