if self.uts_46:
label = idna.uts46_remap(label, False, False)
return _escapify(idna.ulabel(label), True)
- except IDNAError as e:
+ except idna.IDNAError as e:
raise IDNAException(idna_exception=e)
s = b'.'.join(map(_escapify, l))
return s
- def to_unicode(self, omit_final_dot=False, idna=None):
+ def to_unicode(self, omit_final_dot=False, idna_codec=None):
"""Convert name to Unicode text format.
IDN ACE labels are converted to Unicode.
@param omit_final_dot: If True, don't emit the final dot (denoting the
root label) for absolute names. The default is False.
@type omit_final_dot: bool
- @param: idna: IDNA encoder/decoder. If None, the default IDNA 2003
+ @param: idna_codec: IDNA encoder/decoder. If None, the default IDNA
+ 2003
encoder/decoder is used.
- @type idna: dns.name.IDNA
+ @type idna_codec: dns.name.IDNA
@rtype: string
"""
l = self.labels[:-1]
else:
l = self.labels
- if idna is None:
- idna = IDNA_2003
- return u'.'.join([idna.decode(x) for x in l])
+ if idna_codec is None:
+ idna_codec = IDNA_2003
+ return u'.'.join([idna_codec.decode(x) for x in l])
def to_digestable(self, origin=None):
"""Convert name to a format suitable for digesting in hashes.
empty = Name([])
-def from_unicode(text, origin=root, idna=None):
+def from_unicode(text, origin=root, idna_codec=None):
"""Convert unicode text into a Name object.
Labels are encoded in IDN ACE form.
@type text: Unicode string
@param origin: The origin to append to non-absolute names.
@type origin: dns.name.Name
- @param: idna: IDNA encoder/decoder. If None, the default IDNA 2003
+ @param: idna_codec: IDNA encoder/decoder. If None, the default IDNA 2003
encoder/decoder is used.
- @type idna: dns.name.IDNA
+ @type idna_codec: dns.name.IDNA
@rtype: dns.name.Name object
"""
escaping = False
edigits = 0
total = 0
- if idna is None:
- idna = IDNA_2003
+ if idna_codec is None:
+ idna_codec = IDNA_2003
if text == u'@':
text = u''
if text:
elif c in [u'.', u'\u3002', u'\uff0e', u'\uff61']:
if len(label) == 0:
raise EmptyLabel
- labels.append(idna.encode(label))
+ labels.append(idna_codec.encode(label))
label = u''
elif c == u'\\':
escaping = True
if escaping:
raise BadEscape
if len(label) > 0:
- labels.append(idna.encode(label))
+ labels.append(idna_codec.encode(label))
else:
labels.append(b'')
return Name(labels)
-def from_text(text, origin=root, idna=None):
+def from_text(text, origin=root, idna_codec=None):
"""Convert text into a Name object.
@param text: The text to convert into a name.
@type text: string
@param origin: The origin to append to non-absolute names.
@type origin: dns.name.Name
- @param: idna: IDNA encoder/decoder. If None, the default IDNA 2003
+ @param: idna_codec: IDNA encoder/decoder. If None, the default IDNA 2003
encoder/decoder is used.
- @type idna: dns.name.IDNA
+ @type idna_codec: dns.name.IDNA
@rtype: dns.name.Name object
"""
if isinstance(text, text_type):
- return from_unicode(text, origin, idna)
+ return from_unicode(text, origin, idna_codec)
if not isinstance(text, binary_type):
raise ValueError("input to from_text() must be a string")
if not (origin is None or isinstance(origin, Name)):
def from_text_list(name, ttl, rdclass, rdtype, text_rdatas,
- idna=None):
+ idna_codec=None):
"""Create an RRset with the specified name, TTL, class, and type, and with
the specified list of rdatas in text format.
"""
if isinstance(name, string_types):
- name = dns.name.from_text(name, None, idna=idna)
+ name = dns.name.from_text(name, None, idna_codec=idna_codec)
if isinstance(rdclass, string_types):
rdclass = dns.rdataclass.from_text(rdclass)
if isinstance(rdtype, string_types):
return from_text_list(name, ttl, rdclass, rdtype, text_rdatas)
-def from_rdata_list(name, ttl, rdatas, idna=None):
+def from_rdata_list(name, ttl, rdatas, idna_codec=None):
"""Create an RRset with the specified name and TTL, and with
the specified list of rdata objects.
"""
if isinstance(name, string_types):
- name = dns.name.from_text(name, None, idna=idna)
+ name = dns.name.from_text(name, None, idna_codec=idna_codec)
if len(rdatas) == 0:
raise ValueError("rdata list must not be empty")
import dns.reversename
import dns.e164
-if dns.name.have_idna_2008:
- import idna
-
# pylint: disable=line-too-long
def testFromUnicodeIDNA2003Explicit(self):
t = u'Königsgäßchen'
- e = dns.name.from_unicode(t, idna=dns.name.IDNA_2003)
+ e = dns.name.from_unicode(t, idna_codec=dns.name.IDNA_2003)
self.assertEqual(str(e), 'xn--knigsgsschen-lcb0w.')
def testFromUnicodeIDNA2003Default(self):
if dns.name.have_idna_2008:
t = u'Königsgäßchen'
def bad():
- return dns.name.from_unicode(t,
- idna=dns.name.IDNA_2008_Strict)
+ codec = dns.name.IDNA_2008_Strict
+ return dns.name.from_unicode(t, idna_codec=codec)
self.failUnlessRaises(dns.name.IDNAException, bad)
- e1 = dns.name.from_unicode(t, idna=dns.name.IDNA_2008)
+ e1 = dns.name.from_unicode(t, idna_codec=dns.name.IDNA_2008)
self.assertEqual(str(e1), 'xn--knigsgchen-b4a3dun.')
- e2 = dns.name.from_unicode(t, idna=dns.name.IDNA_2008_Transitional)
+ c2 = dns.name.IDNA_2008_Transitional
+ e2 = dns.name.from_unicode(t, idna_codec=c2)
self.assertEqual(str(e2), 'xn--knigsgsschen-lcb0w.')
def testToUnicode1(self):
def testToUnicode4(self):
if dns.name.have_idna_2008:
- n = dns.name.from_text(u'ドメイン.テスト', idna=dns.name.IDNA_2008)
+ n = dns.name.from_text(u'ドメイン.テスト',
+ idna_codec=dns.name.IDNA_2008)
s = n.to_unicode()
self.assertEqual(str(n), 'xn--eckwd4c7c.xn--zckzah.')
self.assertEqual(s, u'ドメイン.テスト.')