[Asterisk-code-review] contrib/sip to pjsip: add a --quiet option to avoid prints (asterisk[16])

Pascal Cadotte Michaud asteriskteam at digium.com
Fri Oct 26 11:01:14 CDT 2018


Pascal Cadotte Michaud has uploaded this change for review. ( https://gerrit.asterisk.org/10546


Change subject: contrib/sip_to_pjsip: add a --quiet option to avoid prints
......................................................................

contrib/sip_to_pjsip: add a --quiet option to avoid prints

Using the --quiet or -q option in conjonction with /dev/stdout as the output
file allow the output to be used as a valid configuration.

Given a script that generates a valid sip.conf I can pipe the output of that
script into `sip_to_pjsip.py -q /dev/stdin /dev/stdout`. This allow me to use
that piped command in my pjsip.conf using the `exec` command.

Change-Id: I7b0e2e90e2549f3f8e01dc96701f111b5874c88d
---
M contrib/scripts/sip_to_pjsip/sip_to_pjsip.py
1 file changed, 28 insertions(+), 12 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/46/10546/1

diff --git a/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py b/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py
index d05f97d..3228da2 100755
--- a/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py
+++ b/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py
@@ -1,5 +1,7 @@
 #!/usr/bin/env python
 
+import logging
+import sys
 import optparse
 import socket
 try:
@@ -11,6 +13,8 @@
 
 PREFIX = 'pjsip_'
 
+logger = logging.getLogger(__name__)
+
 ###############################################################################
 ### some utility functions
 ###############################################################################
@@ -101,7 +105,7 @@
             else:
                 merge_value(key, val, section, pjsip, nmapped, type, section_to, key_to)
         except LookupError:
-            print("lookup error")
+            logger.error("lookup error")
             merge_value(key, val, section, pjsip, nmapped, type, section_to, key_to)
             return
     elif key == 'disallow':
@@ -840,11 +844,11 @@
         method = sip.multi_get('general', ['tlsclientmethod',
                                            'sslclientmethod'])[0]
         if section != 'transport-' + protocol + '6':  # print only once
-            print('In chan_sip, you specified the TLS version. With chan_sip,' \
-                  ' this was just for outbound client connections. In' \
-                  ' chan_pjsip, this value is for client and server. Instead,' \
-                  ' consider not to specify \'tlsclientmethod\' for chan_sip' \
-                  ' and \'method = sslv23\' for chan_pjsip.')
+            logger.error('In chan_sip, you specified the TLS version. With chan_sip,' \
+                         ' this was just for outbound client connections. In' \
+                         ' chan_pjsip, this value is for client and server. Instead,' \
+                         ' consider not to specify \'tlsclientmethod\' for chan_sip' \
+                         ' and \'method = sslv23\' for chan_pjsip.')
     except LookupError:
         """
         OpenSSL emerged during the 90s. SSLv2 and SSLv3 were the only
@@ -1247,7 +1251,7 @@
             pjsip.write(fp)
 
     except IOError:
-        print("Could not open file " + filename + " for writing")
+        logger.error("Could not open file %s for writing", filename)
 
 ###############################################################################
 
@@ -1265,9 +1269,21 @@
     parser = optparse.OptionParser(usage=usage)
     parser.add_option('-p', '--prefix', dest='prefix', default=PREFIX,
                       help='output prefix for include files')
+    parser.add_option('-q', '--quiet', dest='quiet', default=False, action='store_true',
+                      help="don't print messages to stdout")
 
     options, args = parser.parse_args()
     PREFIX = options.prefix
+    stdout_handler = logging.StreamHandler(sys.stdout)
+    stdout_handler.setFormatter(logging.Formatter('%(message)s'))
+    if options.quiet:
+        log_level = logging.ERROR
+    else:
+        log_level = logging.INFO
+
+    stdout_handler.setLevel(log_level)
+    logger.addHandler(stdout_handler)
+    logger.setLevel(log_level)
 
     sip_filename = args[0] if len(args) else 'sip.conf'
     pjsip_filename = args[1] if len(args) == 2 else 'pjsip.conf'
@@ -1278,11 +1294,11 @@
     sip_filename, pjsip_filename = cli_options()
     # configuration parser for sip.conf
     sip = astconfigparser.MultiOrderedConfigParser()
-    print('Please, report any issue at:')
-    print('    https://issues.asterisk.org/')
-    print('Reading ' + sip_filename)
+    logger.info('Please, report any issue at:')
+    logger.info('    https://issues.asterisk.org/')
+    logger.info('Reading %s', sip_filename)
     sip.read(sip_filename)
-    print('Converting to PJSIP...')
+    logger.info('Converting to PJSIP...')
     pjsip, non_mappings = convert(sip, pjsip_filename, dict(), False)
-    print('Writing ' + pjsip_filename)
+    logger.info('Writing %s', pjsip_filename)
     write_pjsip(pjsip_filename, pjsip, non_mappings)

-- 
To view, visit https://gerrit.asterisk.org/10546
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 16
Gerrit-MessageType: newchange
Gerrit-Change-Id: I7b0e2e90e2549f3f8e01dc96701f111b5874c88d
Gerrit-Change-Number: 10546
Gerrit-PatchSet: 1
Gerrit-Owner: Pascal Cadotte Michaud <pcm at wazo.io>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20181026/4a246e84/attachment.html>


More information about the asterisk-code-review mailing list