CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-py-asterisk

Asterisk Manager API Python interface for programmatically controlling and monitoring Asterisk PBX systems

Pending
Overview
Eval results
Files

configuration.mddocs/

Configuration Management

Configuration file handling for py-Asterisk connection settings. The Config class provides structured access to configuration files with support for multiple search locations and environment variables.

Capabilities

Configuration Classes

Config

class Config:
    """
    Configuration file reader and manager for py-asterisk settings.
    Supports multiple configuration file locations and environment variables.
    """
    
    def __init__(config_pathname: str = None):
        """
        Initialize configuration reader.
        
        Args:
            config_pathname: Specific config file path, or None to search default locations
        """
    
    def refresh():
        """Reload configuration from filesystem."""
    
    def get_connection(connection: str = None) -> tuple:
        """
        Get connection parameters from configuration.
        
        Args:
            connection: Connection name in config file, or None for default
            
        Returns:
            Tuple of (address, username, secret) where address is (host, port)
        """

Configuration Constants

CONFIG_FILENAME: str = 'py-asterisk.conf'
"""Default configuration filename."""

CONFIG_PATHNAMES: list = [
    os.environ.get('PYASTERISK_CONF', ''),
    os.path.join(os.environ.get('HOME', ''), '.py-asterisk.conf'),
    os.path.join(os.environ.get('USERPROFILE', ''), 'py-asterisk.conf'),
    'py-asterisk.conf',
    '/etc/py-asterisk.conf',
    '/etc/asterisk/py-asterisk.conf',
]
"""List of paths searched for configuration file in order."""

Configuration Exceptions

class ConfigurationError(BaseException):
    """Raised when there is a problem with the configuration."""

Configuration File Format

py-Asterisk uses standard INI-style configuration files compatible with Python's configparser module:

[manager]
host = 127.0.0.1
port = 5038
username = admin
secret = password123
timeout = 30.0

[logging]
level = INFO
format = %(asctime)s - %(name)s - %(levelname)s - %(message)s

Usage Examples

Basic Configuration Usage

from Asterisk.Config import Config

# Load config from default search paths
config = Config()

# Load config from specific file
config = Config('/path/to/my-asterisk.conf')

Environment Variable Override

import os

# Set environment variable to override config location
os.environ['PYASTERISK_CONF'] = '/opt/asterisk/custom.conf'

# Config will now use the environment variable path first
config = Config()

Configuration Search Order

The Config class searches for configuration files in this order:

  1. PYASTERISK_CONF environment variable path
  2. ~/.py-asterisk.conf (user home directory)
  3. %USERPROFILE%/py-asterisk.conf (Windows user profile)
  4. py-asterisk.conf (current directory)
  5. /etc/py-asterisk.conf (system-wide)
  6. /etc/asterisk/py-asterisk.conf (Asterisk config directory)

Error Handling

from Asterisk.Config import Config, ConfigurationError

try:
    config = Config('/nonexistent/config.conf')
except ConfigurationError as e:
    print(f"Configuration error: {e}")
    # Fall back to default configuration or exit

Integration with Manager

While the Config class provides configuration file support, many applications pass connection parameters directly to the Manager:

from Asterisk.Manager import Manager
from Asterisk.Config import Config

# Option 1: Direct connection parameters
manager = Manager(('127.0.0.1', 5038), 'admin', 'secret')

# Option 2: Using configuration file
config = Config()
address, username, secret = config.get_connection()
manager = Manager(address, username, secret)

Install with Tessl CLI

npx tessl i tessl/pypi-py-asterisk

docs

channel-management.md

cli-utilities.md

configuration.md

exceptions.md

index.md

manager-api.md

tile.json