Moved gedcom date constants and functions to gen.lib.date so they can be shared

svn: r13249
This commit is contained in:
Doug Blank 2009-09-25 20:45:05 +00:00
parent 39b0858c59
commit 8883fe0f97
2 changed files with 104 additions and 119 deletions

View File

@ -1643,3 +1643,100 @@ def Today():
current_date.set_yr_mon_day(*time.localtime(time.time())[0:3])
return current_date
#-------------------------------------------------------------------------
#
# GEDCOM Date Constants
#
#-------------------------------------------------------------------------
HMONTH = [
"", "ELUL", "TSH", "CSH", "KSL", "TVT", "SHV", "ADR",
"ADS", "NSN", "IYR", "SVN", "TMZ", "AAV", "ELL" ]
FMONTH = [
"", "VEND", "BRUM", "FRIM", "NIVO", "PLUV", "VENT",
"GERM", "FLOR", "PRAI", "MESS", "THER", "FRUC", "COMP"]
MONTH = [
"", "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
"JUL", "AUG", "SEP", "OCT", "NOV", "DEC" ]
CALENDAR_MAP = {
Date.CAL_HEBREW : (HMONTH, '@#DHEBREW@'),
Date.CAL_FRENCH : (FMONTH, '@#DFRENCH R@'),
Date.CAL_JULIAN : (MONTH, '@#DJULIAN@'),
Date.CAL_SWEDISH : (MONTH, '@#DUNKNOWN@'),
}
DATE_MODIFIER = {
Date.MOD_ABOUT : "ABT",
Date.MOD_BEFORE : "BEF",
Date.MOD_AFTER : "AFT",
#Date.MOD_INTERPRETED : "INT",
}
DATE_QUALITY = {
Date.QUAL_CALCULATED : "CAL",
Date.QUAL_ESTIMATED : "EST",
}
#-------------------------------------------------------------------------
#
# Date Functions
#
#-------------------------------------------------------------------------
def make_gedcom_date(subdate, calendar, mode, quality):
"""
Convert a GRAMPS date structure into a GEDCOM compatible date.
"""
retval = ""
(day, mon, year) = subdate[0:3]
(mmap, prefix) = CALENDAR_MAP.get(calendar, (MONTH, ""))
if year < 0:
year = -year
bce = " B.C."
else:
bce = ""
try:
retval = __build_date_string(day, mon, year, bce, mmap)
except IndexError:
print "Month index error - %d" % mon
retval = "%d%s" % (year, bce)
if calendar == Date.CAL_SWEDISH:
# If Swedish calendar use ISO for for date and append (swedish)
# to indicate calandar
if year and not mon and not day:
retval = "%i" % (year)
else:
retval = "%i-%02i-%02i" % (year, mon, day)
retval = retval + " (swedish)"
# Skip prefix @#DUNKNOWN@ as it seems
# not used in all other genealogy applications.
# GRAMPS can handle it on import, but not with (swedish) appended
# to explain what calendar, the unknown refer to
prefix = ""
if prefix:
retval = "%s %s" % (prefix, retval)
if mode in DATE_MODIFIER:
retval = "%s %s" % (DATE_MODIFIER[mode], retval)
if quality in DATE_QUALITY:
retval = "%s %s" % (DATE_QUALITY[quality], retval)
return retval
def __build_date_string(day, mon, year, bce, mmap):
"""
Build a date string from the supplied information.
"""
if day == 0:
if mon == 0:
retval = '%d%s' % (year, bce)
elif year == 0:
retval = '(%s)' % mmap[mon]
else:
retval = "%s %d%s" % (mmap[mon], year, bce)
elif mon == 0:
retval = '%d%s' % (year, bce)
elif year == 0:
retval = "(%d %s)" % (day, mmap[mon])
else:
retval = "%d %s %d%s" % (day, mmap[mon], year, bce)
return retval

View File

@ -39,6 +39,7 @@ import time
#
#-------------------------------------------------------------------------
import gen.lib
from gen.lib.date import make_gedcom_date, MONTH
import const
import GrampsDbUtils._GedcomInfo as GedcomInfo
import Errors
@ -59,43 +60,6 @@ NEEDS_PARAMETER = set(
["CAST", "DSCR", "EDUC", "IDNO", "NATI", "NCHI",
"NMR", "OCCU", "PROP", "RELI", "SSN", "TITL"])
#-------------------------------------------------------------------------
#
# Calendar month names
#
#-------------------------------------------------------------------------
HMONTH = [
"", "ELUL", "TSH", "CSH", "KSL", "TVT", "SHV", "ADR",
"ADS", "NSN", "IYR", "SVN", "TMZ", "AAV", "ELL" ]
FMONTH = [
"", "VEND", "BRUM", "FRIM", "NIVO", "PLUV", "VENT",
"GERM", "FLOR", "PRAI", "MESS", "THER", "FRUC", "COMP"]
MONTH = [
"", "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
"JUL", "AUG", "SEP", "OCT", "NOV", "DEC" ]
CALENDAR_MAP = {
gen.lib.Date.CAL_HEBREW : (HMONTH, '@#DHEBREW@'),
gen.lib.Date.CAL_FRENCH : (FMONTH, '@#DFRENCH R@'),
gen.lib.Date.CAL_JULIAN : (MONTH, '@#DJULIAN@'),
gen.lib.Date.CAL_SWEDISH : (MONTH, '@#DUNKNOWN@'),
}
DATE_MODIFIER = {
gen.lib.Date.MOD_ABOUT : "ABT",
gen.lib.Date.MOD_BEFORE : "BEF",
gen.lib.Date.MOD_AFTER : "AFT",
#RelLib.Date.MOD_INTERPRETED : "INT",
}
DATE_QUALITY = {
gen.lib.Date.QUAL_CALCULATED : "CAL",
gen.lib.Date.QUAL_ESTIMATED : "EST",
}
LDS_ORD_NAME = {
gen.lib.LdsOrd.BAPTISM : 'BAPL',
gen.lib.LdsOrd.ENDOWMENT : 'ENDL',
@ -184,82 +148,6 @@ def sort_handles_by_id(handle_list, handle_to_object):
sorted_list.sort()
return sorted_list
#-------------------------------------------------------------------------
#
# make_date
#
#-------------------------------------------------------------------------
def make_date(subdate, calendar, mode, quality):
"""
Convert a GRAMPS date structure into a GEDCOM compatible date.
"""
retval = ""
(day, mon, year) = subdate[0:3]
(mmap, prefix) = CALENDAR_MAP.get(calendar, (MONTH, ""))
if year < 0:
year = -year
bce = " B.C."
else:
bce = ""
try:
retval = __build_date_string(day, mon, year, bce, mmap)
except IndexError:
print "Month index error - %d" % mon
retval = "%d%s" % (year, bce)
if calendar == gen.lib.Date.CAL_SWEDISH:
# If Swedish calendar use ISO for for date and append (swedish)
# to indicate calandar
if year and not mon and not day:
retval = "%i" % (year)
else:
retval = "%i-%02i-%02i" % (year, mon, day)
retval = retval + " (swedish)"
# Skip prefix @#DUNKNOWN@ as it seems
# not used in all other genealogy applications.
# GRAMPS can handle it on import, but not with (swedish) appended
# to explain what calendar, the unknown refer to
prefix = ""
if prefix:
retval = "%s %s" % (prefix, retval)
if mode in DATE_MODIFIER:
retval = "%s %s" % (DATE_MODIFIER[mode], retval)
if quality in DATE_QUALITY:
retval = "%s %s" % (DATE_QUALITY[quality], retval)
return retval
#-------------------------------------------------------------------------
#
# __build_date_string
#
#-------------------------------------------------------------------------
def __build_date_string(day, mon, year, bce, mmap):
"""
Build a date string from the supplied information.
"""
if day == 0:
if mon == 0:
retval = '%d%s' % (year, bce)
elif year == 0:
retval = '(%s)' % mmap[mon]
else:
retval = "%s %d%s" % (mmap[mon], year, bce)
elif mon == 0:
retval = '%d%s' % (year, bce)
elif year == 0:
retval = "(%d %s)" % (day, mmap[mon])
else:
retval = "%d %s %d%s" % (day, mmap[mon], year, bce)
return retval
#-------------------------------------------------------------------------
#
# breakup
@ -1318,7 +1206,7 @@ class GedcomWriter(BasicUtils.UpdateCallback):
def __date(self, level, date):
"""
Write the 'DATE' GEDCOM token, along with the date in GEDCOM's
expected formta.
expected format.
"""
start = date.get_start_date()
if start != gen.lib.Date.EMPTY:
@ -1327,14 +1215,14 @@ class GedcomWriter(BasicUtils.UpdateCallback):
quality = date.get_quality()
if mod == gen.lib.Date.MOD_SPAN:
val = "FROM %s TO %s" % (
make_date(start, cal, mod, quality),
make_date(date.get_stop_date(), cal, mod, quality))
make_gedcom_date(start, cal, mod, quality),
make_gedcom_date(date.get_stop_date(), cal, mod, quality))
elif mod == gen.lib.Date.MOD_RANGE:
val = "BET %s AND %s" % (
make_date(start, cal, mod, quality),
make_date(date.get_stop_date(), cal, mod, quality))
make_gedcom_date(start, cal, mod, quality),
make_gedcom_date(date.get_stop_date(), cal, mod, quality))
else:
val = make_date(start, cal, mod, quality)
val = make_gedcom_date(start, cal, mod, quality)
self.__writeln(level, 'DATE', val)
elif date.get_text():
self.__writeln(level, 'DATE', date.get_text())