示例#1
0
    def _lngettext(self, msgid1, msgid2, n):
        if n == 1:
            tmsg = msgid1
        else:
            tmsg = msgid2

        if not isbasestring(msgid1):
            return ''
        u_msgid1 = to_unicode(msgid1, encoding=self.input_charset)
        try:
            #pylint:disable-msg=E1101
            tmsg = self._catalog[(u_msgid1, self.plural(n))]
        except KeyError:
            if self._fallback:
                try:
                    tmsg = self._fallback.lngettext(msgid1, msgid2, n)
                except (AttributeError, UnicodeError):
                    # Ignore UnicodeErrors: We'll do our own encoding next
                    pass

        # Next decide what encoding to use for the strings we return
        output_encoding = (self._output_charset or
                locale.getpreferredencoding())

        return self._reencode_if_necessary(tmsg, output_encoding)
示例#2
0
    def _lngettext(self, msgid1, msgid2, n):
        if n == 1:
            tmsg = msgid1
        else:
            tmsg = msgid2

        if not isbasestring(msgid1):
            return ''
        u_msgid1 = to_unicode(msgid1, encoding=self.input_charset)
        try:
            #pylint:disable-msg=E1101
            tmsg = self._catalog[(u_msgid1, self.plural(n))]
        except KeyError:
            if self._fallback:
                try:
                    tmsg = self._fallback.lngettext(msgid1, msgid2, n)
                except (AttributeError, UnicodeError):
                    # Ignore UnicodeErrors: We'll do our own encoding next
                    pass

        # Next decide what encoding to use for the strings we return
        output_encoding = (self._output_charset
                           or locale.getpreferredencoding())

        return self._reencode_if_necessary(tmsg, output_encoding)
示例#3
0
    def _ugettext(self, message):
        if not isbasestring(message):
            return u''
        if self._fallback:
            msg = to_unicode(message, encoding=self.input_charset)
            try:
                message = self._fallback.ugettext(msg)
            except (AttributeError, UnicodeError):
                # Ignore UnicodeErrors: We'll do our own decoding later
                pass

        # Make sure we're returning unicode
        return to_unicode(message, encoding=self.input_charset)
示例#4
0
    def _ugettext(self, message):
        if not isbasestring(message):
            return u''
        if self._fallback:
            msg = to_unicode(message, encoding=self.input_charset)
            try:
                message = self._fallback.ugettext(msg)
            except (AttributeError, UnicodeError):
                # Ignore UnicodeErrors: We'll do our own decoding later
                pass

        # Make sure we're returning unicode
        return to_unicode(message, encoding=self.input_charset)
示例#5
0
    def _ugettext(self, message):
        if not isbasestring(message):
            return u''
        message = to_unicode(message, encoding=self.input_charset)
        try:
            message = self._catalog[message] #pylint:disable-msg=E1101
        except KeyError:
            if self._fallback:
                try:
                    message = self._fallback.ugettext(message)
                except (AttributeError, UnicodeError):
                    # Ignore UnicodeErrors: We'll do our own encoding next
                    pass

        # Make sure that we're returning unicode
        return to_unicode(message, encoding=self.input_charset)
示例#6
0
    def _ugettext(self, message):
        if not isbasestring(message):
            return u''
        message = to_unicode(message, encoding=self.input_charset)
        try:
            message = self._catalog[message]  #pylint:disable-msg=E1101
        except KeyError:
            if self._fallback:
                try:
                    message = self._fallback.ugettext(message)
                except (AttributeError, UnicodeError):
                    # Ignore UnicodeErrors: We'll do our own encoding next
                    pass

        # Make sure that we're returning unicode
        return to_unicode(message, encoding=self.input_charset)
def isiterable(obj, include_string=False):
    '''Check whether an object is an iterable

    :arg obj: Object to test whether it is an iterable
    :kwarg include_string: If :data:`True` and :attr:`obj` is a byte
        :class:`bytes` or :class:`str` string this function will return
        :data:`True`.  If set to :data:`False`, byte :class:`bytes` and
        :class:`str` strings will cause this function to return
        :data:`False`.  Default :data:`False`.
    :returns: :data:`True` if :attr:`obj` is iterable, otherwise
        :data:`False`.
    '''
    if include_string or not isbasestring(obj):
        try:
            iter(obj)
        except TypeError:
            return False
        else:
            return True
    return False
示例#8
0
def isiterable(obj, include_string=False):
    '''Check whether an object is an iterable

    :arg obj: Object to test whether it is an iterable
    :kwarg include_string: If :data:`True` and :attr:`obj` is a byte
        :class:`str` or :class:`unicode` string this function will return
        :data:`True`.  If set to :data:`False`, byte :class:`str` and
        :class:`unicode` strings will cause this function to return
        :data:`False`.  Default :data:`False`.
    :returns: :data:`True` if :attr:`obj` is iterable, otherwise
        :data:`False`.
    '''
    if include_string or not isbasestring(obj):
        try:
            iter(obj)
        except TypeError:
            return False
        else:
            return True
    return False
示例#9
0
    def _lgettext(self, message):
        if not isbasestring(message):
            return ''
        tmsg = message
        u_message = to_unicode(message, encoding=self.input_charset)
        try:
            tmsg = self._catalog[u_message] #pylint:disable-msg=E1101
        except KeyError:
            if self._fallback:
                try:
                    tmsg = self._fallback.lgettext(message)
                except (AttributeError, UnicodeError):
                    # Ignore UnicodeErrors: We'll do our own encoding next
                    pass

        # Next decide what encoding to use for the strings we return
        output_encoding = (self._output_charset or
                locale.getpreferredencoding())

        return self._reencode_if_necessary(tmsg, output_encoding)
示例#10
0
    def _lgettext(self, message):
        if not isbasestring(message):
            return ''
        tmsg = message
        u_message = to_unicode(message, encoding=self.input_charset)
        try:
            tmsg = self._catalog[u_message]  #pylint:disable-msg=E1101
        except KeyError:
            if self._fallback:
                try:
                    tmsg = self._fallback.lgettext(message)
                except (AttributeError, UnicodeError):
                    # Ignore UnicodeErrors: We'll do our own encoding next
                    pass

        # Next decide what encoding to use for the strings we return
        output_encoding = (self._output_charset
                           or locale.getpreferredencoding())

        return self._reencode_if_necessary(tmsg, output_encoding)
示例#11
0
    def _ungettext(self, msgid1, msgid2, n):
        if n == 1:
            tmsg = msgid1
        else:
            tmsg = msgid2

        if not isbasestring(msgid1):
            return u''
        u_msgid1 = to_unicode(msgid1, encoding=self.input_charset)
        try:
            #pylint:disable-msg=E1101
            tmsg = self._catalog[(u_msgid1, self.plural(n))]
        except KeyError:
            if self._fallback:
                try:
                    tmsg = self._fallback.ungettext(msgid1, msgid2, n)
                except (AttributeError, UnicodeError):
                    # Ignore UnicodeErrors: We'll do our own encoding next
                    pass

        # Make sure that we're returning unicode
        return to_unicode(tmsg, encoding=self.input_charset, nonstring='empty')
示例#12
0
    def _ungettext(self, msgid1, msgid2, n):
        if n == 1:
            tmsg = msgid1
        else:
            tmsg = msgid2

        if not isbasestring(msgid1):
            return u''
        u_msgid1 = to_unicode(msgid1, encoding=self.input_charset)
        try:
            #pylint:disable-msg=E1101
            tmsg = self._catalog[(u_msgid1, self.plural(n))]
        except KeyError:
            if self._fallback:
                try:
                    tmsg = self._fallback.ungettext(msgid1, msgid2, n)
                except (AttributeError, UnicodeError):
                    # Ignore UnicodeErrors: We'll do our own encoding next
                    pass

        # Make sure that we're returning unicode
        return to_unicode(tmsg, encoding=self.input_charset,
                nonstring='empty')
示例#13
0
 def test_isbasestring(self):
     tools.assert_true(misc.isbasestring('abc'))
     tools.assert_true(misc.isbasestring(u'abc'))
     tools.assert_false(misc.isbasestring(5))
示例#14
0
 def test_isbasestring(self):
     tools.assert_true(misc.isbasestring(b'abc'))
     tools.assert_true(misc.isbasestring('abc'))
     tools.assert_false(misc.isbasestring(5))