2013-05-27 00:58:57 +05:30
|
|
|
#
|
|
|
|
# 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
|
2013-06-17 16:44:05 +05:30
|
|
|
LABELCOL = 9
|
|
|
|
RDELCOL = 10 # right delimiter
|
|
|
|
GEDCOMCOL = 11
|
|
|
|
SHORTERCOL = 12
|
|
|
|
STYLECOL = 13
|
|
|
|
PRIVACYCOL = 14
|
|
|
|
OPTCOL = 15
|
2013-06-01 15:18:52 +05:30
|
|
|
|
2013-06-02 15:38:26 +05:30
|
|
|
CITE_TYPES = {'F': 'REF_TYPE_F', 'L': 'REF_TYPE_L', 'S': 'REF_TYPE_S'}
|
2013-06-01 15:18:52 +05:30
|
|
|
GEDCOMFIELDS = {'A': 'GED_AUTHOR', 'T': 'GED_TITLE',
|
|
|
|
'P': 'GED_PUBINF', 'D': 'GED_DATE'}
|
|
|
|
SHORTERALG = {'LOC': 'SHORTERALG_LOC', 'YEAR': 'SHORTERALG_YEAR',
|
|
|
|
'ETAL': 'SHORTERALG_ETAL', 'REV.': 'SHORTERALG_REVERT_TO_DOT'}
|
|
|
|
STYLES = {'Quoted': 'STYLE_QUOTE', 'Italics': 'STYLE_EMPH',
|
|
|
|
'QuotedCont': 'STYLE_QUOTECONT', 'Bold': 'STYLE_BOLD'}
|
2013-05-27 00:58:57 +05:30
|
|
|
|
|
|
|
nr = -1
|
|
|
|
cat = ''
|
|
|
|
cattype = ''
|
|
|
|
type = ''
|
|
|
|
descr = ''
|
|
|
|
cite_type = ''
|
|
|
|
ident = ''
|
|
|
|
|
|
|
|
TYPE2CITEMAP = {}
|
|
|
|
FIELDTYPEMAP = {}
|
2013-05-28 01:15:30 +05:30
|
|
|
index = 100
|
2013-05-27 00:58:57 +05:30
|
|
|
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]:
|
2013-05-28 01:15:30 +05:30
|
|
|
cat = row[CATCOL].strip()
|
|
|
|
cattype = row[CATTYPECOL].strip()
|
2013-05-28 01:56:48 +05:30
|
|
|
types = row[TYPECOL].strip()
|
2013-05-28 01:15:30 +05:30
|
|
|
descr = row[DESCRCOL].strip()
|
|
|
|
source_type = row[IDENTCOL].strip()
|
2013-05-27 00:58:57 +05:30
|
|
|
if descr:
|
2013-05-28 01:56:48 +05:30
|
|
|
source_descr = '%s - %s - %s (%s)' % (cat, cattype, types, descr)
|
2013-05-27 00:58:57 +05:30
|
|
|
source_descr_code = "_('%(first)s - %(sec)s - %(third)s (%(fourth)s)') % { "\
|
2013-05-28 01:56:48 +05:30
|
|
|
" 'first': _('" + cat + "'),"\
|
2013-05-27 00:58:57 +05:30
|
|
|
" 'sec': _('" + cattype + "'),"\
|
2013-05-28 01:56:48 +05:30
|
|
|
" 'third': _('" + types + "'),"\
|
2013-05-27 00:58:57 +05:30
|
|
|
" 'fourth': _('" + descr + "')}"
|
|
|
|
else:
|
2013-05-28 01:56:48 +05:30
|
|
|
source_descr = '%s - %s - %s' % (cat, cattype, types)
|
2013-05-27 00:58:57 +05:30
|
|
|
source_descr_code = "_('%(first)s - %(sec)s - %(third)s') % { "\
|
2013-05-28 01:56:48 +05:30
|
|
|
" 'first': _('" + cat + "'),"\
|
2013-05-27 00:58:57 +05:30
|
|
|
" 'sec': _('" + cattype + "'),"\
|
2013-05-28 01:56:48 +05:30
|
|
|
" 'third': _('" + types + "')}"
|
2013-05-27 00:58:57 +05:30
|
|
|
if source_type in TYPE2CITEMAP:
|
|
|
|
assert TYPE2CITEMAP[source_type] ['descr'] == source_descr, source_type + ' ' + TYPE2CITEMAP[source_type] ['descr'] + ' NOT ' + source_descr
|
|
|
|
else:
|
2013-06-02 15:38:26 +05:30
|
|
|
TYPE2CITEMAP[source_type] = {'REF_TYPE_L': [], 'REF_TYPE_F': [],
|
|
|
|
'REF_TYPE_S': [],
|
2013-05-27 00:58:57 +05:30
|
|
|
'i': indexval, 'descr': source_descr,
|
|
|
|
'descrcode': source_descr_code}
|
|
|
|
indexval += 1
|
|
|
|
|
|
|
|
|
|
|
|
if row[CITETYPECOL]:
|
2013-06-01 15:18:52 +05:30
|
|
|
#new citation type,
|
2013-05-28 01:15:30 +05:30
|
|
|
cite_type = row[CITETYPECOL].strip()
|
2013-06-01 15:18:52 +05:30
|
|
|
if cite_type == 'S':
|
|
|
|
shortcite = True
|
|
|
|
else:
|
|
|
|
shortcite = False
|
2013-06-02 15:38:26 +05:30
|
|
|
cite_type = CITE_TYPES[cite_type]
|
2013-05-27 00:58:57 +05:30
|
|
|
#add field for template to evidence style
|
2013-05-28 01:15:30 +05:30
|
|
|
field = row[FIELDCOL].strip()
|
2013-05-27 00:58:57 +05:30
|
|
|
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'
|
2013-05-28 01:15:30 +05:30
|
|
|
if row[PRIVACYCOL].strip():
|
2013-05-27 00:58:57 +05:30
|
|
|
private = 'True'
|
|
|
|
optional = 'False'
|
2013-05-28 01:15:30 +05:30
|
|
|
if row[OPTCOL].strip():
|
2013-05-27 00:58:57 +05:30
|
|
|
optional = 'True'
|
2013-06-01 15:18:52 +05:30
|
|
|
shorteralg = SHORTERALG.get(row[SHORTERCOL].strip()) or 'EMPTY'
|
|
|
|
gedcommap = GEDCOMFIELDS.get(row[GEDCOMCOL].strip()) or 'EMPTY'
|
|
|
|
style = STYLES.get(row[STYLECOL].strip()) or 'EMPTY'
|
|
|
|
|
2013-05-27 00:58:57 +05:30
|
|
|
TYPE2CITEMAP[source_type][cite_type] += [(row[LDELCOL], field_type,
|
2013-06-01 15:18:52 +05:30
|
|
|
row[RDELCOL], style, private, optional,
|
|
|
|
shorteralg, gedcommap)]
|
|
|
|
#if shorttype, we store a type for the short version so user can store
|
|
|
|
#this
|
2013-06-02 15:38:26 +05:30
|
|
|
if shortcite and shorteralg == 'EMPTY':
|
2013-06-01 15:18:52 +05:30
|
|
|
field_type_short = field_type + '_SHORT_VERSION'
|
|
|
|
if field_type_short in FIELDTYPEMAP:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
FIELDTYPEMAP[field_type_short] = (index, field_descr + ' (Short)')
|
|
|
|
index += 1
|
2013-05-27 00:58:57 +05:30
|
|
|
|
|
|
|
#now generate the python code we need in source attr types
|
2013-05-28 01:15:30 +05:30
|
|
|
code = " #following fields are generated with evidencefieldgenerator.py\n" \
|
|
|
|
" #the index starts at 100!\n"
|
2013-05-27 00:58:57 +05:30
|
|
|
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
|
2013-05-28 15:44:16 +05:30
|
|
|
code = " #SRCTEMPLATE has some predefined values which map to citation styles\n"
|
2013-05-27 00:58:57 +05:30
|
|
|
|
2013-05-29 20:26:38 +05:30
|
|
|
datamap = " _SRCTEMPLATEVAL_MAP = [\n"\
|
|
|
|
" (UNKNOWN, _('Unknown'), 'Unknown'),\n"
|
2013-05-27 00:58:57 +05:30
|
|
|
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
|
2013-05-29 20:26:38 +05:30
|
|
|
datamap += " (" + source_type + ", " + TYPE2CITEMAP[source_type]['descrcode'] \
|
|
|
|
+", '" + source_type+ "'),\n"
|
2013-05-27 00:58:57 +05:30
|
|
|
|
|
|
|
code += '\n # Localization of the different source types\n'\
|
|
|
|
+ datamap + ' ]\n'
|
|
|
|
|
2013-05-28 01:56:48 +05:30
|
|
|
code += "\n #templates for the source types defined\n"\
|
|
|
|
" # F: Full reference\n"\
|
|
|
|
" # S: Short reference (if F used once, use S afterwards)\n" \
|
|
|
|
" # L: List reference (for in bibliography list)\n"
|
2013-05-27 00:58:57 +05:30
|
|
|
code += ' EVIDENCETEMPLATES = {\n'
|
|
|
|
for source_type in allkeys:
|
2013-05-28 15:47:31 +05:30
|
|
|
code += " " + source_type + ": {\n"
|
2013-06-02 15:38:26 +05:30
|
|
|
for val in ['REF_TYPE_L', 'REF_TYPE_F', 'REF_TYPE_S']:
|
|
|
|
code += " " + val + ": [\n"
|
2013-05-27 00:58:57 +05:30
|
|
|
for field in TYPE2CITEMAP[source_type][val]:
|
|
|
|
# field is tuple (row[LDELCOL], field_type, row[RDELCOL], row[STYLECOL]
|
2013-06-01 15:18:52 +05:30
|
|
|
# , private, optional, shorteralg, gedcommap)
|
|
|
|
code += " ('"+ field[0] + "', " + field[1] + ", '"\
|
|
|
|
+ field[2] + "', " + field[3] + ", " + field[4] + ", "\
|
|
|
|
+ field[5] + ", " + field[6] + ", " + field[7] + "),\n"
|
2013-05-27 00:58:57 +05:30
|
|
|
code += " ],\n"
|
|
|
|
code += " },\n"
|
|
|
|
code += " }\n"
|
|
|
|
|
|
|
|
with open('srcattrtype_extraevidence.py', 'wb') as srcattrfile:
|
|
|
|
srcattrfile.write(code)
|