[asterisk-commits] sgriepentrog: branch 13 r430469 - /branches/13/contrib/scripts/sip_to_pjsip/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Jan 9 16:08:43 CST 2015


Author: sgriepentrog
Date: Fri Jan  9 16:08:40 2015
New Revision: 430469

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=430469
Log:
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


Modified:
    branches/13/contrib/scripts/sip_to_pjsip/astconfigparser.py
    branches/13/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py

Modified: branches/13/contrib/scripts/sip_to_pjsip/astconfigparser.py
URL: http://svnview.digium.com/svn/asterisk/branches/13/contrib/scripts/sip_to_pjsip/astconfigparser.py?view=diff&rev=430469&r1=430468&r2=430469
==============================================================================
--- branches/13/contrib/scripts/sip_to_pjsip/astconfigparser.py (original)
+++ branches/13/contrib/scripts/sip_to_pjsip/astconfigparser.py Fri Jan  9 16:08:40 2015
@@ -1,4 +1,5 @@
 import re
+import itertools
 
 from astdicts import OrderedDict
 from astdicts import MultiOrderedDict
@@ -331,7 +332,9 @@
         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 @@
         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 @@
             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 @@
                 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):

Modified: branches/13/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py
URL: http://svnview.digium.com/svn/asterisk/branches/13/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py?view=diff&rev=430469&r1=430468&r2=430469
==============================================================================
--- branches/13/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py (original)
+++ branches/13/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py Fri Jan  9 16:08:40 2015
@@ -587,7 +587,11 @@
     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 @@
 
         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 @@
         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 @@
     """
     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 @@
     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)




More information about the asterisk-commits mailing list