Brett's fixes for various bugs and coding issues. Closes SF patch #

593560, with some minor cleanups, line folding and whitespace
normalization by Barry.
This commit is contained in:
Barry Warsaw 2002-09-23 22:46:49 +00:00
parent 02669a35bc
commit 4d895fa125
2 changed files with 95 additions and 68 deletions

View File

@ -2,7 +2,7 @@
CLASSES: CLASSES:
LocaleTime -- Discovers and/or stores locale-specific time information LocaleTime -- Discovers and/or stores locale-specific time information
TimeRE -- Creates regexes for pattern matching string of text containing TimeRE -- Creates regexes for pattern matching a string of text containing
time information as is returned by time.strftime() time information as is returned by time.strftime()
FUNCTIONS: FUNCTIONS:
@ -249,24 +249,23 @@ class LocaleTime(object):
date_time[2] = time.strftime("%X", time_tuple) date_time[2] = time.strftime("%X", time_tuple)
for offset,directive in ((0,'%c'), (1,'%x'), (2,'%X')): for offset,directive in ((0,'%c'), (1,'%x'), (2,'%X')):
current_format = date_time[offset] current_format = date_time[offset]
current_format = current_format.replace('%', '%%') for old, new in (
current_format = current_format.replace(self.f_weekday[2], '%A') ('%', '%%'), (self.f_weekday[2], '%A'),
current_format = current_format.replace(self.f_month[3], '%B') (self.f_month[3], '%B'), (self.a_weekday[2], '%a'),
current_format = current_format.replace(self.a_weekday[2], '%a') (self.a_month[3], '%b'), (self.am_pm[1], '%p'),
current_format = current_format.replace(self.a_month[3], '%b') (self.timezone[0], '%Z'), (self.timezone[1], '%Z'),
current_format = current_format.replace(self.am_pm[1], '%p') ('1999', '%Y'), ('99', '%y'), ('22', '%H'),
current_format = current_format.replace(self.timezone[0], '%Z') ('44', '%M'), ('55', '%S'), ('76', '%j'),
current_format = current_format.replace(self.timezone[1], '%Z') ('17', '%d'), ('03', '%m'), ('3', '%m'),
current_format = current_format.replace('1999', '%Y') # '3' needed for when no leading zero.
current_format = current_format.replace('99', '%y') ('2', '%w'), ('10', '%I')):
current_format = current_format.replace('22', '%H') try:
current_format = current_format.replace('44', '%M') # Done this way to deal with possible lack of locale info
current_format = current_format.replace('55', '%S') # manifesting itself as the empty string (i.e., Swedish's
current_format = current_format.replace('76', '%j') # lack of AM/PM info).
current_format = current_format.replace('17', '%d') current_format = current_format.replace(old, new)
current_format = current_format.replace('03', '%m') except ValueError:
current_format = current_format.replace('2', '%w') pass
current_format = current_format.replace('10', '%I')
time_tuple = time.struct_time((1999,1,3,1,1,1,6,3,0)) time_tuple = time.struct_time((1999,1,3,1,1,1,6,3,0))
if time.strftime(directive, time_tuple).find('00'): if time.strftime(directive, time_tuple).find('00'):
U_W = '%U' U_W = '%U'
@ -321,34 +320,27 @@ class TimeRE(dict):
def __getitem__(self, fetch): def __getitem__(self, fetch):
"""Try to fetch regex; if it does not exist, construct it.""" """Try to fetch regex; if it does not exist, construct it."""
try: try:
return super(TimeRE,self).__getitem__(fetch) return super(TimeRE, self).__getitem__(fetch)
except KeyError: except KeyError:
if fetch == 'A': constructors = {
self[fetch] = self.__seqToRE(self.locale_time.f_weekday, 'A': lambda: self.__seqToRE(self.locale_time.f_weekday, fetch),
fetch) 'a': lambda: self.__seqToRE(self.locale_time.a_weekday, fetch),
elif fetch == 'a': 'B': lambda: self.__seqToRE(self.locale_time.f_month[1:],
self[fetch] = self.__seqToRE(self.locale_time.a_weekday, fetch),
fetch) 'b': lambda: self.__seqToRE(self.locale_time.a_month[1:],
elif fetch == 'B': fetch),
self[fetch] = self.__seqToRE(self.locale_time.f_month[1:], 'c': lambda: self.pattern(self.locale_time.LC_date_time),
fetch) 'p': lambda: self.__seqToRE(self.locale_time.am_pm, fetch),
elif fetch == 'b': 'x': lambda: self.pattern(self.locale_time.LC_date),
self[fetch] = self.__seqToRE(self.locale_time.a_month[1:], 'X': lambda: self.pattern(self.locale_time.LC_time),
fetch) 'Z': lambda: self.__seqToRE(self.locale_time.timezone, fetch),
elif fetch == 'c': '%': lambda: '%',
self[fetch] = self.pattern(self.locale_time.LC_date_time) }
elif fetch == 'p': if fetch in constructors:
self[fetch] = self.__seqToRE(self.locale_time.am_pm, fetch) self[fetch] = constructors[fetch]()
elif fetch == 'x': return self[fetch]
self[fetch] = self.pattern(self.locale_time.LC_date) else:
elif fetch == 'X': raise
self[fetch] = self.pattern(self.locale_time.LC_time)
elif fetch == 'Z':
self[fetch] = self.__seqToRE(self.locale_time.timezone,
fetch)
elif fetch == '%':
return '%'
return super(TimeRE,self).__getitem__(fetch)
def __seqToRE(self, to_convert, directive): def __seqToRE(self, to_convert, directive):
"""Convert a list to a regex string for matching directive.""" """Convert a list to a regex string for matching directive."""
@ -371,11 +363,8 @@ class TimeRE(dict):
to_convert = to_convert[:] # Don't want to change value in-place. to_convert = to_convert[:] # Don't want to change value in-place.
to_convert.sort(sorter) to_convert.sort(sorter)
regex = '(?P<%s>' % directive regex = '|'.join(to_convert)
for item in to_convert: regex = '(?P<%s>%s' % (directive, regex)
regex = "%s(?:%s)|" % (regex, item)
else:
regex = regex[:-1]
return '%s)' % regex return '%s)' % regex
def pattern(self, format): def pattern(self, format):
@ -431,23 +420,24 @@ def strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
elif group_key == 'm': elif group_key == 'm':
month = int(found_dict['m']) month = int(found_dict['m'])
elif group_key == 'B': elif group_key == 'B':
month = locale_time.f_month.index(found_dict['B']) month = _insensitiveindex(locale_time.f_month, found_dict['B'])
elif group_key == 'b': elif group_key == 'b':
month = locale_time.a_month.index(found_dict['b']) month = _insensitiveindex(locale_time.a_month, found_dict['b'])
elif group_key == 'd': elif group_key == 'd':
day = int(found_dict['d']) day = int(found_dict['d'])
elif group_key is 'H': elif group_key is 'H':
hour = int(found_dict['H']) hour = int(found_dict['H'])
elif group_key == 'I': elif group_key == 'I':
hour = int(found_dict['I']) hour = int(found_dict['I'])
ampm = found_dict.get('p') ampm = found_dict.get('p', '').lower()
if ampm == locale_time.am_pm[0]: # If there was no AM/PM indicator, we'll treat this like AM
if ampm in ('', locale_time.am_pm[0].lower()):
# We're in AM so the hour is correct unless we're # We're in AM so the hour is correct unless we're
# looking at 12 midnight. # looking at 12 midnight.
# 12 midnight == 12 AM == hour 0 # 12 midnight == 12 AM == hour 0
if hour == 12: if hour == 12:
hour = 0 hour = 0
elif ampm == locale_time.am_pm[1]: elif ampm == locale_time.am_pm[1].lower():
# We're in PM so we need to add 12 to the hour unless # We're in PM so we need to add 12 to the hour unless
# we're looking at 12 noon. # we're looking at 12 noon.
# 12 noon == 12 PM == hour 12 # 12 noon == 12 PM == hour 12
@ -458,9 +448,11 @@ def strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
elif group_key == 'S': elif group_key == 'S':
second = int(found_dict['S']) second = int(found_dict['S'])
elif group_key == 'A': elif group_key == 'A':
weekday = locale_time.f_weekday.index(found_dict['A']) weekday = _insensitiveindex(locale_time.f_weekday,
found_dict['A'])
elif group_key == 'a': elif group_key == 'a':
weekday = locale_time.a_weekday.index(found_dict['a']) weekday = _insensitiveindex(locale_time.a_weekday,
found_dict['a'])
elif group_key == 'w': elif group_key == 'w':
weekday = int(found_dict['w']) weekday = int(found_dict['w'])
if weekday == 0: if weekday == 0:
@ -470,12 +462,15 @@ def strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
elif group_key == 'j': elif group_key == 'j':
julian = int(found_dict['j']) julian = int(found_dict['j'])
elif group_key == 'Z': elif group_key == 'Z':
if locale_time.timezone[0] == found_dict['Z']: found_zone = found_dict['Z'].lower()
if locale_time.timezone[0].lower() == found_zone:
tz = 0 tz = 0
elif locale_time.timezone[1] == found_dict['Z']: elif locale_time.timezone[1].lower() == found_zone:
tz = 1 tz = 1
elif locale_time.timezone[2] == found_dict['Z']: elif locale_time.timezone[2].lower() == found_zone:
tz = 0 tz = 0
#XXX <bc>: If calculating fxns are never exposed to the general
# populous then just inline calculations.
if julian == -1 and year != -1 and month != -1 and day != -1: if julian == -1 and year != -1 and month != -1 and day != -1:
julian = julianday(year, month, day) julian = julianday(year, month, day)
if (month == -1 or day == -1) and julian != -1 and year != -1: if (month == -1 or day == -1) and julian != -1 and year != -1:
@ -485,6 +480,19 @@ def strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
return time.struct_time( return time.struct_time(
(year,month,day,hour,minute,second,weekday, julian,tz)) (year,month,day,hour,minute,second,weekday, julian,tz))
def _insensitiveindex(lst, findme):
# Perform a case-insensitive index search.
#XXX <bc>: If LocaleTime is not exposed, then consider removing this and
# just lowercase when LocaleTime sets its vars and lowercasing
# search values.
findme = findme.lower()
for key,item in enumerate(lst):
if item.lower() == findme:
return key
else:
raise ValueError("value not in list")
def firstjulian(year): def firstjulian(year):
"""Calculate the Julian date up until the first of the year.""" """Calculate the Julian date up until the first of the year."""
return ((146097 * (year + 4799)) // 400) - 31738 return ((146097 * (year + 4799)) // 400) - 31738

View File

@ -1,4 +1,4 @@
"""PyUnit testing against strptime >= 2.1.0.""" """PyUnit testing against strptime"""
import unittest import unittest
import time import time
@ -55,12 +55,21 @@ class LocaleTime_Tests(unittest.TestCase):
def test_date_time(self): def test_date_time(self):
# Check that LC_date_time, LC_date, and LC_time are correct # Check that LC_date_time, LC_date, and LC_time are correct
## strftime_output = time.strftime("%c", self.time_tuple) # the magic date is used so as to not have issues with %c when day of
## self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_date_time, self.time_tuple), "LC_date_time incorrect") # the month is a single digit and has a leading space. This is not an
strftime_output = time.strftime("%x", self.time_tuple) # issue since strptime still parses it correctly. The problem is
self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_date, self.time_tuple), "LC_date incorrect") # testing these directives for correctness by comparing strftime
strftime_output = time.strftime("%X", self.time_tuple) # output.
self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_time, self.time_tuple), "LC_time incorrect") magic_date = (1999, 3, 17, 22, 44, 55, 2, 76, 0)
strftime_output = time.strftime("%c", magic_date)
self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_date_time, magic_date), "LC_date_time incorrect")
strftime_output = time.strftime("%x", magic_date)
self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_date, magic_date), "LC_date incorrect")
strftime_output = time.strftime("%X", magic_date)
self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_time, magic_date), "LC_time incorrect")
LT = _strptime.LocaleTime(am_pm=('',''))
self.failUnless(LT.LC_time, "LocaleTime's LC directives cannot handle "
"empty strings")
def test_lang(self): def test_lang(self):
# Make sure lang is set # Make sure lang is set
@ -221,6 +230,16 @@ class StrptimeTests(unittest.TestCase):
strp_output = _strptime.strptime(strf_output, "%m %% %Y") strp_output = _strptime.strptime(strf_output, "%m %% %Y")
self.failUnless(strp_output[0] == self.time_tuple[0] and strp_output[1] == self.time_tuple[1], "handling of percent sign failed") self.failUnless(strp_output[0] == self.time_tuple[0] and strp_output[1] == self.time_tuple[1], "handling of percent sign failed")
def test_caseinsensitive(self):
# Should handle names case-insensitively.
strf_output = time.strftime("%B", self.time_tuple)
self.failUnless(_strptime.strptime(strf_output.upper(), "%B"),
"strptime does not handle ALL-CAPS names properly")
self.failUnless(_strptime.strptime(strf_output.lower(), "%B"),
"strptime does not handle lowercase names properly")
self.failUnless(_strptime.strptime(strf_output.capitalize(), "%B"),
"strptime does not handle capword names properly")
class FxnTests(unittest.TestCase): class FxnTests(unittest.TestCase):
"""Test functions that fill in info by validating result and are triggered properly.""" """Test functions that fill in info by validating result and are triggered properly."""