gramps/data/evidencestyle/evidencefieldgenerator.py
Benny Malengier bf29e57039 2332: Allow reorder of Data in the Data tab of Source: make Data SourceAttribute, which have sourcetype,
link with GEPS 018: Evidence
This is step 1 needed for GEPS 018. All types are defined needed to evidence style references
This commit also contains fix for 6777: Crash on export to GEDCOM when there are addresses


svn: r22423
2013-05-26 19:28:57 +00:00

180 lines
6.4 KiB
Python

#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2013 Benny Malengier
#
# 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
#
# $Id$
"""
This module parses the evidence csv file and generates the code we need in
Gramps to use the evidence style.
"""
#-------------------------------------------------------------------------
#
# Standard Python modules
#
#-------------------------------------------------------------------------
import csv
#-------------------------------------------------------------------------
#
# Code
#
#-------------------------------------------------------------------------
csvfilename = "evidence_style.csv"
NRCOL = 0
CATCOL = 1
CATTYPECOL = 2
TYPECOL = 3
DESCRCOL= 4
CITETYPECOL = 5
IDENTCOL = 6
LDELCOL = 7 # left delimiter
FIELDCOL = 8
RDELCOL = 9 # right delimiter
STYLECOL = 10
PRIVACYCOL = 11
OPTCOL = 12
nr = -1
cat = ''
cattype = ''
type = ''
descr = ''
cite_type = ''
ident = ''
TYPE2CITEMAP = {}
FIELDTYPEMAP = {}
index = 10
indexval = 10
first = True
with open(csvfilename, 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
if first:
#skip first row with headers
first=False
continue
if row[CATCOL]:
cat = row[CATCOL]
cattype = row[CATTYPECOL]
type = row[TYPECOL]
descr = row[DESCRCOL]
source_type = row[IDENTCOL]
if descr:
source_descr = '%s - %s - %s (%s)' % (type, cattype, cat, descr)
source_descr_code = "_('%(first)s - %(sec)s - %(third)s (%(fourth)s)') % { "\
" 'first': _('" + type + "'),"\
" 'sec': _('" + cattype + "'),"\
" 'third': _('" + cat + "'),"\
" 'fourth': _('" + descr + "')}"
else:
source_descr = '%s - %s - %s' % (type, cattype, cat)
source_descr_code = "_('%(first)s - %(sec)s - %(third)s') % { "\
" 'first': _('" + type + "'),"\
" 'sec': _('" + cattype + "'),"\
" 'third': _('" + cat + "')}"
if source_type in TYPE2CITEMAP:
assert TYPE2CITEMAP[source_type] ['descr'] == source_descr, source_type + ' ' + TYPE2CITEMAP[source_type] ['descr'] + ' NOT ' + source_descr
else:
TYPE2CITEMAP[source_type] = {'L': [], 'F': [], 'S': [],
'i': indexval, 'descr': source_descr,
'descrcode': source_descr_code}
indexval += 1
if row[CITETYPECOL]:
#new citation type,
cite_type = row[CITETYPECOL]
#add field for template to evidence style
field = row[FIELDCOL]
field_type = field.replace(' ', '_').replace("'","")\
.replace('&','AND').replace('(', '6').replace(')','9')\
.replace('[', '').replace(']','').replace('/', '_OR_')\
.replace(',', '').replace('.', '').replace(':', '')\
.replace('-', '_')
field_descr = field.replace('[', '').replace(']','').lower().capitalize()
if field_type in FIELDTYPEMAP:
assert field_descr == FIELDTYPEMAP[field_type][1], 'Problem %s %s %s' % (field_type, field_descr, FIELDTYPEMAP[field_type][1])
else:
FIELDTYPEMAP[field_type] = (index, field_descr)
index += 1
fielddata = []
private = 'False'
if row[PRIVACYCOL]:
private = 'True'
optional = 'False'
if row[OPTCOL]:
optional = 'True'
TYPE2CITEMAP[source_type][cite_type] += [(row[LDELCOL], field_type,
row[RDELCOL], row[STYLECOL], private, optional)]
#now generate the python code we need in source attr types
code = ""
datamap = "\n _DATAMAP += [\n"
allkeys = sorted(FIELDTYPEMAP.keys())
for field_type in allkeys:
code += " " + field_type + ' = %d\n' % FIELDTYPEMAP[field_type][0]
datamap += ' (' + field_type + ', _("' + FIELDTYPEMAP[field_type][1] \
+'"), "' + FIELDTYPEMAP[field_type][1] + '"),\n'
code += '\n' + datamap + ' ]\n'
with open('srcattrtype_extra.py', 'wb') as srcattrfile:
srcattrfile.write(code)
#now generate the python code we need in evidencestyle
# we have predefined sourcetypes, and these have a template for formatting
#
#first an English to internationalized map
code = " #SRCTYPE has some predefined values which map to citation styles\n"
datamap = " _SRCTYPEVAL_MAP = [\n"
allkeys = sorted(TYPE2CITEMAP.keys())
for source_type in allkeys:
code += " " + source_type + ' = %d\n' % TYPE2CITEMAP[source_type]['i']
# we use descrcode in to translate string to reduce work for translators
datamap += ' (' + source_type + ', ' + TYPE2CITEMAP[source_type]['descrcode'] \
+', "' + TYPE2CITEMAP[source_type]['descr'] + '"),\n'
code += '\n # Localization of the different source types\n'\
+ datamap + ' ]\n'
code += "\n #templates for the source types defined\n"
code += ' EVIDENCETEMPLATES = {\n'
for source_type in allkeys:
code += " '" + source_type + "': {\n"
for val in ['F', 'L', 'S']:
code += " '" + val + "': [\n"
for field in TYPE2CITEMAP[source_type][val]:
# field is tuple (row[LDELCOL], field_type, row[RDELCOL], row[STYLECOL]
# , private, optional)
code += " ('"+ field[0] + "', " + field[1] + ", '" + field[2] + \
"', '" + field[3] + "', " + field[4] + ", " + field[5] + "),\n"
code += " ],\n"
code += " },\n"
code += " }\n"
with open('srcattrtype_extraevidence.py', 'wb') as srcattrfile:
srcattrfile.write(code)