Skip to content

API Documentation


Module


spoc

Elastic Frameworks

SPOC is a foundational framework designed to create dynamic and adaptable frameworks. It involves defining a schema for your project(s) and building upon that schema to create a flexible and powerful Application.

This module provides an example of how to create a framework by extending the spoc.Base class. It demonstrates the initialization process using spoc.init with a list of modules, and shows how to access and manage various components and plugins within the framework.

Example

To create a custom framework, extend the spoc.Base class and initialize it using spoc.init with a list of desired modules:

from typing import Any
import spoc

MODULES = ["models", "views"]

class MyFramework(spoc.Base):
    components: Any
    plugins: Any

    def init(self):
        # __init__ Replacement
        app = spoc.init(MODULES)

        # Assign components and plugins from the initialized app
        self.components = app.components
        self.plugins = app.plugins

    @staticmethod
    def keys():
        # Define a list of keys relevant to the framework
        return ("components", "plugins")

Framework Tools


spoc.Base

A Singleton representing the entire class. Ensuring a single global point of access.


spoc.init(modules=None)

Initialize the framework by collecting installed apps and plugins.

Parameters:

  • modules (list | None, default: None ) –

    A list of modules (files) to initialize within the framework.

Example:

spoc.init(["models", "views"]) # will collect from `models.py` and `views.py`

spoc.start_project(settings_text=SETTINGS_TEXT, spoc_text=SPOC_TEXT, env_text=ENV_TEXT)

Creates the core configuration files and directories for a project.

This function sets up the initial directory structure and creates the necessary configuration files for a new project, including settings.py, spoc.toml, and environment-specific configuration files.

Parameters:

  • settings_text (str, default: SETTINGS_TEXT ) –

    The text content for the settings.py file.

  • spoc_text (str, default: SPOC_TEXT ) –

    The text content for the spoc.toml file.

  • env_text (str, default: ENV_TEXT ) –

    The text content for environment-specific .toml files (e.g., production.toml, development.toml, staging.toml).


spoc.Components(*names)

Framework Components

A tool to manage the components.

Example:

components = spoc.Components("model", "view")

add(name, metadata=None)

Add a component with the specified name and optional metadata.

Example:

components = spoc.Components()
components.add("command", {"is_click": True}) # metadata

register(name, obj, config=None)

Mark an object as a component optionally using extra configuration settings.

Example:

components = spoc.Components("command", "model")

def my_obj(): pass

components.register("command", my_obj, config={"setting": "value"})

is_component(name, obj)

Validate if the given object is a component with the specified name.

Example:

components = spoc.Components("command", "model")

def my_obj(): pass

if not components.is_component("command", my_obj):
    print("This is not a valid component.")