Installation
Install via pip:
pip install sentry_options
Or add to your pyproject.toml:
[project]
dependencies = [
"sentry_options>=0.0.14",
]
Quick start
from sentry_options import init, options
# Initialize the global options store
init()
# Get a namespace handle
opts = options('sentry-options-testing')
# Read option values
value = opts.get('example-option')
print(f"Option value: {value}")
Functions
init()
Initialize global options using fallback chain: SENTRY_OPTIONS_DIR env var, then /etc/sentry-options if it exists, otherwise sentry-options/.
Raises
InitializationError - If options are already initialized
SchemaError - If schema loading or validation fails
Example
from sentry_options import init
try:
init()
except Exception as e:
print(f"Failed to initialize: {e}")
options(namespace)
Get a namespace handle for accessing options.
Returns a handle for accessing options within the specified namespace.
Raises
RuntimeError - If init() has not been called
Example
from sentry_options import init, options
init()
testing = options('sentry-options-testing')
example_val = testing.get('example-option')
float_val = testing.get('float-option')
bool_val = testing.get('bool-option')
print(f"values: {example_val} | {float_val} | {bool_val}")
NamespaceOptions class
Handle for accessing options within a specific namespace.
get(key)
Get an option value, returning the schema default if not set.
The option key to retrieve
return
bool | int | float | str | list | None
Returns the option value. Type depends on the schema definition. Objects are not yet supported.
Raises
UnknownNamespaceError - If the namespace does not exist
UnknownOptionError - If the option key does not exist in the namespace
SchemaError - If schema validation fails
Example
from sentry_options import init, options
import time
init()
testing = options('sentry-options-testing')
while True:
time.sleep(3)
example_val = testing.get('example-option')
float_val = testing.get('float-option')
bool_val = testing.get('bool-option')
string_val = testing.get('string-option')
print(
f"values: {example_val} | {float_val} | {bool_val} | {string_val}",
flush=True,
)
isset(key)
Check if an option has a defined value.
Returns True if the option has a value, False if using the schema default.
Raises
UnknownNamespaceError - If the namespace does not exist
UnknownOptionError - If the option key does not exist in the namespace
Example
from sentry_options import init, options
init()
opts = options('sentry-options-testing')
if opts.isset('example-option'):
print(f"Option has explicit value: {opts.get('example-option')}")
else:
print("Option using default value")
Exception types
All exceptions inherit from the base OptionsError exception.
OptionsError
Base exception for sentry-options errors.
Example
from sentry_options import init, options, OptionsError
try:
init()
opts = options('my-namespace')
value = opts.get('my-option')
except OptionsError as e:
print(f"Options error: {e}")
SchemaError
Raised when schema loading or validation fails.
Inherits from OptionsError.
Example
from sentry_options import init, SchemaError
try:
init()
except SchemaError as e:
print(f"Schema validation failed: {e}")
UnknownNamespaceError
Raised when accessing an unknown namespace.
Inherits from OptionsError.
Example
from sentry_options import init, options, UnknownNamespaceError
init()
opts = options('nonexistent-namespace')
try:
value = opts.get('some-option')
except UnknownNamespaceError as e:
print(f"Namespace not found: {e}")
UnknownOptionError
Raised when accessing an unknown option.
Inherits from OptionsError.
Example
from sentry_options import init, options, UnknownOptionError
init()
opts = options('sentry-options-testing')
try:
value = opts.get('nonexistent-option')
except UnknownOptionError as e:
print(f"Option not found: {e}")
InitializationError
Raised when options are already initialized.
Inherits from OptionsError.
Example
from sentry_options import init, InitializationError
init() # First call succeeds
try:
init() # Second call raises error
except InitializationError as e:
print(f"Already initialized: {e}")
Complete example
"""An example usage of the Python options client library.
Every 3 seconds, prints out the value of example-option, float-option,
and bool-option.
Updating values in `../values` will be reflected in stdout.
Ctrl+C to exit.
"""
from __future__ import annotations
import time
from sentry_options import init
from sentry_options import options
init()
testing = options('sentry-options-testing')
while True:
time.sleep(3)
example_val = testing.get('example-option')
float_val = testing.get('float-option')
bool_val = testing.get('bool-option')
string_val = testing.get('string-option')
print(
f"values: {example_val} | {float_val} | {bool_val} | {string_val}",
flush=True,
)
Language comparison
from sentry_options import init, options
init()
opts = options('my-namespace')
value = opts.get('my-option')
has_value = opts.isset('my-option')
use sentry_options::{init, options};
init()?;
let opts = options("my-namespace");
let value = opts.get("my-option")?;
let has_value = opts.isset("my-option")?;