mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
Use six.python_2_unicode_compatible
Rename __unicode__ to __str__ in classes which define it and use the six.python_2_unicode_compatible decorator on them to make them compatible with both Python 2 and 3. Additional changes were required for the ipapython.dnsutil.DNSName class, because it defined both __str__ and __unicode__. Reviewed-By: Petr Viktorin <pviktori@redhat.com>
This commit is contained in:
parent
ebdfa4380b
commit
cf9bf9dcaf
@ -116,6 +116,8 @@ import threading
|
|||||||
import locale
|
import locale
|
||||||
import gettext
|
import gettext
|
||||||
|
|
||||||
|
import six
|
||||||
|
|
||||||
from ipalib.request import context
|
from ipalib.request import context
|
||||||
|
|
||||||
|
|
||||||
@ -187,6 +189,7 @@ class LazyText(object):
|
|||||||
return other + ConcatenatedLazyText(self)
|
return other + ConcatenatedLazyText(self)
|
||||||
|
|
||||||
|
|
||||||
|
@six.python_2_unicode_compatible
|
||||||
class Gettext(LazyText):
|
class Gettext(LazyText):
|
||||||
"""
|
"""
|
||||||
Deferred translation using ``gettext.ugettext()``.
|
Deferred translation using ``gettext.ugettext()``.
|
||||||
@ -241,7 +244,7 @@ class Gettext(LazyText):
|
|||||||
return '%s(%r, domain=%r, localedir=%r)' % (self.__class__.__name__,
|
return '%s(%r, domain=%r, localedir=%r)' % (self.__class__.__name__,
|
||||||
self.msg, self.domain, self.localedir)
|
self.msg, self.domain, self.localedir)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __str__(self):
|
||||||
"""
|
"""
|
||||||
Translate this message and return as a ``unicode`` instance.
|
Translate this message and return as a ``unicode`` instance.
|
||||||
"""
|
"""
|
||||||
@ -252,12 +255,13 @@ class Gettext(LazyText):
|
|||||||
return g(self.msg)
|
return g(self.msg)
|
||||||
|
|
||||||
def __json__(self):
|
def __json__(self):
|
||||||
return self.__unicode__()
|
return self.__unicode__() #pylint: disable=no-member
|
||||||
|
|
||||||
def __mod__(self, kw):
|
def __mod__(self, kw):
|
||||||
return self.__unicode__() % kw
|
return self.__unicode__() % kw #pylint: disable=no-member
|
||||||
|
|
||||||
|
|
||||||
|
@six.python_2_unicode_compatible
|
||||||
class FixMe(Gettext):
|
class FixMe(Gettext):
|
||||||
"""
|
"""
|
||||||
Non-translated place-holder for UI labels.
|
Non-translated place-holder for UI labels.
|
||||||
@ -303,7 +307,7 @@ class FixMe(Gettext):
|
|||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '%s(%r)' % (self.__class__.__name__, self.msg)
|
return '%s(%r)' % (self.__class__.__name__, self.msg)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __str__(self):
|
||||||
return u'<%s>' % self.msg
|
return u'<%s>' % self.msg
|
||||||
|
|
||||||
|
|
||||||
@ -400,6 +404,7 @@ class NGettext(LazyText):
|
|||||||
return ng(self.singular, self.plural, count)
|
return ng(self.singular, self.plural, count)
|
||||||
|
|
||||||
|
|
||||||
|
@six.python_2_unicode_compatible
|
||||||
class ConcatenatedLazyText(object):
|
class ConcatenatedLazyText(object):
|
||||||
"""Concatenation of multiple strings, or any objects convertible to unicode
|
"""Concatenation of multiple strings, or any objects convertible to unicode
|
||||||
|
|
||||||
@ -415,7 +420,7 @@ class ConcatenatedLazyText(object):
|
|||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '%s(%r)' % (self.__class__.__name__, self.components)
|
return '%s(%r)' % (self.__class__.__name__, self.components)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __str__(self):
|
||||||
return u''.join(unicode(c) for c in self.components)
|
return u''.join(unicode(c) for c in self.components)
|
||||||
|
|
||||||
def __json__(self):
|
def __json__(self):
|
||||||
|
@ -21,18 +21,22 @@ import dns.name
|
|||||||
import dns.exception
|
import dns.exception
|
||||||
import copy
|
import copy
|
||||||
|
|
||||||
|
import six
|
||||||
|
|
||||||
|
|
||||||
|
@six.python_2_unicode_compatible
|
||||||
class DNSName(dns.name.Name):
|
class DNSName(dns.name.Name):
|
||||||
labels = None # make pylint happy
|
labels = None # make pylint happy
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_text(cls, labels, origin=None):
|
||||||
|
return cls(dns.name.from_text(labels, origin))
|
||||||
|
|
||||||
def __init__(self, labels, origin=None):
|
def __init__(self, labels, origin=None):
|
||||||
try:
|
try:
|
||||||
if isinstance(labels, str):
|
if isinstance(labels, six.string_types):
|
||||||
#pylint: disable=E1101
|
#pylint: disable=E1101
|
||||||
labels = dns.name.from_text(labels, origin).labels
|
labels = dns.name.from_unicode(unicode(labels), origin).labels
|
||||||
elif isinstance(labels, unicode):
|
|
||||||
#pylint: disable=E1101
|
|
||||||
labels = dns.name.from_unicode(labels, origin).labels
|
|
||||||
elif isinstance(labels, dns.name.Name):
|
elif isinstance(labels, dns.name.Name):
|
||||||
labels = labels.labels
|
labels = labels.labels
|
||||||
|
|
||||||
@ -54,14 +58,11 @@ class DNSName(dns.name.Name):
|
|||||||
return DNSName(copy.deepcopy(self.labels, memo))
|
return DNSName(copy.deepcopy(self.labels, memo))
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.to_text()
|
|
||||||
|
|
||||||
def __unicode__(self):
|
|
||||||
return self.to_unicode()
|
return self.to_unicode()
|
||||||
|
|
||||||
def ToASCII(self):
|
def ToASCII(self):
|
||||||
#method named by RFC 3490 and python standard library
|
#method named by RFC 3490 and python standard library
|
||||||
return str(self).decode('ascii') # must be unicode string
|
return self.to_text().decode('ascii') # must be unicode string
|
||||||
|
|
||||||
def canonicalize(self):
|
def canonicalize(self):
|
||||||
return DNSName(super(DNSName, self).canonicalize())
|
return DNSName(super(DNSName, self).canonicalize())
|
||||||
|
@ -833,7 +833,7 @@ class LDAPClient(object):
|
|||||||
elif isinstance(val, (unicode, six.integer_types, long, Decimal, DN)):
|
elif isinstance(val, (unicode, six.integer_types, long, Decimal, DN)):
|
||||||
return value_to_utf8(val)
|
return value_to_utf8(val)
|
||||||
elif isinstance(val, DNSName):
|
elif isinstance(val, DNSName):
|
||||||
return str(val)
|
return val.to_text()
|
||||||
elif isinstance(val, str):
|
elif isinstance(val, str):
|
||||||
return val
|
return val
|
||||||
elif isinstance(val, list):
|
elif isinstance(val, list):
|
||||||
@ -863,6 +863,8 @@ class LDAPClient(object):
|
|||||||
return val.decode('utf-8')
|
return val.decode('utf-8')
|
||||||
elif target_type is datetime.datetime:
|
elif target_type is datetime.datetime:
|
||||||
return datetime.datetime.strptime(val, LDAP_GENERALIZED_TIME_FORMAT)
|
return datetime.datetime.strptime(val, LDAP_GENERALIZED_TIME_FORMAT)
|
||||||
|
elif target_type is DNSName:
|
||||||
|
return DNSName.from_text(val)
|
||||||
else:
|
else:
|
||||||
return target_type(val)
|
return target_type(val)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
Loading…
Reference in New Issue
Block a user