raise DateError in set2_... if not is_compound()
refactor Date to always use is_compound instead of repeating its logic everywhere svn: r23055
This commit is contained in:
parent
7e04c4b136
commit
6910605c4e
@ -122,8 +122,7 @@ class Span(object):
|
|||||||
v = self.date1.sortval - self.date2.sortval
|
v = self.date1.sortval - self.date2.sortval
|
||||||
self.sort = (v, -Span.ABOUT)
|
self.sort = (v, -Span.ABOUT)
|
||||||
self.minmax = (v - Span.ABOUT, v + Span.ABOUT)
|
self.minmax = (v - Span.ABOUT, v + Span.ABOUT)
|
||||||
elif (self.date2.get_modifier() == Date.MOD_RANGE or
|
elif self.date2.is_compound():
|
||||||
self.date2.get_modifier() == Date.MOD_SPAN):
|
|
||||||
start, stop = self.date2.get_start_stop_range()
|
start, stop = self.date2.get_start_stop_range()
|
||||||
start = Date(*start)
|
start = Date(*start)
|
||||||
stop = Date(*stop)
|
stop = Date(*stop)
|
||||||
@ -148,8 +147,7 @@ class Span(object):
|
|||||||
v = self.date1.sortval - self.date2.sortval
|
v = self.date1.sortval - self.date2.sortval
|
||||||
self.sort = (v, -Span.ABOUT)
|
self.sort = (v, -Span.ABOUT)
|
||||||
self.minmax = (v - Span.ABOUT, v + Span.ABOUT)
|
self.minmax = (v - Span.ABOUT, v + Span.ABOUT)
|
||||||
elif (self.date2.get_modifier() == Date.MOD_RANGE or
|
elif self.date2.is_compound():
|
||||||
self.date2.get_modifier() == Date.MOD_SPAN):
|
|
||||||
v = self.date1.sortval - self.date2.sortval
|
v = self.date1.sortval - self.date2.sortval
|
||||||
self.sort = (v, -Span.ABOUT)
|
self.sort = (v, -Span.ABOUT)
|
||||||
self.minmax = (v - Span.ABOUT, v + Span.ABOUT)
|
self.minmax = (v - Span.ABOUT, v + Span.ABOUT)
|
||||||
@ -170,8 +168,7 @@ class Span(object):
|
|||||||
v = self.date1.sortval - self.date2.sortval
|
v = self.date1.sortval - self.date2.sortval
|
||||||
self.sort = (v, -Span.ABOUT)
|
self.sort = (v, -Span.ABOUT)
|
||||||
self.minmax = (v - Span.ABOUT, v + Span.AFTER)
|
self.minmax = (v - Span.ABOUT, v + Span.AFTER)
|
||||||
elif (self.date2.get_modifier() == Date.MOD_RANGE or
|
elif self.date2.is_compound():
|
||||||
self.date2.get_modifier() == Date.MOD_SPAN):
|
|
||||||
v = self.date1.sortval - self.date2.sortval
|
v = self.date1.sortval - self.date2.sortval
|
||||||
self.sort = (v, -Span.ABOUT)
|
self.sort = (v, -Span.ABOUT)
|
||||||
self.minmax = (v - Span.ABOUT, v + Span.ABOUT)
|
self.minmax = (v - Span.ABOUT, v + Span.ABOUT)
|
||||||
@ -192,13 +189,11 @@ class Span(object):
|
|||||||
v = self.date1.sortval - self.date2.sortval
|
v = self.date1.sortval - self.date2.sortval
|
||||||
self.sort = (v, -Span.ABOUT)
|
self.sort = (v, -Span.ABOUT)
|
||||||
self.minmax = (v - Span.ABOUT, v + Span.ABOUT)
|
self.minmax = (v - Span.ABOUT, v + Span.ABOUT)
|
||||||
elif (self.date2.get_modifier() == Date.MOD_RANGE or
|
elif self.date2.is_compound():
|
||||||
self.date2.get_modifier() == Date.MOD_SPAN):
|
|
||||||
v = self.date1.sortval - self.date2.sortval
|
v = self.date1.sortval - self.date2.sortval
|
||||||
self.sort = (v, -Span.ABOUT)
|
self.sort = (v, -Span.ABOUT)
|
||||||
self.minmax = (v - Span.ABOUT, v + Span.ABOUT)
|
self.minmax = (v - Span.ABOUT, v + Span.ABOUT)
|
||||||
elif (self.date1.get_modifier() == Date.MOD_RANGE or
|
elif self.date1.is_compound():
|
||||||
self.date1.get_modifier() == Date.MOD_SPAN): # SPAN----------------------------
|
|
||||||
if self.date2.get_modifier() == Date.MOD_NONE:
|
if self.date2.get_modifier() == Date.MOD_NONE:
|
||||||
start, stop = self.date1.get_start_stop_range()
|
start, stop = self.date1.get_start_stop_range()
|
||||||
start = Date(*start)
|
start = Date(*start)
|
||||||
@ -219,8 +214,7 @@ class Span(object):
|
|||||||
v = self.date1.sortval - self.date2.sortval
|
v = self.date1.sortval - self.date2.sortval
|
||||||
self.sort = (v, -Span.ABOUT)
|
self.sort = (v, -Span.ABOUT)
|
||||||
self.minmax = (v - Span.ABOUT, v + Span.ABOUT)
|
self.minmax = (v - Span.ABOUT, v + Span.ABOUT)
|
||||||
elif (self.date2.get_modifier() == Date.MOD_RANGE or
|
elif self.date2.is_compound():
|
||||||
self.date2.get_modifier() == Date.MOD_SPAN):
|
|
||||||
start1, stop1 = self.date1.get_start_stop_range()
|
start1, stop1 = self.date1.get_start_stop_range()
|
||||||
start2, stop2 = self.date2.get_start_stop_range()
|
start2, stop2 = self.date2.get_start_stop_range()
|
||||||
start1 = Date(*start1)
|
start1 = Date(*start1)
|
||||||
@ -314,8 +308,7 @@ class Span(object):
|
|||||||
_repr = trans_text("less than") + " " + fdate12
|
_repr = trans_text("less than") + " " + fdate12
|
||||||
elif self.date2.get_modifier() == Date.MOD_ABOUT:
|
elif self.date2.get_modifier() == Date.MOD_ABOUT:
|
||||||
_repr = trans_text("age|about") + " " + fdate12p1
|
_repr = trans_text("age|about") + " " + fdate12p1
|
||||||
elif (self.date2.get_modifier() == Date.MOD_RANGE or
|
elif self.date2.is_compound():
|
||||||
self.date2.get_modifier() == Date.MOD_SPAN):
|
|
||||||
start, stop = self.date2.get_start_stop_range()
|
start, stop = self.date2.get_start_stop_range()
|
||||||
start = Date(*start)
|
start = Date(*start)
|
||||||
stop = Date(*stop)
|
stop = Date(*stop)
|
||||||
@ -330,8 +323,7 @@ class Span(object):
|
|||||||
_repr = trans_text("less than") + " " + fdate12
|
_repr = trans_text("less than") + " " + fdate12
|
||||||
elif self.date2.get_modifier() == Date.MOD_ABOUT:
|
elif self.date2.get_modifier() == Date.MOD_ABOUT:
|
||||||
_repr = trans_text("less than about") + " " + fdate12
|
_repr = trans_text("less than about") + " " + fdate12
|
||||||
elif (self.date2.get_modifier() == Date.MOD_RANGE or
|
elif self.date2.is_compound():
|
||||||
self.date2.get_modifier() == Date.MOD_SPAN):
|
|
||||||
_repr = trans_text("less than") + " " + fdate12
|
_repr = trans_text("less than") + " " + fdate12
|
||||||
elif self.date1.get_modifier() == Date.MOD_AFTER: # AFTER----------------------------
|
elif self.date1.get_modifier() == Date.MOD_AFTER: # AFTER----------------------------
|
||||||
if self.date2.get_modifier() == Date.MOD_NONE:
|
if self.date2.get_modifier() == Date.MOD_NONE:
|
||||||
@ -342,8 +334,7 @@ class Span(object):
|
|||||||
_repr = self._format((-1, -1 , -1))
|
_repr = self._format((-1, -1 , -1))
|
||||||
elif self.date2.get_modifier() == Date.MOD_ABOUT:
|
elif self.date2.get_modifier() == Date.MOD_ABOUT:
|
||||||
_repr = trans_text("more than about") + " " + fdate12p1
|
_repr = trans_text("more than about") + " " + fdate12p1
|
||||||
elif (self.date2.get_modifier() == Date.MOD_RANGE or
|
elif self.date2.is_compound():
|
||||||
self.date2.get_modifier() == Date.MOD_SPAN):
|
|
||||||
_repr = trans_text("more than") + " " + fdate12
|
_repr = trans_text("more than") + " " + fdate12
|
||||||
elif self.date1.get_modifier() == Date.MOD_ABOUT: # ABOUT----------------------------
|
elif self.date1.get_modifier() == Date.MOD_ABOUT: # ABOUT----------------------------
|
||||||
if self.date2.get_modifier() == Date.MOD_NONE:
|
if self.date2.get_modifier() == Date.MOD_NONE:
|
||||||
@ -354,11 +345,9 @@ class Span(object):
|
|||||||
_repr = trans_text("less than about") + " " + fdate12p1
|
_repr = trans_text("less than about") + " " + fdate12p1
|
||||||
elif self.date2.get_modifier() == Date.MOD_ABOUT:
|
elif self.date2.get_modifier() == Date.MOD_ABOUT:
|
||||||
_repr = trans_text("age|about") + " " + fdate12p1
|
_repr = trans_text("age|about") + " " + fdate12p1
|
||||||
elif (self.date2.get_modifier() == Date.MOD_RANGE or
|
elif self.date2.is_compound():
|
||||||
self.date2.get_modifier() == Date.MOD_SPAN):
|
|
||||||
_repr = trans_text("age|about") + " " + fdate12p1
|
_repr = trans_text("age|about") + " " + fdate12p1
|
||||||
elif (self.date1.get_modifier() == Date.MOD_RANGE or
|
elif self.date1.is_compound():
|
||||||
self.date1.get_modifier() == Date.MOD_SPAN): # SPAN----------------------------
|
|
||||||
if self.date2.get_modifier() == Date.MOD_NONE:
|
if self.date2.get_modifier() == Date.MOD_NONE:
|
||||||
start, stop = self.date1.get_start_stop_range()
|
start, stop = self.date1.get_start_stop_range()
|
||||||
start = Date(*start)
|
start = Date(*start)
|
||||||
@ -371,8 +360,7 @@ class Span(object):
|
|||||||
_repr = trans_text("less than") + " " + fdate12
|
_repr = trans_text("less than") + " " + fdate12
|
||||||
elif self.date2.get_modifier() == Date.MOD_ABOUT:
|
elif self.date2.get_modifier() == Date.MOD_ABOUT:
|
||||||
_repr = trans_text("age|about") + " " + fdate12p1
|
_repr = trans_text("age|about") + " " + fdate12p1
|
||||||
elif (self.date2.get_modifier() == Date.MOD_RANGE or
|
elif self.date2.is_compound():
|
||||||
self.date2.get_modifier() == Date.MOD_SPAN):
|
|
||||||
start1, stop1 = self.date1.get_start_stop_range()
|
start1, stop1 = self.date1.get_start_stop_range()
|
||||||
start2, stop2 = self.date2.get_start_stop_range()
|
start2, stop2 = self.date2.get_start_stop_range()
|
||||||
start1 = Date(*start1)
|
start1 = Date(*start1)
|
||||||
@ -1045,7 +1033,7 @@ class Date(object):
|
|||||||
(self.dateval[Date._POS_YR]) % 10,
|
(self.dateval[Date._POS_YR]) % 10,
|
||||||
self.dateval[Date._POS_MON],
|
self.dateval[Date._POS_MON],
|
||||||
self.dateval[Date._POS_DAY])
|
self.dateval[Date._POS_DAY])
|
||||||
elif self.modifier == Date.MOD_RANGE or self.modifier == Date.MOD_SPAN:
|
elif self.is_compound():
|
||||||
val = "%04d-%02d-%02d - %04d-%02d-%02d" % (
|
val = "%04d-%02d-%02d - %04d-%02d-%02d" % (
|
||||||
self.dateval[Date._POS_YR], self.dateval[Date._POS_MON],
|
self.dateval[Date._POS_YR], self.dateval[Date._POS_MON],
|
||||||
self.dateval[Date._POS_DAY], self.dateval[Date._POS_RYR],
|
self.dateval[Date._POS_DAY], self.dateval[Date._POS_RYR],
|
||||||
@ -1208,7 +1196,7 @@ class Date(object):
|
|||||||
of (0, 0, 0, False) is returned. Otherwise, a date of (DD, MM, YY, slash)
|
of (0, 0, 0, False) is returned. Otherwise, a date of (DD, MM, YY, slash)
|
||||||
is returned. If slash is True, then the date is in the form of 1530/1.
|
is returned. If slash is True, then the date is in the form of 1530/1.
|
||||||
"""
|
"""
|
||||||
if self.modifier == Date.MOD_RANGE or self.modifier == Date.MOD_SPAN:
|
if self.is_compound():
|
||||||
val = self.dateval[4:8]
|
val = self.dateval[4:8]
|
||||||
else:
|
else:
|
||||||
val = Date.EMPTY
|
val = Date.EMPTY
|
||||||
@ -1238,7 +1226,7 @@ class Date(object):
|
|||||||
"""
|
"""
|
||||||
Return the item specified.
|
Return the item specified.
|
||||||
"""
|
"""
|
||||||
if self.modifier == Date.MOD_SPAN or self.modifier == Date.MOD_RANGE:
|
if self.is_compound():
|
||||||
val = self.dateval[index]
|
val = self.dateval[index]
|
||||||
else:
|
else:
|
||||||
val = 0
|
val = 0
|
||||||
@ -1296,11 +1284,16 @@ class Date(object):
|
|||||||
self.dateval = tuple(dv)
|
self.dateval = tuple(dv)
|
||||||
self._calc_sort_value()
|
self._calc_sort_value()
|
||||||
|
|
||||||
|
def _assert_compound(self):
|
||||||
|
if not self.is_compound():
|
||||||
|
raise DateError("Operation allowed for compound dates only!")
|
||||||
|
|
||||||
def set2_yr_mon_day(self, year, month, day):
|
def set2_yr_mon_day(self, year, month, day):
|
||||||
"""
|
"""
|
||||||
Set the year, month, and day values in the 2nd part of
|
Set the year, month, and day values in the 2nd part of
|
||||||
a compound date (range or span).
|
a compound date (range or span).
|
||||||
"""
|
"""
|
||||||
|
self._assert_compound()
|
||||||
dv = list(self.dateval)
|
dv = list(self.dateval)
|
||||||
dv[Date._POS_RYR] = year
|
dv[Date._POS_RYR] = year
|
||||||
dv[Date._POS_RMON] = month
|
dv[Date._POS_RMON] = month
|
||||||
@ -1344,6 +1337,7 @@ class Date(object):
|
|||||||
Set the year, month, and day values by offset in the 2nd part
|
Set the year, month, and day values by offset in the 2nd part
|
||||||
of a compound date (range or span).
|
of a compound date (range or span).
|
||||||
"""
|
"""
|
||||||
|
self._assert_compound()
|
||||||
dv = list(self.dateval)
|
dv = list(self.dateval)
|
||||||
if dv[Date._POS_RYR]:
|
if dv[Date._POS_RYR]:
|
||||||
dv[Date._POS_RYR] += year
|
dv[Date._POS_RYR] += year
|
||||||
@ -1383,8 +1377,7 @@ class Date(object):
|
|||||||
new_date = self
|
new_date = self
|
||||||
retval = Date(new_date)
|
retval = Date(new_date)
|
||||||
retval.set_yr_mon_day_offset(year, month, day)
|
retval.set_yr_mon_day_offset(year, month, day)
|
||||||
if (self.get_modifier() == Date.MOD_RANGE or
|
if self.is_compound():
|
||||||
self.get_modifier() == Date.MOD_SPAN):
|
|
||||||
retval.set2_yr_mon_day_offset(year, month, day)
|
retval.set2_yr_mon_day_offset(year, month, day)
|
||||||
if orig_cal == 0:
|
if orig_cal == 0:
|
||||||
return retval
|
return retval
|
||||||
@ -1398,8 +1391,7 @@ class Date(object):
|
|||||||
"""
|
"""
|
||||||
retval = Date(self)
|
retval = Date(self)
|
||||||
retval.set_yr_mon_day(year, month, day)
|
retval.set_yr_mon_day(year, month, day)
|
||||||
if (self.get_modifier() == Date.MOD_RANGE or
|
if self.is_compound():
|
||||||
self.get_modifier() == Date.MOD_SPAN):
|
|
||||||
retval.set2_yr_mon_day(year, month, day)
|
retval.set2_yr_mon_day(year, month, day)
|
||||||
return retval
|
return retval
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ from ...config import config
|
|||||||
from ...datehandler import get_date_formats, set_format
|
from ...datehandler import get_date_formats, set_format
|
||||||
from ...datehandler import parser as _dp
|
from ...datehandler import parser as _dp
|
||||||
from ...datehandler import displayer as _dd
|
from ...datehandler import displayer as _dd
|
||||||
from ...lib.date import Date
|
from ...lib.date import Date, DateError
|
||||||
|
|
||||||
date_tests = {}
|
date_tests = {}
|
||||||
|
|
||||||
@ -470,5 +470,32 @@ class Test_set2(BaseDateTest):
|
|||||||
self.assertEqual(start, (1000, 10, 10))
|
self.assertEqual(start, (1000, 10, 10))
|
||||||
self.assertEqual(stop, (1000, 10, 10))
|
self.assertEqual(stop, (1000, 10, 10))
|
||||||
|
|
||||||
|
def test_set2_ymd_raises_error_unless_compound(self):
|
||||||
|
for mod in (Date.MOD_NONE, Date.MOD_BEFORE, Date.MOD_AFTER,
|
||||||
|
Date.MOD_ABOUT,
|
||||||
|
Date.MOD_TEXTONLY):
|
||||||
|
self.date.set_modifier(mod)
|
||||||
|
try:
|
||||||
|
self.date.set2_yr_mon_day(2013, 2, 2)
|
||||||
|
self.assertTrue(False,
|
||||||
|
"Modifier: {}, dateval: {} - exception expected!".format(
|
||||||
|
mod, self.date.dateval))
|
||||||
|
except DateError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test_set2_ymd_offset_raises_error_unless_compound(self):
|
||||||
|
for mod in (Date.MOD_NONE, Date.MOD_BEFORE, Date.MOD_AFTER,
|
||||||
|
Date.MOD_ABOUT,
|
||||||
|
Date.MOD_TEXTONLY):
|
||||||
|
self.date.set_modifier(mod)
|
||||||
|
try:
|
||||||
|
self.date.set2_yr_mon_day_offset(year=-1)
|
||||||
|
self.assertTrue(False,
|
||||||
|
"Modifier: {}, dateval: {} - exception expected!".format(
|
||||||
|
mod, self.date.dateval))
|
||||||
|
except DateError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user