0
# Configuration Management
1
2
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.
3
4
## Capabilities
5
6
### Configuration Classes
7
8
#### Config
9
10
```python { .api }
11
class Config:
12
"""
13
Configuration file reader and manager for py-asterisk settings.
14
Supports multiple configuration file locations and environment variables.
15
"""
16
17
def __init__(config_pathname: str = None):
18
"""
19
Initialize configuration reader.
20
21
Args:
22
config_pathname: Specific config file path, or None to search default locations
23
"""
24
25
def refresh():
26
"""Reload configuration from filesystem."""
27
28
def get_connection(connection: str = None) -> tuple:
29
"""
30
Get connection parameters from configuration.
31
32
Args:
33
connection: Connection name in config file, or None for default
34
35
Returns:
36
Tuple of (address, username, secret) where address is (host, port)
37
"""
38
```
39
40
### Configuration Constants
41
42
```python { .api }
43
CONFIG_FILENAME: str = 'py-asterisk.conf'
44
"""Default configuration filename."""
45
46
CONFIG_PATHNAMES: list = [
47
os.environ.get('PYASTERISK_CONF', ''),
48
os.path.join(os.environ.get('HOME', ''), '.py-asterisk.conf'),
49
os.path.join(os.environ.get('USERPROFILE', ''), 'py-asterisk.conf'),
50
'py-asterisk.conf',
51
'/etc/py-asterisk.conf',
52
'/etc/asterisk/py-asterisk.conf',
53
]
54
"""List of paths searched for configuration file in order."""
55
```
56
57
### Configuration Exceptions
58
59
```python { .api }
60
class ConfigurationError(BaseException):
61
"""Raised when there is a problem with the configuration."""
62
```
63
64
## Configuration File Format
65
66
py-Asterisk uses standard INI-style configuration files compatible with Python's configparser module:
67
68
```ini
69
[manager]
70
host = 127.0.0.1
71
port = 5038
72
username = admin
73
secret = password123
74
timeout = 30.0
75
76
[logging]
77
level = INFO
78
format = %(asctime)s - %(name)s - %(levelname)s - %(message)s
79
```
80
81
## Usage Examples
82
83
### Basic Configuration Usage
84
85
```python
86
from Asterisk.Config import Config
87
88
# Load config from default search paths
89
config = Config()
90
91
# Load config from specific file
92
config = Config('/path/to/my-asterisk.conf')
93
```
94
95
### Environment Variable Override
96
97
```python
98
import os
99
100
# Set environment variable to override config location
101
os.environ['PYASTERISK_CONF'] = '/opt/asterisk/custom.conf'
102
103
# Config will now use the environment variable path first
104
config = Config()
105
```
106
107
### Configuration Search Order
108
109
The Config class searches for configuration files in this order:
110
111
1. `PYASTERISK_CONF` environment variable path
112
2. `~/.py-asterisk.conf` (user home directory)
113
3. `%USERPROFILE%/py-asterisk.conf` (Windows user profile)
114
4. `py-asterisk.conf` (current directory)
115
5. `/etc/py-asterisk.conf` (system-wide)
116
6. `/etc/asterisk/py-asterisk.conf` (Asterisk config directory)
117
118
### Error Handling
119
120
```python
121
from Asterisk.Config import Config, ConfigurationError
122
123
try:
124
config = Config('/nonexistent/config.conf')
125
except ConfigurationError as e:
126
print(f"Configuration error: {e}")
127
# Fall back to default configuration or exit
128
```
129
130
### Integration with Manager
131
132
While the Config class provides configuration file support, many applications pass connection parameters directly to the Manager:
133
134
```python
135
from Asterisk.Manager import Manager
136
from Asterisk.Config import Config
137
138
# Option 1: Direct connection parameters
139
manager = Manager(('127.0.0.1', 5038), 'admin', 'secret')
140
141
# Option 2: Using configuration file
142
config = Config()
143
address, username, secret = config.get_connection()
144
manager = Manager(address, username, secret)
145
```