sip_to_pjsip: improve ability to parse input files

General improvements to SIP to PJSIP conversion utility:

1) track default section of input file to allow parsing
   an include file that doesn't specify a [section]

2) informatively handle case of assignment without [section]

3) correctly handle getting sections from included files
   - [section]'s are inherited by included file

4) provide null string as default transport bind ip

5) gracefully handle missing portions of registration string

6) denote steps of operation during conversion and confirm
   top level files as a convenience

ASTERISK-24474 #close
Review: https://reviewboard.asterisk.org/r/4280/
Reported by: John Kiniston
........

Merged revisions 430469 from http://svn.asterisk.org/svn/asterisk/branches/13


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@430470 65c4cc65-6c06-0410-ace0-fbb531ad65f3
changes/42/42/1
Scott Griepentrog 10 years ago
parent 5b30938394
commit fba836cc02

@ -1,4 +1,5 @@
import re
import itertools
from astdicts import OrderedDict
from astdicts import MultiOrderedDict
@ -331,7 +332,9 @@ class MultiOrderedConfigParser:
res = sections[key] if key in sections else []
searched.append(self)
if self._includes:
res += self._includes.get_sections(key, attr, searched)
res.extend(list(itertools.chain(*[
incl.get_sections(key, attr, searched)
for incl in self._includes.itervalues()])))
if self._parent:
res += self._parent.get_sections(key, attr, searched)
return res
@ -415,15 +418,15 @@ class MultiOrderedConfigParser:
else:
self.defaults(section)[0][key] = val
def read(self, filename):
def read(self, filename, sect=None):
"""Parse configuration information from a file"""
try:
with open(filename, 'rt') as config_file:
self._read(config_file)
self._read(config_file, sect)
except IOError:
print "Could not open file ", filename, " for reading"
def _read(self, config_file):
def _read(self, config_file, sect):
"""Parse configuration information from the config_file"""
is_comment = False # used for multi-lined comments
for line in config_file:
@ -435,7 +438,7 @@ class MultiOrderedConfigParser:
include_name = try_include(line)
if include_name:
parser = self.add_include(include_name)
parser.read(include_name)
parser.read(include_name, sect)
continue
section, is_template, templates = try_section(line)
@ -447,6 +450,8 @@ class MultiOrderedConfigParser:
continue
key, val = try_option(line)
if sect is None:
raise Exception("Section not defined before assignment")
sect[key] = val
def write(self, config_file):

@ -587,7 +587,11 @@ def create_udp(sip, pjsip, nmapped):
externhost
"""
bind = sip.multi_get('general', ['udpbindaddr', 'bindaddr'])[0]
try:
bind = sip.multi_get('general', ['udpbindaddr', 'bindaddr'])[0]
except LookupError:
bind = ''
bind = build_host(sip, bind, 'general', 'bindport')
try:
@ -974,11 +978,12 @@ class Registration:
auth_section = 'auth_reg_' + self.host
if self.secret:
if hasattr(self, 'secret') and self.secret:
set_value('password', self.secret, auth_section, pjsip, nmapped,
'auth')
set_value('username', self.authuser or self.user, auth_section,
pjsip, nmapped, 'auth')
if hasattr(self, 'authuser'):
set_value('username', self.authuser or self.user, auth_section,
pjsip, nmapped, 'auth')
set_value('outbound_auth', auth_section, section, pjsip, nmapped,
'registration')
@ -988,7 +993,7 @@ class Registration:
else:
client_uri += self.host
if self.domainport:
if hasattr(self, 'domainport') and self.domainport:
client_uri += ":" + self.domainport
elif self.port:
client_uri += ":" + self.port
@ -1136,8 +1141,9 @@ def cli_options():
"""
global PREFIX
usage = "usage: %prog [options] [input-file [output-file]]\n\n" \
"input-file defaults to 'sip.conf'\n" \
"output-file defaults to 'pjsip.conf'"
"Converts the chan_sip configuration input-file to the chan_pjsip output-file.\n"\
"The input-file defaults to 'sip.conf'.\n" \
"The output-file defaults to 'pjsip.conf'."
parser = optparse.OptionParser(usage=usage)
parser.add_option('-p', '--prefix', dest='prefix', default=PREFIX,
help='output prefix for include files')
@ -1154,6 +1160,9 @@ if __name__ == "__main__":
sip_filename, pjsip_filename = cli_options()
# configuration parser for sip.conf
sip = astconfigparser.MultiOrderedConfigParser()
print 'Reading', sip_filename
sip.read(sip_filename)
print 'Converting to PJSIP...'
pjsip, non_mappings = convert(sip, pjsip_filename, dict(), False)
print 'Writing', pjsip_filename
write_pjsip(pjsip_filename, pjsip, non_mappings)

Loading…
Cancel
Save