Ticket #4937: locale-independent-formatting-with-tests3.patch

File locale-independent-formatting-with-tests3.patch, 6.1 KB (added by facundobatista, 6 years ago)
  • twisted/conch/ls.py

    === modified file 'twisted/conch/ls.py'
     
    77
    88from time import time, strftime, localtime
    99
     10# locale-independent month names to use instead of strftime's
     11_MONTH_NAMES = dict(zip(range(1, 13),
     12                    "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split()))
     13
    1014
    1115def lsLine(name, s):
     16    """
     17    Build an 'ls' line for a file ('file' in its generic sense, it
     18    can be of any type).
     19    """
    1220    mode = s.st_mode
    1321    perms = array.array('c', '-'*10)
    1422    ft = stat.S_IFMT(mode)
     
    3947    if mode&stat.S_ISGID:
    4048        if perms[6] == 'x': perms[6] = 's'
    4149        else: perms[6] = 'S'
    42     l = perms.tostring()
    43     l += str(s.st_nlink).rjust(5) + ' '
    44     un = str(s.st_uid)
    45     l += un.ljust(9)
    46     gr = str(s.st_gid)
    47     l += gr.ljust(9)
    48     sz = str(s.st_size)
    49     l += sz.rjust(8)
    50     l += ' '
    51     sixmo = 60 * 60 * 24 * 7 * 26
    52     if s.st_mtime + sixmo < time(): # last edited more than 6mo ago
    53         l += strftime("%b %d  %Y ", localtime(s.st_mtime))
     50
     51    lsresult = [
     52        perms.tostring(),
     53        str(s.st_nlink).rjust(5),
     54        ' ',
     55        str(s.st_uid).ljust(9),
     56        str(s.st_gid).ljust(9),
     57        str(s.st_size).rjust(8),
     58        ' ',
     59    ]
     60
     61    # need to specify the month manually, as strftime depends on locale
     62    ttup = localtime(s.st_mtime)
     63    sixmonths = 60 * 60 * 24 * 7 * 26
     64    if s.st_mtime + sixmonths < time(): # last edited more than 6mo ago
     65        strtime = strftime("%%s %d  %Y ", ttup)
    5466    else:
    55         l += strftime("%b %d %H:%M ", localtime(s.st_mtime))
    56     l += name
    57     return l
     67        strtime = strftime("%%s %d %H:%M ", ttup)
     68    lsresult.append(strtime % (_MONTH_NAMES[ttup[1]],))
     69
     70    lsresult.append(name)
     71    return ''.join(lsresult)
    5872
    5973
    6074__all__ = ['lsLine']
  • twisted/conch/test/test_cftp.py

    === modified file 'twisted/conch/test/test_cftp.py'
     
    66Tests for L{twisted.conch.scripts.cftp}.
    77"""
    88
     9import locale
    910import time, sys, os, operator, getpass, struct
    1011from StringIO import StringIO
    1112
     
    142143            '!---------    0 0        0               0 Aug 29 09:33 foo')
    143144
    144145
     146    def test_localeIndependent(self):
     147        """
     148        The month name in the date is locale independent.
     149        """
     150        # A point about three months in the past.
     151        then = self.now - (60 * 60 * 24 * 31 * 3)
     152        stat = os.stat_result((0, 0, 0, 0, 0, 0, 0, 0, then, 0))
     153
     154        # Fake that we're in a language where August is not Aug (e.g.: Spanish)
     155        currentLocale = locale.getlocale()
     156        locale.setlocale(locale.LC_ALL, "es_AR.UTF8")
     157        self.addCleanup(locale.setlocale, locale.LC_ALL, currentLocale)
     158
     159        self.assertEqual(
     160            self._lsInTimezone('America/New_York', stat),
     161            '!---------    0 0        0               0 Aug 28 17:33 foo')
     162        self.assertEqual(
     163            self._lsInTimezone('Pacific/Auckland', stat),
     164            '!---------    0 0        0               0 Aug 29 09:33 foo')
     165
     166    # if alternate locale is not available, the previous test will be
     167    # skipped, please install this locale for it to run
     168    currentLocale = locale.getlocale()
     169    try:
     170        locale.setlocale(locale.LC_ALL, "es_AR.UTF8")
     171    except locale.Error:
     172        test_localeIndependent.skip = "The es_AR.UTF8 locale is not installed."
     173    finally:
     174        locale.setlocale(locale.LC_ALL, currentLocale)
     175
     176
    145177    def test_newSingleDigitDayOfMonth(self):
    146178        """
    147179        A file with a high-resolution timestamp which falls on a day of the
  • twisted/mail/imap4.py

    === modified file 'twisted/mail/imap4.py'
     
    4949import twisted.cred.credentials
    5050
    5151
     52# locale-independent month names to use instead of strftime's
     53_MONTH_NAMES = dict(zip(range(1, 13),
     54                    "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split()))
    5255
    5356class MessageSet(object):
    5457    """
     
    19041907            log.msg("%d:%r: unpareseable internaldate: %r" % (id, msg, idate))
    19051908            raise IMAP4Exception("Internal failure generating INTERNALDATE")
    19061909
    1907         odate = time.strftime("%d-%b-%Y %H:%M:%S ", ttup[:9])
     1910        # need to specify the month manually, as strftime depends on locale
     1911        strdate = time.strftime("%d-%%s-%Y %H:%M:%S ", ttup[:9])
     1912        odate = strdate % (_MONTH_NAMES[ttup[1]],)
    19081913        if ttup[9] is None:
    19091914            odate = odate + "+0000"
    19101915        else:
  • twisted/mail/test/test_imap.py

    === modified file 'twisted/mail/test/test_imap.py'
     
    1212except ImportError:
    1313    from StringIO import StringIO
    1414
     15import codecs
     16import locale
    1517import os
    1618import types
    17 import codecs
    1819
    1920from zope.interface import implements
    2021
     
    34613462    def testFetchInternalDateUID(self):
    34623463        return self.testFetchInternalDate(1)
    34633464
     3465    def test_FetchInternalDateLocaleIndependent(self):
     3466        """
     3467        The month name in the date is locale independent.
     3468        """
     3469        # Fake that we're in a language where December is not Dec
     3470        currentLocale = locale.getlocale()
     3471        locale.setlocale(locale.LC_ALL, "es_AR.UTF8")
     3472        self.addCleanup(locale.setlocale, locale.LC_ALL, currentLocale)
     3473        return self.testFetchInternalDate(1)
     3474
     3475    # if alternate locale is not available, the previous test will be
     3476    # skipped, please install this locale for it to run
     3477    currentLocale = locale.getlocale()
     3478    try:
     3479        locale.setlocale(locale.LC_ALL, "es_AR.UTF8")
     3480    except locale.Error:
     3481        test_FetchInternalDateLocaleIndependent.skip = ("The es_AR.UTF8 locale"
     3482                                                       " is not installed.")
     3483    finally:
     3484        locale.setlocale(locale.LC_ALL, currentLocale)
     3485
    34643486    def testFetchEnvelope(self, uid=0):
    34653487        self.function = self.client.fetchEnvelope
    34663488        self.messages = '15'