[svn-commits] mmichelson: branch mmichelson/conversion_script r398834 - /team/mmichelson/co...
SVN commits to the Digium repositories
svn-commits at lists.digium.com
Wed Sep 11 14:20:54 CDT 2013
Author: mmichelson
Date: Wed Sep 11 14:20:52 2013
New Revision: 398834
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=398834
Log:
Fix PEP-8 problems.
This only fixes problems found in code I've added in this
branch. Pre-existing PEP-8 violations have been left in so
that when the code review goes up, the diff will be
confined to the new content.
Modified:
team/mmichelson/conversion_script/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py
Modified: team/mmichelson/conversion_script/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/conversion_script/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py?view=diff&rev=398834&r1=398833&r2=398834
==============================================================================
--- team/mmichelson/conversion_script/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py (original)
+++ team/mmichelson/conversion_script/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py Wed Sep 11 14:20:52 2013
@@ -172,6 +172,7 @@
val = 'no'
set_value('inband_progress', val, section, pjsip, nmapped)
+
def build_host(config, host, section, port_key):
"""
Returns a string composed of a host:port. This assumes that the host
@@ -227,6 +228,7 @@
try:
user = sip.multi_get(section, ['defaultuser', 'username'])[0]
+ result += user + '@'
except LookupError:
# It's fine if there's no user name
pass
@@ -234,6 +236,7 @@
result += build_host(sip, val, section, 'port')
set_value('contact', result, section, pjsip, nmapped, 'aor')
+
def from_mailbox(key, val, section, pjsip, nmapped):
"""Determines whether a mailbox configured in sip.conf should map to
@@ -248,8 +251,9 @@
# No subscribemwi option means default it to 'no'
subscribemwi = 'no'
- set_value('mailboxes', val, section, pjsip, nmapped, 'aor' if subscribemwi ==
- 'yes' else 'endpoint')
+ set_value('mailboxes', val, section, pjsip, nmapped, 'aor'
+ if subscribemwi == 'yes' else 'endpoint')
+
def setup_auth(key, val, section, pjsip, nmapped):
"""
@@ -258,10 +262,11 @@
"""
set_value('username', section, section, pjsip, nmapped, 'auth')
# In chan_sip, if a secret and an md5secret are both specified on a peer,
- # then in practice, only the md5secret is used. If both are encountered then
- # we build an auth section that has both an md5_cred and password. However,
- # the auth_type will indicate to authenticators to use the md5_cred, so like
- # with sip.conf, the password will be there but have no purpose.
+ # then in practice, only the md5secret is used. If both are encountered
+ # then we build an auth section that has both an md5_cred and password.
+ # However, the auth_type will indicate to authenticators to use the
+ # md5_cred, so like with sip.conf, the password will be there but have
+ # no purpose.
if key == 'secret':
set_value('password', val, section, pjsip, nmapped, 'auth')
else:
@@ -301,9 +306,11 @@
set_value('endpoint', section, section, pjsip, nmapped, 'identify')
set_value('match', ip, section, pjsip, nmapped, 'identify')
+
def from_encryption_taglen(key, val, section, pjsip, nmapped):
if val == '32':
set_value('srtp_tag_32', 'yes', section, pjsip, nmapped)
+
def from_dtlsenable(key, val, section, pjsip, nmapped):
if val == 'yes':
@@ -447,19 +454,21 @@
# match
]
+
def add_localnet(section, pjsip, nmapped):
"""
Adds localnet values from sip.conf's general section to a transport in
- pjsip.conf. Ideally, we would have just created a template with the localnet
- sections, but because this is a script, it's not hard to add the same thing
- on to every transport.
- """
- try:
- merge_value('localnet', sip.get('general', 'localnet')[0], 'general', pjsip,
- nmapped, 'transport', section)
+ pjsip.conf. Ideally, we would have just created a template with the
+ localnet sections, but because this is a script, it's not hard to add
+ the same thing on to every transport.
+ """
+ try:
+ merge_value('localnet', sip.get('general', 'localnet')[0], 'general',
+ pjsip, nmapped, 'transport', section)
except LookupError:
# No localnet options configured. No biggie!
pass
+
def set_transport_common(section, pjsip, nmapped):
"""
@@ -475,35 +484,36 @@
try:
merge_value('localnet', sip.get('general', 'localnet')[0], 'general',
- pjsip, nmapped, 'transport', section)
+ pjsip, nmapped, 'transport', section)
except LookupError:
# No localnet options configured. Move on.
pass
try:
set_value('tos', sip.get('general', 'sip_tos')[0], 'general', pjsip,
- nmapped, 'transport', section)
+ nmapped, 'transport', section)
except LookupError:
pass
try:
set_value('cos', sip.get('general', 'sip_cos')[0], 'general', pjsip,
- nmapped, 'transport', section)
+ nmapped, 'transport', section)
except LookupError:
pass
+
def split_hostport(addr):
try:
socket.inet_pton(socket.AF_INET6, addr)
- if not host.startswith('['):
+ if not addr.startswith('['):
return (addr, None)
else:
# If brackets are present, there may be a port as well
- port = re.match('\[.*\]:(\d+)', host)
- if port:
- return (host, port.group(1))
+ match = re.match('\[(.*\)]:(\d+)', addr)
+ if match:
+ return (match.group(1), match.group(2))
else:
- return (host, None)
+ return (addr, None)
except socket.error:
pass
@@ -532,19 +542,20 @@
try:
extern_addr = sip.multi_get('general', ['externaddr', 'externip',
- 'externhost'])[0]
+ 'externhost'])[0]
host, port = split_hostport(extern_addr)
set_value('external_signaling_address', host, 'transport-udp', pjsip,
- nmapped, 'transport')
+ nmapped, 'transport')
if port:
set_value('external_signaling_port', port, 'transport-udp', pjsip,
- nmapped, 'transport')
+ nmapped, 'transport')
except LookupError:
pass
set_value('protocol', 'udp', 'transport-udp', pjsip, nmapped, 'transport')
set_value('bind', bind, 'transport-udp', pjsip, nmapped, 'transport')
set_transport_common('transport-udp', pjsip, nmapped)
+
def create_tcp(sip, pjsip, nmapped):
"""
@@ -574,17 +585,17 @@
try:
extern_addr = sip.multi_get('general', ['externaddr', 'externip',
- 'externhost'])[0]
+ 'externhost'])[0]
host, port = split_hostport(extern_addr)
try:
tcpport = sip.get('general', 'externtcpport')
except:
tcpport = port
set_value('external_signaling_address', host, 'transport-tcp', pjsip,
- nmapped, 'transport')
+ nmapped, 'transport')
if tcpport:
- set_value('external_signaling_port', tcpport, 'transport-tcp', pjsip,
- nmapped, 'transport')
+ set_value('external_signaling_port', tcpport, 'transport-tcp',
+ pjsip, nmapped, 'transport')
except LookupError:
pass
@@ -592,7 +603,6 @@
set_value('bind', bind, 'transport-tcp', pjsip, nmapped, 'transport')
set_transport_common('transport-tcp', pjsip, nmapped)
- #XXX Add externtcpport...
def set_tls_bindaddr(val, pjsip, nmapped):
try:
@@ -609,45 +619,55 @@
if matchv4 or matchv6:
if explicit:
# They provided a port. We'll just use it.
- set_value('bind', bind, 'transport-tls', pjsip, nmapped, 'transport')
+ set_value('bind', bind, 'transport-tls', pjsip, nmapped,
+ 'transport')
return
else:
# Need to strip the port from the UDP address
index = bind.rfind(':')
bind = bind[:index]
- # Reaching this point means either there was no port provided or we stripped
- # the port off. We need to add on the default 5061 port
+ # Reaching this point means either there was no port provided or we
+ # stripped the port off. We need to add on the default 5061 port
bind += ':5061'
set_value('bind', bind, 'transport-tls', pjsip, nmapped, 'transport')
+
def set_tls_private_key(val, pjsip, nmapped):
- set_value('privkey_file', val, 'transport-tls', pjsip, nmapped, 'transport')
+ set_value('privkey_file', val, 'transport-tls', pjsip, nmapped,
+ 'transport')
+
def set_tls_cipher(val, pjsip, nmapped):
set_value('cipher', val, 'transport-tls', pjsip, nmapped, 'transport')
+
def set_tls_cafile(val, pjsip, nmapped):
- set_value('ca_list_file', val, 'transport-tls', pjsip, nmapped, 'transport')
+ set_value('ca_list_file', val, 'transport-tls', pjsip, nmapped,
+ 'transport')
+
def set_tls_verifyclient(val, pjsip, nmapped):
set_value('verify_client', val, 'transport-tls', pjsip, nmapped,
- 'transport')
+ 'transport')
+
def set_tls_verifyserver(val, pjsip, nmapped):
- # sip.conf option is tlsdontverifyserver. pjsip.conf is verify_server. So we
- # need to set the value in pjsip.conf to the opposite of sip.conf
+ # sip.conf option is tlsdontverifyserver. pjsip.conf is verify_server.
+ # So we need to set the value in pjsip.conf to the opposite of sip.conf
if val == 'no':
set_value('verify_server', 'yes', 'transport-tls', pjsip, nmapped,
- 'transport')
+ 'transport')
else:
set_value('verify_server', 'no', 'transport-tls', pjsip, nmapped,
- 'transport')
+ 'transport')
+
def set_tls_method(val, pjsip, nmapped):
set_value('method', val, 'transport-tls', pjsip, nmapped, 'transport')
+
def create_tls(sip, pjsip, nmapped):
"""
@@ -696,17 +716,17 @@
set_transport_common('transport-tls', pjsip, nmapped)
try:
extern_addr = sip.multi_get('general', ['externaddr', 'externip',
- 'externhost'])[0]
+ 'externhost'])[0]
host, port = split_hostport(extern_addr)
try:
tlsport = sip.get('general', 'externtlsport')
except:
tlsport = port
set_value('external_signaling_address', host, 'transport-tls', pjsip,
- nmapped, 'transport')
+ nmapped, 'transport')
if tlsport:
- set_value('external_signaling_port', tlsport, 'transport-tls', pjsip,
- nmapped, 'transport')
+ set_value('external_signaling_port', tlsport, 'transport-tls',
+ pjsip, nmapped, 'transport')
except LookupError:
pass
@@ -715,15 +735,15 @@
"""Finds options in sip.conf general section pertaining to
transport configuration and creates appropriate transport
configuration sections in pjsip.conf.
-
+
sip.conf only allows a single UDP transport, TCP transport,
and TLS transport. As such, the mapping into PJSIP can be made
consistent by defining three sections:
-
+
transport-udp
transport-tcp
transport-tls
-
+
To accommodate the default behaviors in sip.conf, we'll need to
create the UDP transport first, followed by the TCP and TLS transports.
"""
@@ -735,6 +755,7 @@
# TCP settings may be dependent on UDP settings, so do it second.
create_tcp(sip, pjsip, nmapped)
create_tls(sip, pjsip, nmapped)
+
def map_auth(sip, pjsip, nmapped):
try:
@@ -763,6 +784,7 @@
set_value('md5_cred', md5, section, pjsip, nmapped, 'auth')
set_value('auth_type', 'md5', section, pjsip, nmapped, 'auth')
+
class Registration:
def __init__(self, line, retry_interval, max_attempts, outbound_proxy):
self.retry_interval = retry_interval
@@ -782,11 +804,11 @@
self.parse_user_part(prehost)
def parse_host_part(self, host_part):
- pre_expiry, sep, expiry = host_part.partition('~')
- pre_extension, sep, self.extension = pre_expiry.partition('/')
- self.host, sep, self.port = pre_extension.partition(':')
-
- self.expiry = expiry if expiry else '120'
+ pre_expiry, sep, expiry = host_part.partition('~')
+ pre_extension, sep, self.extension = pre_expiry.partition('/')
+ self.host, sep, self.port = pre_extension.partition(':')
+
+ self.expiry = expiry if expiry else '120'
def parse_user_part(self, user_part):
colons = user_part.count(':')
@@ -840,7 +862,7 @@
if self.extension:
set_value('contact_user', self.extension, section, pjsip, nmapped,
'registration')
-
+
set_value('expiration', self.expiry, section, pjsip, nmapped,
'registration')
More information about the svn-commits
mailing list