From 0753f87630b2874c10c0d589edccaf3fe83a142a Mon Sep 17 00:00:00 2001 From: Vassilii Khachaturov Date: Wed, 27 Nov 2013 18:21:15 +0200 Subject: [PATCH] 7212: convert invalid date to text on .gw import Back-port from master [829986] [1ac0e2] --- src/plugins/import/ImportGeneWeb.py | 20 ++++++-- src/plugins/import/test/ImportGeneWeb_test.py | 49 +++++++++++++++++++ 2 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 src/plugins/import/test/ImportGeneWeb_test.py diff --git a/src/plugins/import/ImportGeneWeb.py b/src/plugins/import/ImportGeneWeb.py index 3e0891c7e..abd36e0fc 100644 --- a/src/plugins/import/ImportGeneWeb.py +++ b/src/plugins/import/ImportGeneWeb.py @@ -96,9 +96,10 @@ def importData(database, filename, cb=None): class GeneWebParser(object): def __init__(self, dbase, file): self.db = dbase - self.f = open(file,"rU") - self.filename = file - self.encoding = 'iso-8859-1' + if file: # Unit tests can create the parser w/o underlying file + self.f = open(file,"rU") + self.filename = file + self.encoding = 'iso-8859-1' def get_next_line(self): self.lineno += 1 @@ -805,8 +806,17 @@ class GeneWebParser(object): sub2 = (0,0,0) cal1 = _cal_map.get(groups[2],gen.lib.Date.CAL_GREGORIAN) sub1 = self.sub_date(groups[1]) - date.set(gen.lib.Date.QUAL_NONE,mod, cal1, - (sub1[0],sub1[1],sub1[2],0,sub2[0],sub2[1],sub2[2],0)) + try: + date.set(gen.lib.Date.QUAL_NONE,mod, cal1, + (sub1[0],sub1[1],sub1[2],0,sub2[0],sub2[1],sub2[2],0)) + except gen.lib.DateError as e: + # TRANSLATORS: leave the {date} and {gw_snippet} untranslated + # in the format string, but you may re-order them if needed. + LOG.warning(_( + "Invalid date {date} in {gw_snippet}, " + "preserving date as text." + ).format(date=e.date.dateval, gw_snippet=field)) + date.set(modifier=gen.lib.Date.MOD_TEXTONLY, text=field) return date else: return None diff --git a/src/plugins/import/test/ImportGeneWeb_test.py b/src/plugins/import/test/ImportGeneWeb_test.py new file mode 100644 index 000000000..32bb04d56 --- /dev/null +++ b/src/plugins/import/test/ImportGeneWeb_test.py @@ -0,0 +1,49 @@ +# +# Gramps - a GTK+/GNOME based genealogy program +# +# Copyright (C) 2013 Vassilii Khachaturov +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +""" +Unit test of the GeneWebParser +""" +from __future__ import print_function +import unittest +import sys +import os +sys.path.append(os.curdir) +sys.path.append(os.path.join(os.curdir, 'plugins', 'import')) + +from ImportGeneWeb import GeneWebParser +from gen.lib.date import Date + +class ParseDateTest(unittest.TestCase): + def setUp(self): + self.parse = GeneWebParser(dbase=None, file=None).parse_date + + def test_regular_date_parsed_dmy(self): + d = self.parse("28/2/1876") + self.assertEqual(d.get_ymd(), (1876, 2, 28)) + + def test_invalid_date_stays_verbatim_text(self): + text = "29/2/1875" + d = self.parse(text) + self.assertEqual(d.get_modifier(), Date.MOD_TEXTONLY) + self.assertEqual(d.get_text(), text) + +if __name__ == "__main__": + unittest.main()