Provides a centralized place to register and access settings schemas. Supports both individual schemas and SchemaGroup objects for organizing related configurations.
source
SettingsRegistry
SettingsRegistry ()
Registry for managing settings schemas and schema groups.
# Example: Create and use a registry
from cjm_fasthtml_settings.core.config import get_app_config_schema
registry = SettingsRegistry()
# Register the app config schema
app_schema = get_app_config_schema(app_title= "My App" )
registry.register(app_schema)
# Register a custom schema
custom_schema = {
"name" : "custom" ,
"title" : "Custom Settings" ,
"type" : "object" ,
"properties" : {
"api_key" : {
"type" : "string" ,
"title" : "API Key" ,
"default" : ""
}
}
}
registry.register(custom_schema)
print (f"Registered schemas: { registry. list_schemas()} " )
print (f" \n General schema title: { registry. get('general' )['title' ]} " )
print (f"Custom schema title: { registry. get('custom' )['title' ]} " )
Registered schemas: ['general', 'custom']
General schema title: Application Configuration
Custom schema title: Custom Settings
# Example: Register and use SchemaGroups
from cjm_fasthtml_settings.core.schema_group import SchemaGroup
registry2 = SettingsRegistry()
# Register a simple schema
registry2.register({
"name" : "general" ,
"title" : "General Settings" ,
"type" : "object" ,
"properties" : {"app_name" : {"type" : "string" , "default" : "My App" }}
})
# Register a schema group
media_group = SchemaGroup(
name= "media" ,
title= "Media Settings" ,
schemas= {
"scanner" : {
"name" : "scanner" ,
"title" : "Scanner Settings" ,
"type" : "object" ,
"properties" : {"scan_path" : {"type" : "string" , "default" : "/media" }}
},
"player" : {
"name" : "player" ,
"title" : "Player Settings" ,
"type" : "object" ,
"properties" : {"volume" : {"type" : "integer" , "default" : 50 }}
}
}
)
registry2.register(media_group)
print (f"Registered items: { registry2. list_schemas()} " )
print (f" \n Direct schema lookup:" )
schema, err = registry2.resolve_schema("general" )
print (f" 'general' -> { schema['title' ] if schema else err} " )
print (f" \n Grouped schema lookup:" )
schema, err = registry2.resolve_schema("media_scanner" )
print (f" 'media_scanner' -> { schema['title' ] if schema else err} " )
print (f" unique_id: { schema. get('unique_id' ) if schema else 'N/A' } " )
schema, err = registry2.resolve_schema("media_player" )
print (f" 'media_player' -> { schema['title' ] if schema else err} " )
Registered items: ['general', 'media']
Direct schema lookup:
'general' -> General Settings
Grouped schema lookup:
'media_scanner' -> Scanner Settings
unique_id: media_scanner
'media_player' -> Player Settings
# Example: Using the module-level registry
from cjm_fasthtml_settings.core.schemas import registry as settings_registry
from cjm_fasthtml_settings.core.config import get_app_config_schema
# Clear for demo (normally you wouldn't do this)
settings_registry._schemas = {}
# Register schemas
settings_registry.register(get_app_config_schema(app_title= "My App" , include_theme= False ))
settings_registry.register({
"name" : "custom" ,
"title" : "Custom Settings" ,
"type" : "object" ,
"properties" : {"api_key" : {"type" : "string" , "title" : "API Key" , "default" : "" }}
})
print (f"Registered schemas: { settings_registry. list_schemas()} " )
print (f"General schema: { settings_registry. get('general' )['title' ]} " )
Registered schemas: ['general', 'custom']
General schema: Application Configuration
Module-Level Registry Instance