2006-05-02 02:41:26 +05:30
|
|
|
# -*- coding: utf-8 -*-
|
2004-11-11 10:37:41 +05:30
|
|
|
#
|
2004-09-17 09:00:04 +05:30
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2006-02-04 03:33:53 +05:30
|
|
|
# Copyright (C) 2004-2006 Donald N. Allingham
|
2004-09-17 09:00:04 +05:30
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
|
2004-09-28 01:45:38 +05:30
|
|
|
# $Id$
|
|
|
|
|
2004-09-17 09:00:04 +05:30
|
|
|
"""
|
2004-12-06 09:43:13 +05:30
|
|
|
Date parsing class. Serves as the base class for any localized
|
|
|
|
date parsing class. The default, base class provides parsing for
|
|
|
|
English.
|
2004-09-17 09:00:04 +05:30
|
|
|
"""
|
|
|
|
|
2004-09-28 01:45:38 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Python modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2004-09-17 09:00:04 +05:30
|
|
|
import re
|
2004-11-15 07:37:21 +05:30
|
|
|
import calendar
|
2004-09-18 05:22:09 +05:30
|
|
|
|
2004-09-28 01:45:38 +05:30
|
|
|
#-------------------------------------------------------------------------
|
2006-03-05 10:15:44 +05:30
|
|
|
#
|
|
|
|
# set up logging
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(".DateParser")
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
2004-09-28 01:45:38 +05:30
|
|
|
#
|
|
|
|
# GRAMPS modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2007-10-08 22:11:39 +05:30
|
|
|
from gen.lib import Date, DateError
|
2005-12-30 09:27:31 +05:30
|
|
|
import GrampsLocale
|
2006-02-04 03:33:53 +05:30
|
|
|
|
2004-11-10 07:25:30 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Top-level module functions
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2004-11-15 07:37:21 +05:30
|
|
|
_max_days = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
|
|
|
|
_leap_days = [ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
|
2004-11-10 09:50:47 +05:30
|
|
|
|
2004-11-10 07:25:30 +05:30
|
|
|
def gregorian_valid(date_tuple):
|
2009-02-09 16:17:03 +05:30
|
|
|
""" Checks if date_tuple is a valid date in Gregorian Calendar """
|
2004-11-10 07:25:30 +05:30
|
|
|
day = date_tuple[0]
|
|
|
|
month = date_tuple[1]
|
|
|
|
valid = True
|
2004-11-10 09:50:47 +05:30
|
|
|
try:
|
|
|
|
if month > 12:
|
|
|
|
valid = False
|
2004-11-15 07:37:21 +05:30
|
|
|
elif calendar.isleap(date_tuple[2]):
|
|
|
|
if day > _leap_days[month-1]:
|
|
|
|
valid = False
|
2004-11-10 09:50:47 +05:30
|
|
|
elif day > _max_days[month-1]:
|
|
|
|
valid = False
|
|
|
|
except:
|
2004-11-10 07:25:30 +05:30
|
|
|
valid = False
|
|
|
|
return valid
|
|
|
|
|
2009-01-24 14:11:14 +05:30
|
|
|
def julian_valid(date_tuple):
|
2009-02-09 16:17:03 +05:30
|
|
|
""" Checks if date_tuple is a valid date in Julian Calendar """
|
2009-01-24 14:11:14 +05:30
|
|
|
day = date_tuple[0]
|
|
|
|
month = date_tuple[1]
|
|
|
|
valid = True
|
|
|
|
try:
|
|
|
|
if month > 12:
|
|
|
|
valid = False
|
|
|
|
elif (date_tuple[2]) % 4 == 0:
|
|
|
|
# julian always have leapyear every 4 year
|
|
|
|
if day > _leap_days[month-1]:
|
|
|
|
valid = False
|
|
|
|
elif day > _max_days[month-1]:
|
|
|
|
valid = False
|
|
|
|
except:
|
|
|
|
valid = False
|
|
|
|
return valid
|
|
|
|
|
2009-01-20 01:39:19 +05:30
|
|
|
def swedish_valid(date_tuple):
|
2009-02-09 16:17:03 +05:30
|
|
|
""" Checks if date_tuple is a valid date in Swedish Calendar """
|
2009-01-20 01:39:19 +05:30
|
|
|
valid = gregorian_valid(date_tuple)
|
|
|
|
# not sure how <= and >= works with tuples???
|
|
|
|
date_tuple = (date_tuple[2], date_tuple[1], date_tuple[0])
|
2009-02-09 16:17:03 +05:30
|
|
|
if date_tuple <= (1700, 2, 28):
|
2009-01-20 01:39:19 +05:30
|
|
|
valid = False
|
2009-02-09 16:17:03 +05:30
|
|
|
if date_tuple == (1700, 2, 29): # leapday 1700 was skipped
|
2009-01-20 01:39:19 +05:30
|
|
|
valid = False
|
2009-02-09 16:17:03 +05:30
|
|
|
if date_tuple == (1712, 2, 30): # extra day was inserted 1712
|
2009-01-20 01:39:19 +05:30
|
|
|
valid = True
|
2009-02-09 16:17:03 +05:30
|
|
|
if date_tuple >= (1712, 3, 1): # back to julian
|
2009-01-22 22:37:52 +05:30
|
|
|
valid = False
|
2009-01-20 01:39:19 +05:30
|
|
|
return valid
|
|
|
|
|
2009-01-22 15:45:01 +05:30
|
|
|
def french_valid(date_tuple):
|
2009-02-09 16:17:03 +05:30
|
|
|
""" Checks if date_tuple is a valid date in French Calendar """
|
2009-01-22 15:45:01 +05:30
|
|
|
valid = True
|
|
|
|
# year 1 starts on 22.9.1792
|
|
|
|
if date_tuple[2] < 1:
|
|
|
|
valid = False
|
|
|
|
return valid
|
|
|
|
|
2004-09-28 01:45:38 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Parser class
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2009-05-21 22:49:50 +05:30
|
|
|
class DateParser(object):
|
2004-09-17 09:00:04 +05:30
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Convert a text string into a Date object. If the date cannot be
|
2004-09-17 09:00:04 +05:30
|
|
|
converted, the text string is assigned.
|
|
|
|
"""
|
2004-09-18 05:22:09 +05:30
|
|
|
|
2004-10-12 06:59:39 +05:30
|
|
|
_fmt_parse = re.compile(".*%(\S).*%(\S).*%(\S).*")
|
2004-09-18 05:22:09 +05:30
|
|
|
|
2004-09-27 10:25:39 +05:30
|
|
|
# RFC-2822 only uses capitalized English abbreviated names, no locales.
|
2007-06-27 10:20:33 +05:30
|
|
|
_rfc_days = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat')
|
2004-09-27 10:25:39 +05:30
|
|
|
_rfc_mons_to_int = {
|
2004-11-15 07:37:21 +05:30
|
|
|
'Jan' : 1, 'Feb' : 2, 'Mar' : 3, 'Apr' : 4,
|
|
|
|
'May' : 5, 'Jun' : 6, 'Jul' : 7, 'Aug' : 8,
|
|
|
|
'Sep' : 9, 'Oct' : 10, 'Nov' : 11, 'Dec' : 12,
|
2004-09-27 10:25:39 +05:30
|
|
|
}
|
2004-09-27 09:50:43 +05:30
|
|
|
|
2005-12-30 09:27:31 +05:30
|
|
|
month_to_int = GrampsLocale.month_to_int
|
2004-09-17 09:00:04 +05:30
|
|
|
|
2005-12-06 12:08:09 +05:30
|
|
|
# modifiers before the date
|
2004-09-17 09:00:04 +05:30
|
|
|
modifier_to_int = {
|
2004-09-24 03:02:07 +05:30
|
|
|
'before' : Date.MOD_BEFORE, 'bef' : Date.MOD_BEFORE,
|
|
|
|
'bef.' : Date.MOD_BEFORE, 'after' : Date.MOD_AFTER,
|
|
|
|
'aft' : Date.MOD_AFTER, 'aft.' : Date.MOD_AFTER,
|
|
|
|
'about' : Date.MOD_ABOUT, 'abt.' : Date.MOD_ABOUT,
|
|
|
|
'abt' : Date.MOD_ABOUT, 'circa' : Date.MOD_ABOUT,
|
|
|
|
'c.' : Date.MOD_ABOUT, 'around' : Date.MOD_ABOUT,
|
|
|
|
}
|
2005-12-06 12:08:09 +05:30
|
|
|
# in some languages some of above listed modifiers are after the date,
|
|
|
|
# in that case the subclass should put them into this dictionary instead
|
|
|
|
modifier_after_to_int = {}
|
2004-09-24 03:02:07 +05:30
|
|
|
|
|
|
|
hebrew_to_int = {
|
2010-03-24 21:35:42 +05:30
|
|
|
u"tishri" : 1, u"heshvan" : 2, u"kislev" : 3,
|
|
|
|
u"tevet" : 4, u"shevat" : 5, u"adari" : 6,
|
|
|
|
u"adarii" : 7, u"nisan" : 8, u"iyyar" : 9,
|
|
|
|
u"sivan" : 10, u"tammuz" : 11, u"av" : 12,
|
|
|
|
u"elul" : 13,
|
|
|
|
#alternative spelling
|
|
|
|
u"cheshvan": 2, u"adar sheni": 7, u"iyar" : 9,
|
|
|
|
#GEDCOM months
|
|
|
|
u"tsh" : 1, u"csh": 5, u"ksl": 3, u"tvt": 4, u"shv": 5, u"adr": 6,
|
|
|
|
u"ads" : 7, u"nsn": 8, u"iyr": 9, u"svn":10, u"tmz":11, u"aav":12,
|
|
|
|
u"ell":13,
|
2004-09-24 03:02:07 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
french_to_int = {
|
2006-05-02 02:41:26 +05:30
|
|
|
u'vendémiaire' : 1, u'brumaire' : 2,
|
|
|
|
u'frimaire' : 3, u'nivôse': 4,
|
|
|
|
u'pluviôse' : 5, u'ventôse' : 6,
|
|
|
|
u'germinal' : 7, u'floréal' : 8,
|
|
|
|
u'prairial' : 9, u'messidor' : 10,
|
|
|
|
u'thermidor' : 11, u'fructidor' : 12,
|
2010-03-24 21:35:42 +05:30
|
|
|
u'extra' : 13,
|
|
|
|
#GEDCOM months
|
|
|
|
u'vend' : 1, u'brum' : 2,
|
|
|
|
u'frim' : 3, u'nivo' : 4,
|
|
|
|
u'pluv' : 5, u'vent' : 6,
|
|
|
|
u'germ' : 7, u'flor' : 8,
|
|
|
|
u'prai' : 9, u'mess' : 10,
|
|
|
|
u'ther' : 11, u'fruc' : 12,
|
|
|
|
u'comp' : 13,
|
2004-09-24 03:02:07 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
islamic_to_int = {
|
|
|
|
"muharram" : 1, "muharram ul haram" : 1,
|
|
|
|
"safar" : 2, "rabi`al-awwal" : 3,
|
|
|
|
"rabi'l" : 3, "rabi`ul-akhir" : 4,
|
|
|
|
"rabi`ath-thani" : 4, "rabi` ath-thani" : 4,
|
|
|
|
"rabi`al-thaany" : 4, "rabi` al-thaany" : 4,
|
|
|
|
"rabi' ii" : 4, "jumada l-ula" : 5,
|
|
|
|
"jumaada-ul-awwal" : 5, "jumaada i" : 5,
|
|
|
|
"jumada t-tania" : 6, "jumaada-ul-akhir" : 6,
|
|
|
|
"jumaada al-thaany" : 6, "jumaada ii" : 5,
|
|
|
|
"rajab" : 7, "sha`ban" : 8,
|
|
|
|
"sha`aban" : 8, "ramadan" : 9,
|
|
|
|
"ramadhan" : 9, "shawwal" : 10,
|
|
|
|
"dhu l-qa`da" : 11, "dhu qadah" : 11,
|
|
|
|
"thw al-qi`dah" : 11, "dhu l-hijja" : 12,
|
|
|
|
"dhu hijja" : 12, "thw al-hijjah" : 12,
|
|
|
|
}
|
|
|
|
|
|
|
|
persian_to_int = {
|
2006-05-02 02:41:26 +05:30
|
|
|
"farvardin" : 1, "ordibehesht" : 2,
|
|
|
|
"khordad" : 3, "tir" : 4,
|
|
|
|
"mordad" : 5, "shahrivar" : 6,
|
|
|
|
"mehr" : 7, "aban" : 8,
|
|
|
|
"azar" : 9, "dey" : 10,
|
|
|
|
"bahman" : 11, "esfand" : 12,
|
2004-09-24 03:02:07 +05:30
|
|
|
}
|
|
|
|
|
2009-01-20 01:39:19 +05:30
|
|
|
swedish_to_int = {
|
|
|
|
"januari" : 1, "februari" : 2,
|
|
|
|
"mars" : 3, "april" : 4,
|
|
|
|
"maj" : 5, "juni" : 6,
|
|
|
|
"juli" : 7, "augisti" : 8,
|
|
|
|
"september" : 9, "oktober" : 10,
|
|
|
|
"november" : 11, "december" : 12,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-02 02:41:26 +05:30
|
|
|
bce = ["B.C.E.", "B.C.E", "BCE", "B.C.", "B.C", "BC" ]
|
2004-09-24 03:02:07 +05:30
|
|
|
|
|
|
|
calendar_to_int = {
|
|
|
|
'gregorian' : Date.CAL_GREGORIAN,
|
|
|
|
'g' : Date.CAL_GREGORIAN,
|
|
|
|
'julian' : Date.CAL_JULIAN,
|
|
|
|
'j' : Date.CAL_JULIAN,
|
|
|
|
'hebrew' : Date.CAL_HEBREW,
|
|
|
|
'h' : Date.CAL_HEBREW,
|
|
|
|
'islamic' : Date.CAL_ISLAMIC,
|
|
|
|
'i' : Date.CAL_ISLAMIC,
|
|
|
|
'french' : Date.CAL_FRENCH,
|
|
|
|
'french republican': Date.CAL_FRENCH,
|
|
|
|
'f' : Date.CAL_FRENCH,
|
|
|
|
'persian' : Date.CAL_PERSIAN,
|
2009-01-20 01:39:19 +05:30
|
|
|
'p' : Date.CAL_PERSIAN,
|
|
|
|
'swedish' : Date.CAL_SWEDISH,
|
|
|
|
's' : Date.CAL_SWEDISH,
|
2004-09-17 09:00:04 +05:30
|
|
|
}
|
2009-01-17 20:03:10 +05:30
|
|
|
|
|
|
|
newyear_to_int = {
|
2009-01-17 20:52:28 +05:30
|
|
|
"jan1": Date.NEWYEAR_JAN1,
|
2009-01-17 20:03:10 +05:30
|
|
|
"mar1": Date.NEWYEAR_MAR1,
|
|
|
|
"mar25": Date.NEWYEAR_MAR25,
|
|
|
|
"sep1" : Date.NEWYEAR_SEP1,
|
|
|
|
}
|
2004-09-17 09:00:04 +05:30
|
|
|
|
|
|
|
quality_to_int = {
|
|
|
|
'estimated' : Date.QUAL_ESTIMATED,
|
|
|
|
'est.' : Date.QUAL_ESTIMATED,
|
|
|
|
'est' : Date.QUAL_ESTIMATED,
|
|
|
|
'calc.' : Date.QUAL_CALCULATED,
|
|
|
|
'calc' : Date.QUAL_CALCULATED,
|
|
|
|
'calculated' : Date.QUAL_CALCULATED,
|
|
|
|
}
|
|
|
|
|
2004-11-11 10:37:41 +05:30
|
|
|
def __init__(self):
|
|
|
|
self.init_strings()
|
|
|
|
self.parser = {
|
2009-01-24 14:11:14 +05:30
|
|
|
Date.CAL_GREGORIAN : self._parse_gregorian,
|
|
|
|
Date.CAL_JULIAN : self._parse_julian,
|
2005-12-06 12:08:09 +05:30
|
|
|
Date.CAL_FRENCH : self._parse_french,
|
2004-11-11 10:37:41 +05:30
|
|
|
Date.CAL_PERSIAN : self._parse_persian,
|
|
|
|
Date.CAL_HEBREW : self._parse_hebrew,
|
|
|
|
Date.CAL_ISLAMIC : self._parse_islamic,
|
2009-01-20 01:39:19 +05:30
|
|
|
Date.CAL_SWEDISH : self._parse_swedish,
|
2004-11-11 10:37:41 +05:30
|
|
|
}
|
2004-10-16 10:40:35 +05:30
|
|
|
|
2005-12-30 09:27:31 +05:30
|
|
|
fmt = GrampsLocale.tformat
|
2004-11-11 10:37:41 +05:30
|
|
|
match = self._fmt_parse.match(fmt.lower())
|
|
|
|
if match:
|
2008-07-14 17:44:43 +05:30
|
|
|
self.dmy = (match.groups() == ('d', 'm', 'y') or \
|
|
|
|
match.groups() == ('d', 'b', 'y'))
|
|
|
|
self.ymd = (match.groups() == ('y', 'm', 'd') or \
|
|
|
|
match.groups() == ('y', 'b', 'd'))
|
2004-11-11 10:37:41 +05:30
|
|
|
else:
|
|
|
|
self.dmy = True
|
2006-05-02 02:41:26 +05:30
|
|
|
self.ymd = False
|
|
|
|
|
|
|
|
def re_longest_first(self, keys):
|
2008-01-06 01:40:26 +05:30
|
|
|
"""
|
|
|
|
returns a string for a RE group which contains the given keys
|
2006-05-02 02:41:26 +05:30
|
|
|
sorted so that longest keys match first. Any '.' characters
|
2008-01-06 01:40:26 +05:30
|
|
|
are quoted.
|
|
|
|
"""
|
|
|
|
keys.sort(lambda x, y: cmp(len(y), len(x)))
|
|
|
|
return '(' + '|'.join([key.replace('.', '\.') for key in keys]) + ')'
|
|
|
|
|
2004-11-11 10:37:41 +05:30
|
|
|
def init_strings(self):
|
|
|
|
"""
|
|
|
|
This method compiles regular expression strings for matching dates.
|
|
|
|
|
|
|
|
Most of the re's in most languages can stay as is. span and range
|
|
|
|
most likely will need to change. Whatever change is done, this method
|
|
|
|
may be called first as DateParser.init_strings(self) so that the
|
|
|
|
invariant expresions don't need to be repeteadly coded. All differences
|
|
|
|
can be coded after DateParser.init_strings(self) call, that way they
|
|
|
|
override stuff from this method. See DateParserRU() as an example.
|
|
|
|
"""
|
|
|
|
self._rfc_mon_str = '(' + '|'.join(self._rfc_mons_to_int.keys()) + ')'
|
|
|
|
self._rfc_day_str = '(' + '|'.join(self._rfc_days) + ')'
|
|
|
|
|
2006-05-02 02:41:26 +05:30
|
|
|
self._bce_str = self.re_longest_first(self.bce)
|
|
|
|
self._qual_str = self.re_longest_first(self.quality_to_int.keys())
|
|
|
|
self._mod_str = self.re_longest_first(self.modifier_to_int.keys())
|
|
|
|
self._mod_after_str = self.re_longest_first(
|
|
|
|
self.modifier_after_to_int.keys())
|
|
|
|
|
|
|
|
self._mon_str = self.re_longest_first(self.month_to_int.keys())
|
|
|
|
self._jmon_str = self.re_longest_first(self.hebrew_to_int.keys())
|
|
|
|
self._fmon_str = self.re_longest_first(self.french_to_int.keys())
|
|
|
|
self._pmon_str = self.re_longest_first(self.persian_to_int.keys())
|
|
|
|
self._imon_str = self.re_longest_first(self.islamic_to_int.keys())
|
2009-01-20 01:39:19 +05:30
|
|
|
self._smon_str = self.re_longest_first(self.swedish_to_int.keys())
|
2006-05-02 02:41:26 +05:30
|
|
|
self._cal_str = self.re_longest_first(self.calendar_to_int.keys())
|
2010-04-04 05:03:17 +05:30
|
|
|
self._ny_str = self.re_longest_first(self.newyear_to_int.keys())
|
2006-05-02 02:41:26 +05:30
|
|
|
|
|
|
|
# bce, calendar type and quality may be either at the end or at
|
|
|
|
# the beginning of the given date string, therefore they will
|
|
|
|
# be parsed from the middle and will be in match.group(2).
|
2010-04-04 05:03:17 +05:30
|
|
|
self._bce_re = re.compile("(.*)\s+%s( ?.*)" % self._bce_str)
|
|
|
|
|
|
|
|
self._cal = re.compile("(.*)\s+\(%s\)( ?.*)" % self._cal_str,
|
|
|
|
re.IGNORECASE)
|
|
|
|
self._calny = re.compile("(.*)\s+\(%s,\s*%s\)( ?.*)" % (self._cal_str,
|
|
|
|
self._ny_str),
|
|
|
|
re.IGNORECASE)
|
|
|
|
self._calny_iso = re.compile("(.*)\s+\(%s,\s*(\d{1,2}-\d{1,2})\)( ?.*)" % self._cal_str,
|
|
|
|
re.IGNORECASE)
|
|
|
|
|
|
|
|
self._ny = re.compile("(.*)\s+\(%s\)( ?.*)" % self._ny_str,
|
|
|
|
re.IGNORECASE)
|
|
|
|
self._ny_iso = re.compile("(.*)\s+\((\d{1,2}-\d{1,2})\)( ?.*)")
|
2006-05-02 02:41:26 +05:30
|
|
|
|
|
|
|
self._qual = re.compile("(.* ?)%s\s+(.+)" % self._qual_str,
|
2007-06-27 10:20:33 +05:30
|
|
|
re.IGNORECASE)
|
|
|
|
|
2005-06-05 09:31:56 +05:30
|
|
|
self._span = re.compile("(from)\s+(?P<start>.+)\s+to\s+(?P<stop>.+)",
|
2007-06-27 10:20:33 +05:30
|
|
|
re.IGNORECASE)
|
2005-06-05 09:31:56 +05:30
|
|
|
self._range = re.compile("(bet|bet.|between)\s+(?P<start>.+)\s+and\s+(?P<stop>.+)",
|
2007-06-27 10:20:33 +05:30
|
|
|
re.IGNORECASE)
|
2004-11-11 10:37:41 +05:30
|
|
|
self._modifier = re.compile('%s\s+(.*)' % self._mod_str,
|
2007-06-27 10:20:33 +05:30
|
|
|
re.IGNORECASE)
|
2005-12-06 12:08:09 +05:30
|
|
|
self._modifier_after = re.compile('(.*)\s+%s' % self._mod_after_str,
|
2007-06-27 10:20:33 +05:30
|
|
|
re.IGNORECASE)
|
|
|
|
self._abt2 = re.compile('<(.*)>', re.IGNORECASE)
|
2008-05-28 10:06:10 +05:30
|
|
|
self._text = re.compile('%s\.?\s+(\d+)?\s*,?\s*((\d+)(/\d+)?)?\s*$' % self._mon_str,
|
2007-06-27 10:20:33 +05:30
|
|
|
re.IGNORECASE)
|
2008-05-28 10:06:10 +05:30
|
|
|
self._text2 = re.compile('(\d+)?\s+?%s\.?\s*((\d+)(/\d+)?)?\s*$' % self._mon_str,
|
2007-06-27 10:20:33 +05:30
|
|
|
re.IGNORECASE)
|
2005-12-06 12:08:09 +05:30
|
|
|
self._jtext = re.compile('%s\s+(\d+)?\s*,?\s*((\d+)(/\d+)?)?\s*$' % self._jmon_str,
|
2007-06-27 10:20:33 +05:30
|
|
|
re.IGNORECASE)
|
2005-12-06 12:08:09 +05:30
|
|
|
self._jtext2 = re.compile('(\d+)?\s+?%s\s*((\d+)(/\d+)?)?\s*$' % self._jmon_str,
|
2007-06-27 10:20:33 +05:30
|
|
|
re.IGNORECASE)
|
2005-12-06 12:08:09 +05:30
|
|
|
self._ftext = re.compile('%s\s+(\d+)?\s*,?\s*((\d+)(/\d+)?)?\s*$' % self._fmon_str,
|
2007-06-27 10:20:33 +05:30
|
|
|
re.IGNORECASE)
|
2005-12-06 12:08:09 +05:30
|
|
|
self._ftext2 = re.compile('(\d+)?\s+?%s\s*((\d+)(/\d+)?)?\s*$' % self._fmon_str,
|
2007-06-27 10:20:33 +05:30
|
|
|
re.IGNORECASE)
|
2005-12-06 12:08:09 +05:30
|
|
|
self._ptext = re.compile('%s\s+(\d+)?\s*,?\s*((\d+)(/\d+)?)?\s*$' % self._pmon_str,
|
2007-06-27 10:20:33 +05:30
|
|
|
re.IGNORECASE)
|
2005-12-06 12:08:09 +05:30
|
|
|
self._ptext2 = re.compile('(\d+)?\s+?%s\s*((\d+)(/\d+)?)?\s*$' % self._pmon_str,
|
2007-06-27 10:20:33 +05:30
|
|
|
re.IGNORECASE)
|
2005-12-06 12:08:09 +05:30
|
|
|
self._itext = re.compile('%s\s+(\d+)?\s*,?\s*((\d+)(/\d+)?)?\s*$' % self._imon_str,
|
2007-06-27 10:20:33 +05:30
|
|
|
re.IGNORECASE)
|
2005-12-06 12:08:09 +05:30
|
|
|
self._itext2 = re.compile('(\d+)?\s+?%s\s*((\d+)(/\d+)?)?\s*$' % self._imon_str,
|
2007-06-27 10:20:33 +05:30
|
|
|
re.IGNORECASE)
|
2009-01-20 01:39:19 +05:30
|
|
|
self._stext = re.compile('%s\.?\s+(\d+)?\s*,?\s*((\d+)(/\d+)?)?\s*$' % self._smon_str,
|
|
|
|
re.IGNORECASE)
|
|
|
|
self._stext2 = re.compile('(\d+)?\s+?%s\.?\s*((\d+)(/\d+)?)?\s*$' % self._smon_str,
|
|
|
|
re.IGNORECASE)
|
2007-02-05 03:10:27 +05:30
|
|
|
self._numeric = re.compile("((\d+)[/\.]\s*)?((\d+)[/\.]\s*)?(\d+)\s*$")
|
2006-05-02 02:41:26 +05:30
|
|
|
self._iso = re.compile("(\d+)(/(\d+))?-(\d+)-(\d+)\s*$")
|
2010-05-21 16:45:08 +05:30
|
|
|
self._isotimestamp = re.compile("^\s*?(\d{4})([01]\d)([0123]\d)(?:(?:[012]\d[0-5]\d[0-5]\d)|(?:\s+[012]\d:[0-5]\d(?::[0-5]\d)?))?\s*?$")
|
2004-11-11 10:37:41 +05:30
|
|
|
self._rfc = re.compile("(%s,)?\s+(\d|\d\d)\s+%s\s+(\d+)\s+\d\d:\d\d(:\d\d)?\s+(\+|-)\d\d\d\d"
|
2007-06-27 10:20:33 +05:30
|
|
|
% (self._rfc_day_str, self._rfc_mon_str))
|
2004-09-24 03:02:07 +05:30
|
|
|
|
2007-06-27 10:20:33 +05:30
|
|
|
def _get_int(self, val):
|
2004-09-17 09:00:04 +05:30
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Convert the string to an integer if the value is not None. If the
|
2004-09-17 09:00:04 +05:30
|
|
|
value is None, a zero is returned
|
|
|
|
"""
|
2008-06-16 20:31:46 +05:30
|
|
|
if val is None:
|
2004-09-17 09:00:04 +05:30
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
return int(val)
|
|
|
|
|
2007-06-27 10:20:33 +05:30
|
|
|
def _parse_hebrew(self, text):
|
|
|
|
return self._parse_calendar(text, self._jtext, self._jtext2,
|
2004-09-24 03:02:07 +05:30
|
|
|
self.hebrew_to_int)
|
|
|
|
|
2007-06-27 10:20:33 +05:30
|
|
|
def _parse_islamic(self, text):
|
|
|
|
return self._parse_calendar(text, self._itext, self._itext2,
|
2004-09-24 03:02:07 +05:30
|
|
|
self.islamic_to_int)
|
2004-09-18 05:22:09 +05:30
|
|
|
|
2007-06-27 10:20:33 +05:30
|
|
|
def _parse_persian(self, text):
|
|
|
|
return self._parse_calendar(text, self._ptext, self._ptext2,
|
2004-09-24 03:02:07 +05:30
|
|
|
self.persian_to_int)
|
|
|
|
|
2007-06-27 10:20:33 +05:30
|
|
|
def _parse_french(self, text):
|
|
|
|
return self._parse_calendar(text, self._ftext, self._ftext2,
|
2009-01-22 15:45:01 +05:30
|
|
|
self.french_to_int, french_valid)
|
2004-09-24 03:02:07 +05:30
|
|
|
|
2009-01-24 14:11:14 +05:30
|
|
|
def _parse_gregorian(self, text):
|
2007-06-27 10:20:33 +05:30
|
|
|
return self._parse_calendar(text, self._text, self._text2,
|
|
|
|
self.month_to_int, gregorian_valid)
|
2009-01-24 14:11:14 +05:30
|
|
|
|
|
|
|
def _parse_julian(self, text):
|
|
|
|
return self._parse_calendar(text, self._text, self._text2,
|
|
|
|
self.month_to_int, julian_valid)
|
|
|
|
|
2009-01-20 01:39:19 +05:30
|
|
|
def _parse_swedish(self, text):
|
|
|
|
return self._parse_calendar(text, self._stext, self._stext2,
|
|
|
|
self.swedish_to_int,swedish_valid)
|
|
|
|
|
2004-09-24 03:02:07 +05:30
|
|
|
|
2007-06-27 10:20:33 +05:30
|
|
|
def _parse_calendar(self, text, regex1, regex2, mmap, check=None):
|
2004-11-17 09:06:18 +05:30
|
|
|
match = regex1.match(text.lower())
|
2004-09-17 09:00:04 +05:30
|
|
|
if match:
|
|
|
|
groups = match.groups()
|
2008-06-16 20:31:46 +05:30
|
|
|
if groups[0] is None:
|
2004-09-17 09:00:04 +05:30
|
|
|
m = 0
|
|
|
|
else:
|
2004-09-24 03:02:07 +05:30
|
|
|
m = mmap[groups[0].lower()]
|
2004-09-17 09:00:04 +05:30
|
|
|
|
2008-06-16 20:31:46 +05:30
|
|
|
if groups[2] is None:
|
2004-10-22 09:37:02 +05:30
|
|
|
y = self._get_int(groups[1])
|
|
|
|
d = 0
|
2006-05-02 02:41:26 +05:30
|
|
|
s = False
|
2004-09-17 09:00:04 +05:30
|
|
|
else:
|
2004-10-22 09:37:02 +05:30
|
|
|
d = self._get_int(groups[1])
|
2008-06-16 20:31:46 +05:30
|
|
|
if groups[4] is not None: # slash year "/80"
|
2008-04-19 10:21:12 +05:30
|
|
|
y = int(groups[3]) + 1 # fullyear + 1
|
2008-04-19 09:09:58 +05:30
|
|
|
s = True
|
|
|
|
else: # regular, non-slash date
|
|
|
|
y = int(groups[3])
|
|
|
|
s = False
|
2007-06-27 10:20:33 +05:30
|
|
|
value = (d, m, y, s)
|
|
|
|
if check and not check((d, m, y)):
|
2004-11-15 07:37:21 +05:30
|
|
|
value = Date.EMPTY
|
|
|
|
return value
|
2004-09-17 09:00:04 +05:30
|
|
|
|
2004-11-17 09:06:18 +05:30
|
|
|
match = regex2.match(text.lower())
|
2004-09-17 09:00:04 +05:30
|
|
|
if match:
|
|
|
|
groups = match.groups()
|
2008-06-16 20:31:46 +05:30
|
|
|
if groups[1] is None:
|
2004-09-17 09:00:04 +05:30
|
|
|
m = 0
|
|
|
|
else:
|
2004-09-24 03:02:07 +05:30
|
|
|
m = mmap[groups[1].lower()]
|
2004-09-17 09:00:04 +05:30
|
|
|
d = self._get_int(groups[0])
|
|
|
|
|
2008-06-16 20:31:46 +05:30
|
|
|
if groups[2] is None:
|
2006-05-02 02:41:26 +05:30
|
|
|
y = None
|
|
|
|
s = False
|
2004-09-17 09:00:04 +05:30
|
|
|
else:
|
2008-06-16 20:31:46 +05:30
|
|
|
if groups[4] is not None: # slash year digit
|
2008-04-19 10:21:12 +05:30
|
|
|
y = int(groups[3]) + 1 # fullyear + 1
|
2008-04-19 09:09:58 +05:30
|
|
|
s = True
|
|
|
|
else:
|
|
|
|
y = int(groups[3])
|
|
|
|
s = False
|
2007-06-27 10:20:33 +05:30
|
|
|
value = (d, m, y, s)
|
|
|
|
if check and not check((d, m, y)):
|
2004-11-17 02:15:25 +05:30
|
|
|
value = Date.EMPTY
|
2004-11-15 07:37:21 +05:30
|
|
|
return value
|
|
|
|
|
2004-09-24 03:02:07 +05:30
|
|
|
return Date.EMPTY
|
|
|
|
|
2007-06-27 10:20:33 +05:30
|
|
|
def _parse_subdate(self, text, subparser=None):
|
2004-09-24 03:02:07 +05:30
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Convert only the date portion of a date.
|
2004-09-24 03:02:07 +05:30
|
|
|
"""
|
2008-06-16 20:31:46 +05:30
|
|
|
if subparser is None:
|
2009-01-24 14:11:14 +05:30
|
|
|
subparser = self._parse_gregorian
|
2005-12-06 12:08:09 +05:30
|
|
|
|
2009-01-24 14:11:14 +05:30
|
|
|
if subparser == self._parse_gregorian:
|
2004-11-15 07:37:21 +05:30
|
|
|
check = gregorian_valid
|
2009-01-24 14:11:14 +05:30
|
|
|
|
|
|
|
elif subparser == self._parse_julian:
|
|
|
|
check = julian_valid
|
|
|
|
|
2009-01-23 01:16:41 +05:30
|
|
|
elif subparser == self._parse_swedish:
|
2009-01-20 01:39:19 +05:30
|
|
|
check = swedish_valid
|
2009-01-24 14:11:14 +05:30
|
|
|
|
2009-01-23 01:16:41 +05:30
|
|
|
elif subparser == self._parse_french:
|
2009-01-22 15:45:01 +05:30
|
|
|
check = french_valid
|
2004-11-15 07:37:21 +05:30
|
|
|
else:
|
|
|
|
check = None
|
2009-01-24 14:11:14 +05:30
|
|
|
|
2004-09-24 03:02:07 +05:30
|
|
|
value = subparser(text)
|
|
|
|
if value != Date.EMPTY:
|
|
|
|
return value
|
2004-09-17 09:00:04 +05:30
|
|
|
match = self._iso.match(text)
|
|
|
|
if match:
|
|
|
|
groups = match.groups()
|
|
|
|
y = self._get_int(groups[0])
|
2006-05-02 02:41:26 +05:30
|
|
|
m = self._get_int(groups[3])
|
|
|
|
d = self._get_int(groups[4])
|
2007-06-27 10:20:33 +05:30
|
|
|
if check and not check((d, m, y)):
|
2004-10-11 04:52:12 +05:30
|
|
|
return Date.EMPTY
|
2008-04-19 09:09:58 +05:30
|
|
|
if groups[2]: # slash year digit
|
|
|
|
return (d, m, y + 1, True)
|
2006-05-02 02:41:26 +05:30
|
|
|
else:
|
2007-06-27 10:20:33 +05:30
|
|
|
return (d, m, y, False)
|
2004-09-17 09:00:04 +05:30
|
|
|
|
2010-05-21 16:45:08 +05:30
|
|
|
# Database datetime format, used in ex. MSSQL
|
|
|
|
# YYYYMMDD HH:MM:SS or YYYYMMDD or YYYYMMDDHHMMSS
|
|
|
|
match = self._isotimestamp.match(text)
|
|
|
|
if match:
|
|
|
|
groups = match.groups()
|
|
|
|
y = self._get_int(groups[0])
|
|
|
|
m = self._get_int(groups[1])
|
|
|
|
d = self._get_int(groups[2])
|
|
|
|
value = (d, m, y, False)
|
|
|
|
if not check((d, m, y)):
|
|
|
|
value = Date.Empty
|
|
|
|
return value
|
|
|
|
|
2004-09-27 09:50:43 +05:30
|
|
|
match = self._rfc.match(text)
|
|
|
|
if match:
|
|
|
|
groups = match.groups()
|
2004-09-28 01:45:38 +05:30
|
|
|
d = self._get_int(groups[2])
|
|
|
|
m = self._rfc_mons_to_int[groups[3]]
|
|
|
|
y = self._get_int(groups[4])
|
2007-06-27 10:20:33 +05:30
|
|
|
value = (d, m, y, False)
|
|
|
|
if check and not check((d, m, y)):
|
2006-05-02 02:41:26 +05:30
|
|
|
value = Date.EMPTY
|
|
|
|
return value
|
2004-09-27 09:50:43 +05:30
|
|
|
|
2004-09-17 09:00:04 +05:30
|
|
|
match = self._numeric.match(text)
|
|
|
|
if match:
|
|
|
|
groups = match.groups()
|
2006-05-02 02:41:26 +05:30
|
|
|
if self.ymd:
|
|
|
|
# '1789' and ymd: incomplete date
|
2008-06-16 20:31:46 +05:30
|
|
|
if groups[1] is None:
|
2006-05-02 02:41:26 +05:30
|
|
|
y = self._get_int(groups[4])
|
|
|
|
m = 0
|
|
|
|
d = 0
|
|
|
|
else:
|
|
|
|
y = self._get_int(groups[1])
|
|
|
|
m = self._get_int(groups[3])
|
|
|
|
d = self._get_int(groups[4])
|
2004-10-12 06:59:39 +05:30
|
|
|
else:
|
2006-05-02 02:41:26 +05:30
|
|
|
y = self._get_int(groups[4])
|
|
|
|
if self.dmy:
|
|
|
|
m = self._get_int(groups[3])
|
|
|
|
d = self._get_int(groups[1])
|
|
|
|
else:
|
|
|
|
m = self._get_int(groups[1])
|
|
|
|
d = self._get_int(groups[3])
|
2007-06-27 10:20:33 +05:30
|
|
|
value = (d, m, y, False)
|
|
|
|
if check and not check((d, m, y)):
|
2004-11-15 07:37:21 +05:30
|
|
|
value = Date.EMPTY
|
|
|
|
return value
|
|
|
|
|
2004-09-17 09:00:04 +05:30
|
|
|
return Date.EMPTY
|
|
|
|
|
2007-06-27 10:20:33 +05:30
|
|
|
def match_calendar(self, text, cal):
|
2004-09-17 09:00:04 +05:30
|
|
|
"""
|
2004-11-17 10:37:49 +05:30
|
|
|
Try parsing calendar.
|
2004-09-24 03:02:07 +05:30
|
|
|
|
2006-05-02 02:41:26 +05:30
|
|
|
Return calendar index and the text with calendar removed.
|
2004-11-17 10:37:49 +05:30
|
|
|
"""
|
2004-09-24 03:02:07 +05:30
|
|
|
match = self._cal.match(text)
|
|
|
|
if match:
|
2006-05-02 02:41:26 +05:30
|
|
|
cal = self.calendar_to_int[match.group(2).lower()]
|
|
|
|
text = match.group(1) + match.group(3)
|
2007-06-27 10:20:33 +05:30
|
|
|
return (text, cal)
|
2004-09-24 03:02:07 +05:30
|
|
|
|
2009-01-17 20:03:10 +05:30
|
|
|
def match_calendar_newyear(self, text, cal, newyear):
|
|
|
|
"""
|
|
|
|
Try parsing calendar and newyear code.
|
|
|
|
|
|
|
|
Return newyear index and the text with calendar removed.
|
|
|
|
"""
|
|
|
|
match = self._calny.match(text)
|
|
|
|
if match:
|
|
|
|
cal = self.calendar_to_int[match.group(2).lower()]
|
|
|
|
newyear = self.newyear_to_int[match.group(3).lower()]
|
|
|
|
text = match.group(1) + match.group(4)
|
2010-04-04 05:03:17 +05:30
|
|
|
else:
|
|
|
|
match = self._calny_iso.match(text)
|
|
|
|
if match:
|
|
|
|
cal = self.calendar_to_int[match.group(2).lower()]
|
|
|
|
newyear = tuple([int(s) for s in match.group(3).split("-")])
|
|
|
|
text = match.group(1) + match.group(4)
|
2009-01-17 20:03:10 +05:30
|
|
|
return (text, cal, newyear)
|
|
|
|
|
|
|
|
def match_newyear(self, text, newyear):
|
|
|
|
"""
|
|
|
|
Try parsing calendar and newyear code.
|
|
|
|
|
|
|
|
Return newyear index and the text with calendar removed.
|
|
|
|
"""
|
|
|
|
match = self._ny.match(text)
|
|
|
|
if match:
|
|
|
|
newyear = self.newyear_to_int[match.group(2).lower()]
|
|
|
|
text = match.group(1) + match.group(3)
|
2010-04-04 05:03:17 +05:30
|
|
|
else:
|
|
|
|
match = self._ny_iso.match(text)
|
|
|
|
if match:
|
|
|
|
newyear = tuple([int(s) for s in match.group(2).split("-")])
|
|
|
|
text = match.group(1) + match.group(3)
|
2009-01-17 20:03:10 +05:30
|
|
|
return (text, newyear)
|
|
|
|
|
2007-06-27 10:20:33 +05:30
|
|
|
def match_quality(self, text, qual):
|
2004-11-17 10:37:49 +05:30
|
|
|
"""
|
|
|
|
Try matching quality.
|
|
|
|
|
2006-05-02 02:41:26 +05:30
|
|
|
Return quality index and the text with quality removed.
|
2004-11-17 10:37:49 +05:30
|
|
|
"""
|
2004-09-17 09:00:04 +05:30
|
|
|
match = self._qual.match(text)
|
|
|
|
if match:
|
2006-05-02 02:41:26 +05:30
|
|
|
qual = self.quality_to_int[match.group(2).lower()]
|
|
|
|
text = match.group(1) + match.group(3)
|
2007-06-27 10:20:33 +05:30
|
|
|
return (text, qual)
|
2004-09-24 03:02:07 +05:30
|
|
|
|
2009-01-17 20:03:10 +05:30
|
|
|
def match_span(self, text, cal, ny, qual, date):
|
2004-11-17 10:37:49 +05:30
|
|
|
"""
|
|
|
|
Try matching span date.
|
|
|
|
|
|
|
|
On success, set the date and return 1. On failure return 0.
|
|
|
|
"""
|
2004-09-17 09:00:04 +05:30
|
|
|
match = self._span.match(text)
|
|
|
|
if match:
|
2004-11-17 10:37:49 +05:30
|
|
|
text_parser = self.parser[cal]
|
2007-06-27 10:20:33 +05:30
|
|
|
(text1, bc1) = self.match_bce(match.group('start'))
|
|
|
|
start = self._parse_subdate(text1, text_parser)
|
2010-03-24 21:35:42 +05:30
|
|
|
if start == Date.EMPTY and text1 != "":
|
|
|
|
return 0
|
2006-05-02 02:41:26 +05:30
|
|
|
if bc1:
|
|
|
|
start = self.invert_year(start)
|
|
|
|
|
2007-06-27 10:20:33 +05:30
|
|
|
(text2, bc2) = self.match_bce(match.group('stop'))
|
|
|
|
stop = self._parse_subdate(text2, text_parser)
|
2010-03-24 21:35:42 +05:30
|
|
|
if stop == Date.EMPTY and text2 != "":
|
|
|
|
return 0
|
2006-05-02 02:41:26 +05:30
|
|
|
if bc2:
|
|
|
|
stop = self.invert_year(stop)
|
|
|
|
|
2009-01-17 20:03:10 +05:30
|
|
|
date.set(qual, Date.MOD_SPAN, cal, start + stop, newyear=ny)
|
2004-11-17 10:37:49 +05:30
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
2009-01-17 20:03:10 +05:30
|
|
|
def match_range(self, text, cal, ny, qual, date):
|
2004-11-17 10:37:49 +05:30
|
|
|
"""
|
|
|
|
Try matching range date.
|
|
|
|
|
|
|
|
On success, set the date and return 1. On failure return 0.
|
|
|
|
"""
|
2004-09-17 09:00:04 +05:30
|
|
|
match = self._range.match(text)
|
|
|
|
if match:
|
2004-11-17 10:37:49 +05:30
|
|
|
text_parser = self.parser[cal]
|
2007-06-27 10:20:33 +05:30
|
|
|
(text1, bc1) = self.match_bce(match.group('start'))
|
|
|
|
start = self._parse_subdate(text1, text_parser)
|
2010-03-24 21:35:42 +05:30
|
|
|
if start == Date.EMPTY and text1 != "":
|
|
|
|
return 0
|
2006-05-02 02:41:26 +05:30
|
|
|
if bc1:
|
|
|
|
start = self.invert_year(start)
|
|
|
|
|
2007-06-27 10:20:33 +05:30
|
|
|
(text2, bc2) = self.match_bce(match.group('stop'))
|
|
|
|
stop = self._parse_subdate(text2, text_parser)
|
2010-03-24 21:35:42 +05:30
|
|
|
if stop == Date.EMPTY and text2 != "":
|
|
|
|
return 0
|
2006-05-02 02:41:26 +05:30
|
|
|
if bc2:
|
|
|
|
stop = self.invert_year(stop)
|
|
|
|
|
2009-01-17 20:03:10 +05:30
|
|
|
date.set(qual, Date.MOD_RANGE, cal, start + stop, newyear=ny)
|
2004-11-17 10:37:49 +05:30
|
|
|
return 1
|
|
|
|
return 0
|
2004-09-20 08:42:51 +05:30
|
|
|
|
2007-06-27 10:20:33 +05:30
|
|
|
def match_bce(self, text):
|
2004-11-17 10:37:49 +05:30
|
|
|
"""
|
|
|
|
Try matching BCE qualifier.
|
|
|
|
|
2006-05-02 02:41:26 +05:30
|
|
|
Return BCE (True/False) and the text with matched part removed.
|
2004-11-17 10:37:49 +05:30
|
|
|
"""
|
2004-10-16 10:40:35 +05:30
|
|
|
match = self._bce_re.match(text)
|
|
|
|
bc = False
|
|
|
|
if match:
|
2008-01-06 01:40:26 +05:30
|
|
|
# bce is in the match.group(2)
|
2006-05-02 02:41:26 +05:30
|
|
|
try:
|
|
|
|
text = match.group(1) + match.group(3)
|
|
|
|
except:
|
2008-04-19 10:21:12 +05:30
|
|
|
print "ERROR MATCH:", match.groups()
|
2004-10-16 10:40:35 +05:30
|
|
|
bc = True
|
2007-06-27 10:20:33 +05:30
|
|
|
return (text, bc)
|
2004-11-17 10:37:49 +05:30
|
|
|
|
2009-01-17 20:03:10 +05:30
|
|
|
def match_modifier(self, text, cal, ny, qual, bc, date):
|
2004-11-17 10:37:49 +05:30
|
|
|
"""
|
|
|
|
Try matching date with modifier.
|
|
|
|
|
|
|
|
On success, set the date and return 1. On failure return 0.
|
|
|
|
"""
|
2005-12-06 12:08:09 +05:30
|
|
|
# modifiers before the date
|
2004-09-17 09:00:04 +05:30
|
|
|
match = self._modifier.match(text)
|
|
|
|
if match:
|
|
|
|
grps = match.groups()
|
2006-05-02 02:41:26 +05:30
|
|
|
start = self._parse_subdate(grps[1], self.parser[cal])
|
|
|
|
mod = self.modifier_to_int.get(grps[0].lower(), Date.MOD_NONE)
|
2007-09-21 00:59:50 +05:30
|
|
|
if start == Date.EMPTY:
|
|
|
|
date.set_modifier(Date.MOD_TEXTONLY)
|
|
|
|
date.set_text_value(text)
|
|
|
|
elif bc:
|
2009-01-17 20:03:10 +05:30
|
|
|
date.set(qual, mod, cal, self.invert_year(start), newyear=ny)
|
2004-10-16 10:40:35 +05:30
|
|
|
else:
|
2009-01-17 20:03:10 +05:30
|
|
|
date.set(qual, mod, cal, start, newyear=ny)
|
2005-08-18 11:28:28 +05:30
|
|
|
return True
|
2005-12-06 12:08:09 +05:30
|
|
|
# modifiers after the date
|
|
|
|
if self.modifier_after_to_int:
|
|
|
|
match = self._modifier_after.match(text)
|
|
|
|
if match:
|
|
|
|
grps = match.groups()
|
2006-05-02 02:41:26 +05:30
|
|
|
start = self._parse_subdate(grps[0], self.parser[cal])
|
2006-02-04 03:33:53 +05:30
|
|
|
mod = self.modifier_after_to_int.get(grps[1].lower(),
|
|
|
|
Date.MOD_NONE)
|
2010-03-24 21:35:42 +05:30
|
|
|
if start == Date.EMPTY:
|
|
|
|
date.set_modifier(Date.MOD_TEXTONLY)
|
|
|
|
date.set_text_value(text)
|
|
|
|
elif bc:
|
2009-01-17 20:03:10 +05:30
|
|
|
date.set(qual, mod, cal, self.invert_year(start), newyear=ny)
|
2005-12-06 12:08:09 +05:30
|
|
|
else:
|
2009-01-17 20:03:10 +05:30
|
|
|
date.set(qual, mod, cal, start, newyear=ny)
|
2005-12-06 12:08:09 +05:30
|
|
|
return True
|
2005-08-18 11:28:28 +05:30
|
|
|
match = self._abt2.match(text)
|
|
|
|
if match:
|
|
|
|
grps = match.groups()
|
|
|
|
start = self._parse_subdate(grps[0])
|
|
|
|
mod = Date.MOD_ABOUT
|
2010-03-24 21:35:42 +05:30
|
|
|
if start == Date.EMPTY:
|
|
|
|
date.set_modifier(Date.MOD_TEXTONLY)
|
|
|
|
date.set_text_value(text)
|
|
|
|
elif bc:
|
2009-01-17 20:03:10 +05:30
|
|
|
date.set(qual, mod, cal, self.invert_year(start), newyear=ny)
|
2005-08-18 11:28:28 +05:30
|
|
|
else:
|
2009-01-17 20:03:10 +05:30
|
|
|
date.set(qual, mod, cal, start, newyear=ny)
|
2005-08-18 11:28:28 +05:30
|
|
|
return True
|
|
|
|
return False
|
2004-11-17 10:37:49 +05:30
|
|
|
|
2007-06-27 10:20:33 +05:30
|
|
|
def set_date(self, date, text):
|
2004-11-17 10:37:49 +05:30
|
|
|
"""
|
|
|
|
Parses the text and sets the date according to the parsing.
|
|
|
|
"""
|
2008-04-24 06:36:33 +05:30
|
|
|
text = text.strip() # otherwise spaces can make it a bad date
|
2004-11-17 10:37:49 +05:30
|
|
|
date.set_text_value(text)
|
|
|
|
qual = Date.QUAL_NONE
|
|
|
|
cal = Date.CAL_GREGORIAN
|
2009-01-17 20:03:10 +05:30
|
|
|
newyear = Date.NEWYEAR_JAN1
|
2004-11-17 10:37:49 +05:30
|
|
|
|
2009-01-17 20:03:10 +05:30
|
|
|
(text, cal, newyear) = self.match_calendar_newyear(text, cal, newyear)
|
|
|
|
(text, newyear) = self.match_newyear(text, newyear)
|
2009-01-17 20:52:28 +05:30
|
|
|
(text, cal) = self.match_calendar(text, cal)
|
2007-06-27 10:20:33 +05:30
|
|
|
(text, qual) = self.match_quality(text, qual)
|
2005-12-06 12:08:09 +05:30
|
|
|
|
2009-01-17 20:03:10 +05:30
|
|
|
if self.match_span(text, cal, newyear, qual, date):
|
2004-11-17 10:37:49 +05:30
|
|
|
return
|
2009-01-17 20:03:10 +05:30
|
|
|
if self.match_range(text, cal, newyear, qual, date):
|
2004-11-17 10:37:49 +05:30
|
|
|
return
|
2004-09-17 09:00:04 +05:30
|
|
|
|
2007-06-27 10:20:33 +05:30
|
|
|
(text, bc) = self.match_bce(text)
|
2009-01-17 20:03:10 +05:30
|
|
|
if self.match_modifier(text, cal, newyear, qual, bc, date):
|
2004-11-17 10:37:49 +05:30
|
|
|
return
|
2005-12-06 12:08:09 +05:30
|
|
|
|
|
|
|
try:
|
2007-06-27 10:20:33 +05:30
|
|
|
subdate = self._parse_subdate(text, self.parser[cal])
|
2005-12-06 12:08:09 +05:30
|
|
|
if subdate == Date.EMPTY and text != "":
|
|
|
|
date.set_as_text(text)
|
|
|
|
return
|
2010-03-24 21:35:42 +05:30
|
|
|
#else:
|
|
|
|
# print 'valid subdate', text, subdate
|
2005-12-06 12:08:09 +05:30
|
|
|
except:
|
|
|
|
date.set_as_text(text)
|
|
|
|
return
|
2004-09-17 09:00:04 +05:30
|
|
|
|
2004-10-16 10:40:35 +05:30
|
|
|
if bc:
|
2009-01-17 20:03:10 +05:30
|
|
|
date.set(qual, Date.MOD_NONE, cal, self.invert_year(subdate), newyear=newyear)
|
2004-10-16 10:40:35 +05:30
|
|
|
else:
|
2009-01-17 20:03:10 +05:30
|
|
|
date.set(qual, Date.MOD_NONE, cal, subdate, newyear=newyear)
|
2008-02-25 11:16:45 +05:30
|
|
|
|
2007-06-27 10:20:33 +05:30
|
|
|
def invert_year(self, subdate):
|
|
|
|
return (subdate[0], subdate[1], -subdate[2], subdate[3])
|
2004-10-16 10:40:35 +05:30
|
|
|
|
2007-06-27 10:20:33 +05:30
|
|
|
def parse(self, text):
|
2004-09-17 09:00:04 +05:30
|
|
|
"""
|
|
|
|
Parses the text, returning a Date object.
|
|
|
|
"""
|
2006-02-04 03:33:53 +05:30
|
|
|
new_date = Date()
|
2005-05-24 18:38:06 +05:30
|
|
|
try:
|
2007-06-27 10:20:33 +05:30
|
|
|
self.set_date(new_date, text)
|
2005-05-24 18:38:06 +05:30
|
|
|
except DateError:
|
|
|
|
new_date.set_as_text(text)
|
2004-09-17 09:00:04 +05:30
|
|
|
return new_date
|