/usr/share/pyshared/asterisk/config.py is in python-pyst 0.6.50-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | #!/usr/bin/env python
# vim: set expandtab:
"""
Parse Asterisk configuration files.
This module provides parsing functionality for asterisk config files.
import asterisk.config
import sys
# load and parse the config file
try:
config = asterisk.config.Config('/etc/asterisk/extensions.conf')
except asterisk.config.ParseError, (line, reason):
print "Parse Error line: %s: %s" % (line, reason)
sys.exit(1)
except IOError, reason:
print "Error opening file: %s" % reason
sys.exit(1)
# print our parsed output
for category in config.categories:
print '[%s]' % category.name # print the current category
for item in category.items:
print ' %s = %s' % (item.name, item.value)
"""
import sys
class ParseError(Exception): pass
class Line(object):
def __init__(self, line, number):
self.line = ''
self.comment = ''
line = line.strip() # I guess we don't preserve indentation
self.number = number
parts = line.split(';')
if len(parts) >= 2:
self.line = parts[0].strip()
self.comment = ';'.join(parts[1:]) #Just in case the comment contained ';'
else:
self.line = line
def __str__(self):
return self.get_line()
def get_line(self):
if self.comment and self.line:
return '%s\t;%s' % (self.line, self.comment)
elif self.comment and not self.line:
return ';%s' % self.comment
return self.line
class Category(Line):
def __init__(self, line='', num=-1, name=None):
Line.__init__(self, line, num)
if self.line:
if (self.line[0] != '[' or self.line[-1] != ']'):
raise ParseError(self.number, "Missing '[' or ']' in category definition")
self.name = self.line[1:-1]
elif name:
self.name = name
else:
raise Exception("Must provide name or line representing a category")
self.items = []
self.comments = []
def get_line(self):
if self.comment:
return '[%s]\t;%s' % (self.name, self.comment)
return '[%s]' % self.name
def append(self, item):
self.items.append(item)
def insert(self, index, item):
self.items.insert(index, item)
def pop(self, index=-1):
self.items.pop(index)
def remove(self, item):
self.items.remove(item)
class Item(Line):
def __init__(self, line='', num=-1, name=None, value=None):
Line.__init__(self, line, num)
self.style = ''
if self.line:
self.parse()
elif (name and value):
self.name = name
self.value = value
else:
raise Exception("Must provide name or value representing an item")
def parse(self):
try:
name, value = self.line.split('=', 1)
except ValueError:
if self.line.strip()[-1] == ']':
raise ParseError(self.number, "Category name missing '['")
else:
raise ParseError(self.number, "Item must be in name = value pairs")
if value and value[0] == '>':
self.style = '>' #preserve the style of the original
value = value[1:].strip()
self.name = name.strip()
self.value = value
def get_line(self):
if self.comment:
return '%s =%s %s\t;%s' % (self.name, self.style, self.value, self.comment)
return '%s =%s %s' % (self.name, self.style, self.value)
class Config(object):
def __init__(self, filename):
self.filename = filename
self.raw_lines = [] # Holds the raw strings
self.lines = [] # Holds things in order
self.categories = []
# load and parse the file
self.load()
self.parse()
def load(self):
self.raw_lines = open(self.filename).readlines()
#try:
#self.raw_lines = open(self.filename).readlines()
#except IOError:
#sys.stderr.write('WARNING: error opening filename: %s No data read. Starting new file?' % self.filename)
#self.raw_lines = []
def parse(self):
cat = None
num = 0
for line in self.raw_lines:
num += 1
line = line.strip()
if not line or line[0] == ';':
item = Line(line or '', num)
self.lines.append(item)
if cat: cat.comments.append(item)
continue
elif line[0] == '[':
cat = Category(line, num)
self.lines.append(cat)
self.categories.append(cat)
continue
else:
item = Item(line, num)
self.lines.append(item)
if cat: cat.append(item)
continue
|