port revs 10470 and 10471 from trunk

svn: r10743
This commit is contained in:
James G Sack 2008-05-19 18:07:33 +00:00
parent d9fe3fcf65
commit 6f542c76c6
3 changed files with 72 additions and 4 deletions

View File

@ -269,7 +269,7 @@ def extract_date(text):
# parse the range if we match, if so, return
match = RANGE.match(text)
if match:
(cal1, data1, ignore, cal2, data2) = match.groups()
(cal1, data1, cal2, data2) = match.groups()
cal = CALENDAR_MAP.get(cal1, gen.lib.Date.CAL_GREGORIAN)
@ -309,7 +309,12 @@ def extract_date(text):
dateobj = DATE_CNV.parse(text)
dateobj.set_quality(qual)
return dateobj
# FIXME: explain where/why an IOError might arise
# and also: is such a long try-clause needed
# having this fallback invites "what about other exceptions?"
except IOError:
# fallback strategy (evidently)
return DATE_CNV.set_text(text)
#-------------------------------------------------------------------------

View File

@ -252,11 +252,17 @@ def breakup(txt, limit):
maximum length specified, while breaking words in the middle of a word
to avoid issues with spaces.
"""
if limit < 1:
raise ValueError("breakup: unexpected limit: %r" % limit)
data = []
while limit < len(txt)+1:
idx = limit-1
while txt[idx-1].isspace() or txt[idx].isspace() :
while len(txt) > limit:
# look for non-space pair to break between
idx = limit
while idx>0 and (txt[idx-1].isspace() or txt[idx].isspace()):
idx -= 1
if idx == 0:
#no words to break on, just break at limit anyway
idx = limit
data.append(txt[:idx])
txt = txt[idx:]
if len(txt) > 0:

View File

@ -0,0 +1,57 @@
"""gedwrite_breakup_test tests the breakup function in GedcomWrite"""
import unittest as U
from test.test_util import msg, path_append_parent
path_append_parent();
import _WriteGedcom as WG
class Test1(U.TestCase):
def test1a_common(s):
#txt, limit, [split-results]
L=4
dat = (
##0123456
("a", L, ["a",]),
("abc", L, ["abc",]),
("abcd", L, ["abcd",]),
("abcde", L, ["abcd","e"]),
("1234567", L, ["1234","567"]),
("12345678", L, ["1234","5678"]),
("123456789", L, ["1234","5678", "9"]),
)
for (t,l,r) in dat:
g = WG.breakup(t,l)
s.assertEquals(g,r, msg(g,r, "breakup(%r,%d) results" % (t,l)))
def test1b_spaces(s):
#txt, limit, [split-results]
L=4
dat = (
##0123456
("a b ", L, ["a b ",]),
(" a b", L, [" a b",]),
("asdf g", L, ["asd", "f g"]),
(" z", L, [" ", "z"]),
(" z", L, [" ", " z"]),
(" A", 2, [" ", " ", " ", "A"]),
)
for (t,l,r) in dat:
g = WG.breakup(t,l)
s.assertEquals(g,r, msg(g,r, "breakup(%r,%d) results" % (t,l)))
def test1c_unusual(s):
#just documenting behavior for unlikely input
#txt, limit, [split-results]
dat = (
##0123456
("", 4, []),
("xyz", 1, ["x", "y", "z"]),
)
for (t,l,r) in dat:
g = WG.breakup(t,l)
s.assertEquals(g,r, msg(g,r, "breakup(%r,%d) results" % (t,l)))
s.assertRaises(ValueError, WG.breakup, "xy",0)
if __name__ == "__main__":
U.main()
#===eof===