Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Functions | Variables
astconfigparser Namespace Reference

Data Structures

class  MultiOrderedConfigParser
 
class  Section
 

Functions

def find_dict (mdicts, key, val)
 
def merge_values (left, right, key)
 
def remove_comment (line, is_comment)
 
def try_include (line)
 
def try_option (line)
 
def try_section (line)
 
def write_dicts (config_file, mdicts)
 

Variables

string COMMENT = ';'
 
string COMMENT_END = '--;'
 
string COMMENT_START = ';--'
 
string DEFAULTSECT = 'general'
 

Detailed Description

Copyright (C) 2016, Digium, Inc.

This program is free software, distributed under the terms of
the GNU General Public License Version 2.

Function Documentation

def astconfigparser.find_dict (   mdicts,
  key,
  val 
)
Given a list of mult-dicts, return the multi-dict that contains
the given key/value pair.

Definition at line 289 of file astconfigparser.py.

289 def find_dict(mdicts, key, val):
290  """
291  Given a list of mult-dicts, return the multi-dict that contains
292  the given key/value pair.
293  """
294 
295  def found(d):
296  return key in d and val in d[key]
297 
298  try:
299  return [d for d in mdicts if found(d)][0]
300  except IndexError:
301  raise LookupError("Dictionary not located for key = %s, value = %s"
302  % (key, val))
303 
304 
def find_dict(mdicts, key, val)
def astconfigparser.merge_values (   left,
  right,
  key 
)
Merges values from right into left.

Definition at line 16 of file astconfigparser.py.

Referenced by Section.get_merged().

16 def merge_values(left, right, key):
17  """Merges values from right into left."""
18  if isinstance(left, list):
19  vals0 = left
20  else: # assume dictionary
21  vals0 = left[key] if key in left else []
22  vals1 = right[key] if key in right else []
23 
24  return vals0 + [i for i in vals1 if i not in vals0]
25 
def merge_values(left, right, key)
def astconfigparser.remove_comment (   line,
  is_comment 
)
Remove any commented elements from the line.

Definition at line 195 of file astconfigparser.py.

Referenced by MultiOrderedConfigParser.read().

195 def remove_comment(line, is_comment):
196  """Remove any commented elements from the line."""
197  if not line:
198  return line, is_comment
199 
200  if is_comment:
201  part = line.partition(COMMENT_END)
202  if part[1]:
203  # found multi-line comment end check string after it
204  return remove_comment(part[2], False)
205  return "", True
206 
207  part = line.partition(COMMENT_START)
208  if part[1] and not part[2].startswith('-'):
209  # found multi-line comment start check string before
210  # it to make sure there wasn't an eol comment in it
211  has_comment = part[0].partition(COMMENT)
212  if has_comment[1]:
213  # eol comment found return anything before it
214  return has_comment[0], False
215 
216  # check string after it to see if the comment ends
217  line, is_comment = remove_comment(part[2], True)
218  if is_comment:
219  # return possible string data before comment
220  return part[0].strip(), True
221 
222  # otherwise it was an embedded comment so combine
223  return ''.join([part[0].strip(), ' ', line]).rstrip(), False
224 
225  # find the first occurence of a comment that is not escaped
226  match = re.match(r'.*?([^\\];)', line)
227 
228  if match:
229  # the end of where the real string is is where the comment starts
230  line = line[0:(match.end()-1)]
231  if line.startswith(";"):
232  # if the line is actually a comment just ignore it all
233  line = ""
234 
235  return line.replace("\\", "").strip(), False
236 
def remove_comment(line, is_comment)
def astconfigparser.try_include (   line)
Checks to see if the given line is an include.  If so return the
included filename, otherwise None.

Definition at line 237 of file astconfigparser.py.

Referenced by MultiOrderedConfigParser.read().

237 def try_include(line):
238  """
239  Checks to see if the given line is an include. If so return the
240  included filename, otherwise None.
241  """
242 
243  match = re.match('^#include\s*([^;]+).*$', line)
244  if match:
245  trimmed = match.group(1).rstrip()
246  quoted = re.match('^"([^"]+)"$', trimmed)
247  if quoted:
248  return quoted.group(1)
249  bracketed = re.match('^<([^>]+)>$', trimmed)
250  if bracketed:
251  return bracketed.group(1)
252  return trimmed
253  return None
254 
255 
def try_include(line)
def astconfigparser.try_option (   line)
Parses the line as an option, returning the key/value pair.

Definition at line 280 of file astconfigparser.py.

Referenced by MultiOrderedConfigParser.read().

280 def try_option(line):
281  """Parses the line as an option, returning the key/value pair."""
282  data = re.split('=>?', line, 1)
283  # should split in two (key/val), but either way use first two elements
284  return data[0].rstrip(), data[1].lstrip()
285 
def try_option(line)
def astconfigparser.try_section (   line)
Checks to see if the given line is a section. If so return the section
name, otherwise return 'None'.

Definition at line 256 of file astconfigparser.py.

Referenced by MultiOrderedConfigParser.read().

256 def try_section(line):
257  """
258  Checks to see if the given line is a section. If so return the section
259  name, otherwise return 'None'.
260  """
261  # leading spaces were stripped when checking for comments
262  if not line.startswith('['):
263  return None, False, []
264 
265  section, delim, templates = line.partition(']')
266  if not templates:
267  return section[1:], False, []
268 
269  # strip out the parens and parse into an array
270  templates = templates.replace('(', "").replace(')', "").split(',')
271  # go ahead and remove extra whitespace
272  templates = [i.strip() for i in templates]
273  try:
274  templates.remove('!')
275  return section[1:], True, templates
276  except:
277  return section[1:], False, templates
278 
279 
def try_section(line)
def astconfigparser.write_dicts (   config_file,
  mdicts 
)
Write the contents of the mdicts to the specified config file

Definition at line 305 of file astconfigparser.py.

Referenced by MultiOrderedConfigParser.write(), and Registration.write().

305 def write_dicts(config_file, mdicts):
306  """Write the contents of the mdicts to the specified config file"""
307  for section, sect_list in mdicts.iteritems():
308  # every section contains a list of dictionaries
309  for sect in sect_list:
310  config_file.write("[%s]\n" % section)
311  for key, val_list in sect.iteritems():
312  # every value is also a list
313  for v in val_list:
314  key_val = key
315  if v is not None:
316  key_val += " = " + str(v)
317  config_file.write("%s\n" % (key_val))
318  config_file.write("\n")
319 
def write_dicts(config_file, mdicts)