[thorstenp] import idna only when needed. see #4457

This commit is contained in:
Yann Leboulanger 2008-11-05 19:48:58 +00:00
parent 47f34bcd56
commit aaa82b8202
1 changed files with 6 additions and 2 deletions

View File

@ -40,12 +40,10 @@ import sha
import base64 import base64
import sys import sys
from encodings.punycode import punycode_encode from encodings.punycode import punycode_encode
from encodings import idna
import gajim import gajim
from i18n import Q_ from i18n import Q_
from i18n import ngettext from i18n import ngettext
from xmpp_stringprep import nodeprep, resourceprep, nameprep
import xmpp import xmpp
try: try:
@ -121,6 +119,7 @@ def parse_jid(jidstring):
def idn_to_ascii(host): def idn_to_ascii(host):
'''convert IDN (Internationalized Domain Names) to ACE '''convert IDN (Internationalized Domain Names) to ACE
(ASCII-compatible encoding)''' (ASCII-compatible encoding)'''
from encodings import idna
labels = idna.dots.split(host) labels = idna.dots.split(host)
converted_labels = [] converted_labels = []
for label in labels: for label in labels:
@ -130,6 +129,7 @@ def idn_to_ascii(host):
def ascii_to_idn(host): def ascii_to_idn(host):
'''convert ACE (ASCII-compatible encoding) to IDN '''convert ACE (ASCII-compatible encoding) to IDN
(Internationalized Domain Names)''' (Internationalized Domain Names)'''
from encodings import idna
labels = idna.dots.split(host) labels = idna.dots.split(host)
converted_labels = [] converted_labels = []
for label in labels: for label in labels:
@ -140,6 +140,7 @@ def parse_resource(resource):
'''Perform stringprep on resource and return it''' '''Perform stringprep on resource and return it'''
if resource: if resource:
try: try:
from xmpp_stringprep import resourceprep
return resourceprep.prepare(unicode(resource)) return resourceprep.prepare(unicode(resource))
except UnicodeError: except UnicodeError:
raise InvalidFormat, 'Invalid character in resource.' raise InvalidFormat, 'Invalid character in resource.'
@ -151,6 +152,7 @@ def prep(user, server, resource):
if user: if user:
try: try:
from xmpp_stringprep import nodeprep
user = nodeprep.prepare(unicode(user)) user = nodeprep.prepare(unicode(user))
except UnicodeError: except UnicodeError:
raise InvalidFormat, _('Invalid character in username.') raise InvalidFormat, _('Invalid character in username.')
@ -161,12 +163,14 @@ def prep(user, server, resource):
raise InvalidFormat, _('Server address required.') raise InvalidFormat, _('Server address required.')
else: else:
try: try:
from xmpp_stringprep import nameprep
server = nameprep.prepare(unicode(server)) server = nameprep.prepare(unicode(server))
except UnicodeError: except UnicodeError:
raise InvalidFormat, _('Invalid character in hostname.') raise InvalidFormat, _('Invalid character in hostname.')
if resource: if resource:
try: try:
from xmpp_stringprep import resourceprep
resource = resourceprep.prepare(unicode(resource)) resource = resourceprep.prepare(unicode(resource))
except UnicodeError: except UnicodeError:
raise InvalidFormat, _('Invalid character in resource.') raise InvalidFormat, _('Invalid character in resource.')