diff --git a/ChangeLog b/ChangeLog index 72a9e40c4..bfafcce76 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2007-09-07 Don Allingham + * src/GrampsDb/_DbBase.py: pylint + * src/GrampsDbUtils/_GedcomStageOne.py: added + * src/GrampsDbUtils/_GedcomParse.py: pylint + * src/GrampsDbUtils/_ReadGedcom.py: pylint + * src/GrampsDbUtils/_ReadXML.py: pylint + * src/GrampsDbUtils/_GedcomLex.py: pylint + * src/GrampsDbUtils/_WriteXML.py: pylint + * src/GrampsDbUtils/_GedcomUtils.py: pylint + * src/GrampsDbUtils/_ProxyDbBase.py: pylint + * src/GrampsDbUtils/Makefile.am: added _GedcomStageOne.py + * po/POTFILES.in: added _GedcomStageOne.py + 2007-09-07 Zsolt Foldvari * src/docgen/GtkPrint.py: Small fixes. Enable graphical reports. diff --git a/po/POTFILES.in b/po/POTFILES.in index 789eec98d..38dac81d1 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -209,6 +209,7 @@ src/GrampsDb/__init__.py # GrampsDbUtils package src/GrampsDbUtils/_Backup.py src/GrampsDbUtils/_GedcomInfo.py +src/GrampsDbUtils/_GedcomStageOne.py #src/GrampsDbUtils/_GedcomLex.py src/GrampsDbUtils/_GedcomParse.py src/GrampsDbUtils/_GedcomTokens.py diff --git a/src/GrampsDb/_DbBase.py b/src/GrampsDb/_DbBase.py index d853c34a5..7d4dc9b35 100644 --- a/src/GrampsDb/_DbBase.py +++ b/src/GrampsDb/_DbBase.py @@ -40,12 +40,6 @@ class DbBase: but all data marked private will be hidden from the user. """ - def __init__(self,db): - """ - Creates a new PrivateProxyDb instance. - """ - raise NotImplementedError - def set_prefixes(self, person, media, family, source, place, event, repository, note): raise NotImplementedError diff --git a/src/GrampsDbUtils/Makefile.am b/src/GrampsDbUtils/Makefile.am index eff8ca1b2..bed94eed7 100644 --- a/src/GrampsDbUtils/Makefile.am +++ b/src/GrampsDbUtils/Makefile.am @@ -11,6 +11,7 @@ pkgdata_PYTHON = \ _GedcomInfo.py\ _GedcomLex.py\ _GedcomParse.py\ + _GedcomStageOne.py\ _GedcomTokens.py\ _GedcomUtils.py\ _GrampsDbWRFactories.py\ diff --git a/src/GrampsDbUtils/_GedcomLex.py b/src/GrampsDbUtils/_GedcomLex.py index 401041a6f..9d7645906 100644 --- a/src/GrampsDbUtils/_GedcomLex.py +++ b/src/GrampsDbUtils/_GedcomLex.py @@ -38,7 +38,7 @@ import re #------------------------------------------------------------------------- from _GedcomInfo import * -from _GedcomTokens import * +import _GedcomTokens as GedcomTokens import RelLib from DateHandler._DateParser import DateParser @@ -74,7 +74,7 @@ for __val in personalConstantAttributes.keys(): #------------------------------------------------------------------------- # -# GedLine +# # #------------------------------------------------------------------------- @@ -102,7 +102,7 @@ SEX_MAP = { #----------------------------------------------------------------------- # -# GedLine - represents a tokenized version of a GEDCOM line +# # #----------------------------------------------------------------------- class GedcomDateParser(DateParser): @@ -157,7 +157,7 @@ class GedLine: if self.level == 0: if self.token_text and self.token_text[0] == '@' \ and self.token_text[-1] == '@': - self.token = TOKEN_ID + self.token = GedcomTokens.TOKEN_ID self.token_text = self.token_text[1:-1] self.data = self.data.strip() else: @@ -188,24 +188,24 @@ class GedLine: """ token = GED2GRAMPS.get(self.token_text) if token: - event = RelLib.Event() - event.set_description(self.data) - event.set_type(token) - self.token = TOKEN_GEVENT - self.data = event + event = RelLib.Event() + event.set_description(self.data) + event.set_type(token) + self.token = GedcomTokens.TOKEN_GEVENT + self.data = event else: token = GED2ATTR.get(self.token_text) if token: attr = RelLib.Attribute() attr.set_value(self.data) attr.set_type(token) - self.token = TOKEN_ATTR + self.token = GedcomTokens.TOKEN_ATTR self.data = attr def calc_note(self): gid = self.data.strip() if len(gid) > 2 and gid[0] == '@' and gid[-1] == '@': - self.token = TOKEN_RNOTE + self.token = GedcomTokens.TOKEN_RNOTE self.data = gid[1:-1] def calc_nchi(self): @@ -213,14 +213,14 @@ class GedLine: attr.set_value(self.data) attr.set_type(RelLib.AttributeType.NUM_CHILD) self.data = attr - self.token = TOKEN_ATTR + self.token = GedcomTokens.TOKEN_ATTR def calc_attr(self): attr = RelLib.Attribute() attr.set_value(self.data) attr.set_type((RelLib.AttributeType.CUSTOM, self.token_text)) self.data = attr - self.token = TOKEN_ATTR + self.token = GedcomTokens.TOKEN_ATTR def __repr__(self): return "%d: %d (%d:%s) %s" % (self.line, self.level, self.token, @@ -233,14 +233,14 @@ class GedLine: # #------------------------------------------------------------------------- MAP_DATA = { - TOKEN_UNKNOWN : GedLine.calc_unknown, - TOKEN_DATE : GedLine.calc_date, - TOKEN_SEX : GedLine.calc_sex, - TOKEN_NOTE : GedLine.calc_note, - TOKEN_NCHI : GedLine.calc_nchi, - TOKEN__STAT : GedLine.calc_attr, - TOKEN__UID : GedLine.calc_attr, - TOKEN_AFN : GedLine.calc_attr, + GedcomTokens.TOKEN_UNKNOWN : GedLine.calc_unknown, + GedcomTokens.TOKEN_DATE : GedLine.calc_date, + GedcomTokens.TOKEN_SEX : GedLine.calc_sex, + GedcomTokens.TOKEN_NOTE : GedLine.calc_note, + GedcomTokens.TOKEN_NCHI : GedLine.calc_nchi, + GedcomTokens.TOKEN__STAT : GedLine.calc_attr, + GedcomTokens.TOKEN__UID : GedLine.calc_attr, + GedcomTokens.TOKEN_AFN : GedLine.calc_attr, } #------------------------------------------------------------------------- @@ -329,8 +329,8 @@ class Reader: self.cnt = 0 self.index = 0 self.func_map = { - TOKEN_CONT : self.__fix_token_cont, - TOKEN_CONC : self.__fix_token_conc, + GedcomTokens.TOKEN_CONT : self.__fix_token_cont, + GedcomTokens.TOKEN_CONC : self.__fix_token_conc, } def readline(self): @@ -343,7 +343,7 @@ class Reader: def __fix_token_cont(self, data): line = self.current_list[0] - new_value = line[2]+'\n'+data[2] + new_value = line[2] + '\n' + data[2] self.current_list[0] = (line[0], line[1], new_value, line[3], line[4]) def __fix_token_conc(self, data): @@ -365,7 +365,7 @@ class Reader: except: continue - token = tokens.get(line[1], TOKEN_UNKNOWN) + token = tokens.get(line[1], GedcomTokens.TOKEN_UNKNOWN) data = (level, token, line[2], line[1], self.index) func = self.func_map.get(data[1]) diff --git a/src/GrampsDbUtils/_GedcomParse.py b/src/GrampsDbUtils/_GedcomParse.py index 678605719..fd60e8de2 100644 --- a/src/GrampsDbUtils/_GedcomParse.py +++ b/src/GrampsDbUtils/_GedcomParse.py @@ -93,7 +93,6 @@ import os import sys import re import time -import codecs from gettext import gettext as _ #------------------------------------------------------------------------ @@ -112,27 +111,19 @@ LOG = logging.getLogger(".GedcomImport") import Errors import RelLib from BasicUtils import name_displayer, UpdateCallback -import Utils import Mime import LdsUtils import Utils -from _GedcomInfo import * from _GedcomTokens import * -from _GedcomLex import Reader -from _GedcomChar import * +import _GedcomInfo as GedcomInfo import _GedcomUtils as GedcomUtils +import _GedcomLex as GedcomLex +import _GedcomChar as GedcomChar from GrampsDb._GrampsDbConst import EVENT_KEY -try: - import Config - DEFAULT_SOURCE = Config.get(Config.DEFAULT_SOURCE) -except: - LOG.warn("No Config module available using defaults.") - DEFAULT_SOURCE = False - #------------------------------------------------------------------------- # # Address/Place constants @@ -144,9 +135,6 @@ ADDR3_RE = re.compile('(.+)([\n\r]+)(.+)\s*, (.+)') TRUNC_MSG = _("Your GEDCOM file is corrupted. " "It appears to have been truncated.") -BAD_UTF16 = _("Your GEDCOM file is corrupted. " - "The file appears to be encoded using the UTF16 " - "character set, but is missing the BOM marker.") #------------------------------------------------------------------------- # @@ -216,14 +204,14 @@ MEDIA_MAP = { # #------------------------------------------------------------------------- GED_2_GRAMPS = {} -for _val in personalConstantEvents.keys(): - _key = personalConstantEvents[_val] +for _val in GedcomInfo.personalConstantEvents.keys(): + _key = GedcomInfo.personalConstantEvents[_val] if _key != "": GED_2_GRAMPS[_key] = _val GED_2_FAMILY = {} -for _val in familyConstantEvents.keys(): - _key = familyConstantEvents[_val] +for _val in GedcomInfo.familyConstantEvents.keys(): + _key = GedcomInfo.familyConstantEvents[_val] if _key != "": GED_2_FAMILY[_key] = _val @@ -239,142 +227,20 @@ PERSON_RE = re.compile(r"\s*\d+\s+\@(\S+)\@\s+INDI(.*)$") #------------------------------------------------------------------------- # -# is_xref_value +# find_from_handle # #------------------------------------------------------------------------- -def is_xref_value(value): +def find_from_handle(gramps_id, table): """ - Returns True if value is in the form of a XREF value. We assume that - if we have a leading '@' character, then we are okay. + Finds a handle corresponding the the specified GRAMPS ID. The passed + table contains the mapping. If the value is found, we return it, + otherwise we create a new handle, store it, and return it. """ - return value and value[0] == '@' - -#------------------------------------------------------------------------- -# -# StageOne -# -#------------------------------------------------------------------------- -class StageOne: - """ - The StageOne parser scans the file quickly, looking for a few things. This - includes: - - 1. Character set encoding - 2. Number of people and families in the list - 3. Child to family references, since Ancestry.com creates GEDCOM files - without the FAMC references. - """ - def __init__(self, ifile): - self.ifile = ifile - self.famc = {} - self.fams = {} - self.enc = "" - self.pcnt = 0 - self.lcnt = 0 - - def __detect_file_decoder(self, input_file): - """ - Detects the file encoding of the file by looking for a BOM - (byte order marker) in the GEDCOM file. If we detect a UTF-16 - encoded file, we must connect to a wrapper using the codecs - package. - """ - line = input_file.read(2) - if line == "\xef\xbb": - input_file.read(1) - self.enc = "UTF8" - return input_file - elif line == "\xff\xfe": - self.enc = "UTF16" - input_file.seek(0) - return codecs.EncodedFile(input_file, 'utf8', 'utf16') - elif line[0] == "\x00" or line[1] == "\x00": - raise Errors.GedcomError(BAD_UTF16) - else: - input_file.seek(0) - return input_file - - def parse(self): - """ - Parse the input file. - """ - current = "" - - reader = self.__detect_file_decoder(self.ifile) - - for line in reader: - line = line.strip() - if not line: - continue - self.lcnt += 1 - - data = line.split(None, 2) + [''] - try: - (level, key, value) = data[:3] - value = value.strip() - level = int(level) - key = key.strip() - except: - LOG.warn(_("Invalid line %d in GEDCOM file.") % self.lcnt) - continue - - if level == 0 and key[0] == '@': - if value == ("FAM", "FAMILY") : - current = key.strip()[1:-1] - elif value == ("INDI", "INDIVIDUAL"): - self.pcnt += 1 - elif key in ("HUSB", "HUSBAND", "WIFE") and is_xref_value(value): - value = value[1:-1] - if self.fams.has_key(value): - self.fams[value].append(current) - else: - self.fams[value] = [current] - elif key in ("CHIL", "CHILD") and is_xref_value(value): - value = value[1:-1] - if self.famc.has_key(value): - self.famc[value].append(current) - else: - self.famc[value] = [current] - elif key == 'CHAR' and not self.enc: - assert(type(value) == str or type(value) == unicode) - self.enc = value - - def get_famc_map(self): - """ - Returns the Person to Child Family map - """ - return self.famc - - def get_fams_map(self): - """ - Returns the Person to Family map (where the person is a spouse) - """ - return self.fams - - def get_encoding(self): - """ - Returns the detected encoding - """ - return self.enc.upper() - - def set_encoding(self, enc): - """ - Forces the encoding - """ - assert(type(enc) == str or type(enc) == unicode) - self.enc = enc - - def get_person_count(self): - """ - Returns the number of INDI records found - """ - return self.pcnt - - def get_line_count(self): - """ - Returns the number of lines in the file - """ - return self.lcnt + intid = table.get(gramps_id) + if not intid: + intid = Utils.create_id() + table[gramps_id] = intid + return intid #------------------------------------------------------------------------- # @@ -390,11 +256,12 @@ class GedcomParser(UpdateCallback): SyntaxError = "Syntax Error" BadFile = "Not a GEDCOM file" - def __init__(self, dbase, ifile, filename, callback, stage_one): + def __init__(self, dbase, ifile, filename, callback, stage_one, default_source): UpdateCallback.__init__(self, callback) self.set_total(stage_one.get_line_count()) self.repo2id = {} + self.trans = None self.maxpeople = stage_one.get_person_count() self.dbase = dbase self.emapper = GedcomUtils.IdFinder(dbase.get_gramps_ids(EVENT_KEY), @@ -405,9 +272,9 @@ class GedcomParser(UpdateCallback): self.place_parser = GedcomUtils.PlaceParser() self.inline_srcs = {} self.media_map = {} - self.gedmap = GedcomInfoDB() + self.gedmap = GedcomInfo.GedcomInfoDB() self.gedsource = self.gedmap.get_from_source_tag('GEDCOM 5.5') - self.use_def_src = DEFAULT_SOURCE + self.use_def_src = default_source if self.use_def_src: self.def_src = RelLib.Source() fname = os.path.basename(filename).split('\\')[-1] @@ -416,6 +283,7 @@ class GedcomParser(UpdateCallback): self.dir_path = os.path.dirname(filename) self.is_ftw = False self.is_ancestry_com = False + self.groups = None self.pid_map = GedcomUtils.IdMapper( self.dbase.id_trans, @@ -920,15 +788,15 @@ class GedcomParser(UpdateCallback): enc = stage_one.get_encoding() if enc == "ANSEL": - rdr = AnselReader(ifile) + rdr = GedcomChar.AnselReader(ifile) elif enc in ("UTF-8", "UTF8"): - rdr = UTF8Reader(ifile) + rdr = GedcomChar.UTF8Reader(ifile) elif enc in ("UTF-16", "UTF16", "UNICODE"): - rdr = UTF16Reader(ifile) + rdr = GedcomChar.UTF16Reader(ifile) else: - rdr = AnsiReader(ifile) + rdr = GedcomChar.AnsiReader(ifile) - self.lexer = Reader(rdr) + self.lexer = GedcomLex.Reader(rdr) self.filename = filename self.backoff = False @@ -936,7 +804,7 @@ class GedcomParser(UpdateCallback): self.geddir = os.path.dirname(fullpath) self.error_count = 0 - amap = personalConstantAttributes + amap = GedcomInfo.personalConstantAttributes self.attrs = amap.values() self.gedattr = {} @@ -951,7 +819,6 @@ class GedcomParser(UpdateCallback): no_magic = self.maxpeople < 1000 self.trans = self.dbase.transaction_begin("", not use_trans, no_magic) - self.debug = False self.dbase.disable_signals() self.__parse_header_head() self.__parse_header_source() @@ -972,41 +839,29 @@ class GedcomParser(UpdateCallback): self.dbase.enable_signals() self.dbase.request_rebuild() - def __find_from_handle(self, gramps_id, table): - """ - Finds a handle corresponding the the specified GRAMPS ID. The passed - table contains the mapping. If the value is found, we return it, - otherwise we create a new handle, store it, and return it. - """ - intid = table.get(gramps_id) - if not intid: - intid = Utils.create_id() - table[gramps_id] = intid - return intid - def __find_person_handle(self, gramps_id): """ Returns the database handle associated with the person's GRAMPS ID """ - return self.__find_from_handle(gramps_id, self.gid2id) + return find_from_handle(gramps_id, self.gid2id) def __find_family_handle(self, gramps_id): """ Returns the database handle associated with the family's GRAMPS ID """ - return self.__find_from_handle(gramps_id, self.fid2id) + return find_from_handle(gramps_id, self.fid2id) def __find_object_handle(self, gramps_id): """ Returns the database handle associated with the media object's GRAMPS ID """ - return self.__find_from_handle(gramps_id, self.oid2id) + return find_from_handle(gramps_id, self.oid2id) def __find_note_handle(self, gramps_id): """ Returns the database handle associated with the media object's GRAMPS ID """ - return self.__find_from_handle(gramps_id, self.nid2id) + return find_from_handle(gramps_id, self.nid2id) def __find_or_create_person(self, gramps_id): """ @@ -1019,7 +874,7 @@ class GedcomParser(UpdateCallback): if self.dbase.has_person_handle(intid): person.unserialize(self.dbase.get_raw_person_data(intid)) else: - intid = self.__find_from_handle(gramps_id, self.gid2id) + intid = find_from_handle(gramps_id, self.gid2id) person.set_handle(intid) person.set_gramps_id(gramps_id) return person @@ -1035,7 +890,7 @@ class GedcomParser(UpdateCallback): if self.dbase.has_family_handle(intid): family.unserialize(self.dbase.get_raw_family_data(intid)) else: - intid = self.__find_from_handle(gramps_id, self.fid2id) + intid = find_from_handle(gramps_id, self.fid2id) family.set_handle(intid) family.set_gramps_id(gramps_id) return family @@ -1051,7 +906,7 @@ class GedcomParser(UpdateCallback): if self.dbase.has_object_handle(intid): obj.unserialize(self.dbase.get_raw_object_data(intid)) else: - intid = self.__find_from_handle(gramps_id, self.oid2id) + intid = find_from_handle(gramps_id, self.oid2id) obj.set_handle(intid) obj.set_gramps_id(gramps_id) return obj @@ -1067,7 +922,7 @@ class GedcomParser(UpdateCallback): if self.dbase.has_source_handle(intid): obj.unserialize(self.dbase.get_raw_source_data(intid)) else: - intid = self.__find_from_handle(gramps_id, self.sid2id) + intid = find_from_handle(gramps_id, self.sid2id) obj.set_handle(intid) obj.set_gramps_id(gramps_id) return obj @@ -1092,7 +947,7 @@ class GedcomParser(UpdateCallback): if self.dbase.has_repository_handle(intid): repository.unserialize(self.dbase.get_raw_repository_data(intid)) else: - intid = self.__find_from_handle(gramps_id, self.rid2id) + intid = find_from_handle(gramps_id, self.rid2id) repository.set_handle(intid) repository.set_gramps_id(gramps_id) if need_commit: @@ -1119,7 +974,7 @@ class GedcomParser(UpdateCallback): if self.dbase.has_note_handle(intid): note.unserialize(self.dbase.get_raw_note_data(intid)) else: - intid = self.__find_from_handle(gramps_id, self.nid2id) + intid = find_from_handle(gramps_id, self.nid2id) note.set_handle(intid) note.set_gramps_id(gramps_id) if need_commit: @@ -1225,7 +1080,7 @@ class GedcomParser(UpdateCallback): self.backoff = False return self.groups - def __not_recognized(self, level): + def __not_recognized(self, line, level): """ Prints a message when an undefined token is found. All subordinate items to the current item are ignored. @@ -1233,8 +1088,7 @@ class GedcomParser(UpdateCallback): @param level: Current level in the file @type level: int """ - text = self.groups.line - msg = _("Line %d was not understood, so it was ignored.") % text + msg = _("Line %d was not understood, so it was ignored.") % line.line self.__warn(msg) self.error_count += 1 self.__skip_subordinate_levels(level) @@ -1260,7 +1114,7 @@ class GedcomParser(UpdateCallback): try: line = self.__get_next_line() if line and line.token != TOKEN_TRLR: - self.__not_recognized(0) + self.__not_recognized(line, 0) except TypeError: return @@ -1325,7 +1179,7 @@ class GedcomParser(UpdateCallback): pass self.__parse_inline_note(line, 1) else: - self.__not_recognized(1) + self.__not_recognized(line, 1) def __parse_level(self, state, __map, default): """ @@ -1340,8 +1194,6 @@ class GedcomParser(UpdateCallback): return else: func = __map.get(line.token, default) - if self.debug: - print line, func func(line, state) def __undefined(self, line, state): @@ -1351,7 +1203,7 @@ class GedcomParser(UpdateCallback): @param state: The current state @type state: CurrentState """ - self.__not_recognized(state.level+1) + self.__not_recognized(line, state.level+1) #---------------------------------------------------------------------- # @@ -1390,29 +1242,28 @@ class GedcomParser(UpdateCallback): # find the person real_id = self.pid_map[line.token_text] - self.person = self.__find_or_create_person(real_id) + person = self.__find_or_create_person(real_id) # set up the state for the parsing - state = GedcomUtils.CurrentState(person=self.person, level=1) + state = GedcomUtils.CurrentState(person=person, level=1) # Ancestry.com GEDCOM files are massively broken, not providing # the FAMC and FAMS values for a person if self.is_ancestry_com: - self.map_ancestry_com(line.token_text.strip()) + self.map_ancestry_com(person, line.token_text.strip()) # do the actual parsing self.__parse_level(state, self.indi_parse_tbl, self.__person_event) # Add the default reference if no source has found - self.__add_default_source(self.person) + self.__add_default_source(person) # commit the person to the database - if self.person.change: - self.dbase.commit_person(self.person, self.trans, - change_time=state.person.change) + if person.change: + self.dbase.commit_person(person, self.trans, + change_time=state.person.change) else: - self.dbase.commit_person(self.person, self.trans) - del self.person + self.dbase.commit_person(person, self.trans) def __person_sour(self, line, state): """ @@ -2208,7 +2059,7 @@ class GedcomParser(UpdateCallback): @param state: The current state @type state: CurrentState """ - value = self.extract_temple(line) + value = self.__extract_temple(line) if value: state.lds_ord.set_temple(value) @@ -2296,7 +2147,7 @@ class GedcomParser(UpdateCallback): @param state: The current state @type state: CurrentState """ - status = lds_status.get(line.data, RelLib.LdsOrd.STATUS_NONE) + status = GedcomInfo.lds_status.get(line.data, RelLib.LdsOrd.STATUS_NONE) state.lds_ord.set_status(status) def __person_famc(self, line, state): @@ -2788,7 +2639,7 @@ class GedcomParser(UpdateCallback): @type state: CurrentState """ if line.data and line.data[0] == '@': - self.__not_recognized(state.level) + self.__not_recognized(line, state.level) else: (form, filename, title, note) = self.__obje(state.level) self.build_media_object(state.family, form, filename, title, note) @@ -2986,7 +2837,7 @@ class GedcomParser(UpdateCallback): @type state: CurrentState """ if line.data and line.data[0] == '@': - self.__not_recognized(state.level) + self.__not_recognized(line, state.level) else: (form, filename, title, note) = self.__obje(state.level) self.build_media_object(state.event, form, filename, title, note) @@ -3093,7 +2944,7 @@ class GedcomParser(UpdateCallback): @type state: CurrentState """ if line.data and line.data[0] == '@': - self.__not_recognized(state.level) + self.__not_recognized(line, state.level) else: (form, filename, title, note) = self.__obje(state.level) self.build_media_object(state.place, form, filename, title, note) @@ -3323,10 +3174,8 @@ class GedcomParser(UpdateCallback): @type state: CurrentState """ if line.data and line.data[0] == "@": - """ - n _WITN @@ - +1 TYPE - """ + # n _WITN @@ + # +1 TYPE assert( state.event.handle) # event handle is required to be set wit = self.__find_or_create_person(self.pid_map[line.data]) event_ref = RelLib.EventRef() @@ -3337,17 +3186,16 @@ class GedcomParser(UpdateCallback): break elif line.token == TOKEN_TYPE: if line.data in ("WITNESS_OF_MARRIAGE"): - r = RelLib.EventRoleType(RelLib.EventRoleType.WITNESS) + role = RelLib.EventRoleType( + RelLib.EventRoleType.WITNESS) else: - r = RelLib.EventRoleType((RelLib.EventRoleType.CUSTOM, - line.data)) - event_ref.set_role(r) + role = RelLib.EventRoleType( + (RelLib.EventRoleType.CUSTOM, line.data)) + event_ref.set_role(role) wit.add_event_ref(event_ref) self.dbase.commit_person(wit, self.trans) else: - """ - n _WITN - """ + # n _WITN attr = RelLib.Attribute() attr.set_type(RelLib.AttributeType.WITNESS) attr.set_value(line.data) @@ -3598,7 +3446,7 @@ class GedcomParser(UpdateCallback): @type state: CurrentState """ if line.data and line.data[0] == '@': - self.__not_recognized(state.level) + self.__not_recognized(line, state.level) else: src = self.dbase.get_source_from_handle(state.handle) (form, filename, title, note) = self.__obje(state.level) @@ -3696,7 +3544,7 @@ class GedcomParser(UpdateCallback): @type state: CurrentState """ if line.data and line.data[0] == '@': - self.__not_recognized(state.level) + self.__not_recognized(line, state.level) else: (form, filename, title, note) = self.__obje(state.level+1) self.build_media_object(state.source, form, filename, title, note) @@ -3717,7 +3565,7 @@ class GedcomParser(UpdateCallback): @param state: The current state @type state: CurrentState """ - self.__not_recognized(state.level+1) + self.__not_recognized(line, state.level+1) def __source_repo(self, line, state): """ @@ -4183,7 +4031,7 @@ class GedcomParser(UpdateCallback): state.location = RelLib.Location() self.__parse_note(line, state.event, state.level+1) - def map_ancestry_com(self, original_gid): + def map_ancestry_com(self, person, original_gid): """ GEDCOM files created by Ancestry.com for some reason do not include the FAMC and FAMS mappings in the INDI record. If we don't fix this, @@ -4200,12 +4048,12 @@ class GedcomParser(UpdateCallback): for fams_id in self.fams_map.get(original_gid, []): mapped_id = self.fid_map[fams_id] fams_handle = self.__find_family_handle(mapped_id) - self.person.add_family_handle(fams_handle) + person.add_family_handle(fams_handle) for famc_id in self.famc_map.get(original_gid, []): mapped_id = self.fid_map[famc_id] famc_handle = self.__find_family_handle(mapped_id) - self.person.add_parent_family_handle(famc_handle) + person.add_parent_family_handle(famc_handle) def __optional_note(self, line, state): """ @@ -4432,7 +4280,7 @@ class GedcomParser(UpdateCallback): elif line.token == TOKEN_NOTE: self.__skip_subordinate_levels(level+1) else: - self.__not_recognized(level+1) + self.__not_recognized(line, level+1) # Attempt to convert the values to a valid change time if tstr: @@ -4542,7 +4390,7 @@ class GedcomParser(UpdateCallback): event_ref.set_reference_handle(event.handle) return event_ref - def extract_temple(self, line): + def __extract_temple(self, line): def get_code(code): if LdsUtils.Temples.is_valid_code(code): return code diff --git a/src/GrampsDbUtils/_GedcomStageOne.py b/src/GrampsDbUtils/_GedcomStageOne.py new file mode 100644 index 000000000..d8ddbaa4c --- /dev/null +++ b/src/GrampsDbUtils/_GedcomStageOne.py @@ -0,0 +1,206 @@ +# +# Gramps - a GTK+/GNOME based genealogy program +# +# Copyright (C) 2000-2007 Donald N. Allingham +# +# 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: _ReadGedcom.py 8032 2007-02-03 17:11:05Z hippy $ + +""" +Import from GEDCOM +""" + +__revision__ = "$Revision: $" +__author__ = "Don Allingham" + +#------------------------------------------------------------------------- +# +# standard python modules +# +#------------------------------------------------------------------------- +import codecs +from gettext import gettext as _ + +#------------------------------------------------------------------------- +# +# GRAMPS modules +# +#------------------------------------------------------------------------- +import Errors + +#------------------------------------------------------------------------ +# +# Set up logging +# +#------------------------------------------------------------------------ +import logging +LOG = logging.getLogger(".GedcomImport") + +#------------------------------------------------------------------------- +# +# Constants +# +#------------------------------------------------------------------------- +BAD_UTF16 = _("Your GEDCOM file is corrupted. " + "The file appears to be encoded using the UTF16 " + "character set, but is missing the BOM marker.") + +#------------------------------------------------------------------------- +# +# is_xref_value +# +#------------------------------------------------------------------------- +def is_xref_value(value): + """ + Returns True if value is in the form of a XREF value. We assume that + if we have a leading '@' character, then we are okay. + """ + return value and value[0] == '@' + +#------------------------------------------------------------------------- +# +# add_to_list +# +#------------------------------------------------------------------------- +def add_to_list(table, key, value): + """ + Adds the value to the table entry associated with key. If the entry + does not exist, it is added. + """ + if table.has_key(key): + table[key].append(value) + else: + table[key] = [value] + +#------------------------------------------------------------------------- +# +# StageOne +# +#------------------------------------------------------------------------- +class StageOne: + """ + The StageOne parser scans the file quickly, looking for a few things. This + includes: + + 1. Character set encoding + 2. Number of people and families in the list + 3. Child to family references, since Ancestry.com creates GEDCOM files + without the FAMC references. + """ + def __init__(self, ifile): + self.ifile = ifile + self.famc = {} + self.fams = {} + self.enc = "" + self.pcnt = 0 + self.lcnt = 0 + + def __detect_file_decoder(self, input_file): + """ + Detects the file encoding of the file by looking for a BOM + (byte order marker) in the GEDCOM file. If we detect a UTF-16 + encoded file, we must connect to a wrapper using the codecs + package. + """ + line = input_file.read(2) + if line == "\xef\xbb": + input_file.read(1) + self.enc = "UTF8" + return input_file + elif line == "\xff\xfe": + self.enc = "UTF16" + input_file.seek(0) + return codecs.EncodedFile(input_file, 'utf8', 'utf16') + elif line[0] == "\x00" or line[1] == "\x00": + raise Errors.GedcomError(BAD_UTF16) + else: + input_file.seek(0) + return input_file + + def parse(self): + """ + Parse the input file. + """ + current_family_id = "" + + reader = self.__detect_file_decoder(self.ifile) + + for line in reader: + line = line.strip() + if not line: + continue + self.lcnt += 1 + + data = line.split(None, 2) + [''] + try: + (level, key, value) = data[:3] + value = value.strip() + level = int(level) + key = key.strip() + except: + LOG.warn(_("Invalid line %d in GEDCOM file.") % self.lcnt) + continue + + if level == 0 and key[0] == '@': + if value == ("FAM", "FAMILY") : + current_family_id = key.strip()[1:-1] + elif value == ("INDI", "INDIVIDUAL"): + self.pcnt += 1 + elif key in ("HUSB", "HUSBAND", "WIFE") and is_xref_value(value): + add_to_list(self.fams, value[1:-1], current_family_id) + elif key in ("CHIL", "CHILD") and is_xref_value(value): + add_to_list(self.famc, value[1:-1], current_family_id) + elif key == 'CHAR' and not self.enc: + assert(type(value) == str or type(value) == unicode) + self.enc = value + + def get_famc_map(self): + """ + Returns the Person to Child Family map + """ + return self.famc + + def get_fams_map(self): + """ + Returns the Person to Family map (where the person is a spouse) + """ + return self.fams + + def get_encoding(self): + """ + Returns the detected encoding + """ + return self.enc.upper() + + def set_encoding(self, enc): + """ + Forces the encoding + """ + assert(type(enc) == str or type(enc) == unicode) + self.enc = enc + + def get_person_count(self): + """ + Returns the number of INDI records found + """ + return self.pcnt + + def get_line_count(self): + """ + Returns the number of lines in the file + """ + return self.lcnt diff --git a/src/GrampsDbUtils/_GedcomUtils.py b/src/GrampsDbUtils/_GedcomUtils.py index b506255a2..e4e91f6a9 100644 --- a/src/GrampsDbUtils/_GedcomUtils.py +++ b/src/GrampsDbUtils/_GedcomUtils.py @@ -18,10 +18,16 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # -import re +""" +Support classes to simplify GEDCOM importing +""" +import re import RelLib +NAME_RE = re.compile(r"/?([^/]*)(/([^/]*)(/([^/]*))?)?") +SURNAME_RE = re.compile(r"/([^/]*)/([^/]*)") + #------------------------------------------------------------------------- # # CurrentState @@ -35,7 +41,6 @@ class CurrentState: """ Initializes the object """ - self.note = "" self.name_cnt = 0 self.person = person self.level = level @@ -55,15 +60,19 @@ class CurrentState: """ self.__dict__[name] = value - def add_to_note(self, text): - self.note += text - - def get_text(self): - return self.note - +#------------------------------------------------------------------------- +# +# PlaceParser +# +#------------------------------------------------------------------------- class PlaceParser: + """ + Provides the ability to parse GEDCOM FORM statements for places, and + the parse the line of text, mapping the text components to Location + values based of the FORM statement. + """ - field_map = { + __field_map = { 'addr' : RelLib.Location.set_street, 'subdivision' : RelLib.Location.set_street, 'addr1' : RelLib.Location.set_street, @@ -81,27 +90,44 @@ class PlaceParser: } def __init__(self, line=None): - self.pf = [] + self.parse_function = [] - if line: - self.parse_form(line) + if line: + self.parse_form(line) def parse_form(self, line): + """ + Parses the GEDCOM PLAC.FORM into a list of function + pointers (if possible). It does this my mapping the text strings + (separated by commas) to the corresponding RelLib.Location + method via the __field_map variable + """ for item in line.data.split(','): item = item.lower().strip() - fcn = self.field_map.get(item, lambda x, y: None) - self.pf.append(fcn) + fcn = self.__field_map.get(item, lambda x, y: None) + self.parse_function.append(fcn) def load_place(self, place, text): - items = [item.strip() for item in text.split(',')] - if len(items) != len(self.pf): - return - loc = place.get_main_location() - index = 0 - for item in items: - self.pf[index](loc, item) - index += 1 + """ + Takes the text string representing a place, splits it into + its subcomponents (comma separated), and calls the approriate + function based of its position, depending on the parsed value + from the FORM statement. + """ + items = [item.strip() for item in text.split(',')] + if len(items) != len(self.parse_function): + return + loc = place.get_main_location() + index = 0 + for item in items: + self.parse_function[index](loc, item) + index += 1 +#------------------------------------------------------------------------- +# +# IdFinder +# +#------------------------------------------------------------------------- class IdFinder: """ Provides method of finding the next available ID. @@ -130,6 +156,11 @@ class IdFinder: self.index += 1 return index +#------------------------------------------------------------------------- +# +# IdMapper +# +#------------------------------------------------------------------------- class IdMapper: def __init__(self, trans, find_next, translate): @@ -168,16 +199,15 @@ class IdMapper: # Support functions # #------------------------------------------------------------------------ - -NAME_RE = re.compile(r"/?([^/]*)(/([^/]*)(/([^/]*))?)?") -SURNAME_RE = re.compile(r"/([^/]*)/([^/]*)") - def parse_name_personal(text): + """ + Parses a GEDCOM NAME value into an Name structure + """ name = RelLib.Name() - m = SURNAME_RE.match(text) - if m: - names = m.groups() + match = SURNAME_RE.match(text) + if match: + names = match.groups() name.set_first_name(names[1].strip()) name.set_surname(names[0].strip()) else: @@ -193,7 +223,7 @@ def parse_name_personal(text): def extract_id(value): """ Extracts a value to use for the GRAMPS ID value from the GEDCOM - reference token. The value should be in the form of @XXX@, and the - returned value will be XXX + reference token. The value should be in the form of @XYZ@, and the + returned value will be XYZ """ return value.strip()[1:-1] diff --git a/src/GrampsDbUtils/_ProxyDbBase.py b/src/GrampsDbUtils/_ProxyDbBase.py index b8ded3413..1884d6ee9 100644 --- a/src/GrampsDbUtils/_ProxyDbBase.py +++ b/src/GrampsDbUtils/_ProxyDbBase.py @@ -32,7 +32,6 @@ __revision__ = "$Revision: 8864 $" # GRAMPS libraries # #------------------------------------------------------------------------- -from RelLib import * from GrampsDb import DbBase class ProxyDbBase(DbBase): @@ -41,7 +40,7 @@ class ProxyDbBase(DbBase): but all data marked private will be hidden from the user. """ - def __init__(self,db): + def __init__(self, db): """ Creates a new PrivateProxyDb instance. """ diff --git a/src/GrampsDbUtils/_ReadGedcom.py b/src/GrampsDbUtils/_ReadGedcom.py index e3a2da493..fddade694 100644 --- a/src/GrampsDbUtils/_ReadGedcom.py +++ b/src/GrampsDbUtils/_ReadGedcom.py @@ -22,20 +22,49 @@ "Import from GEDCOM" +#------------------------------------------------------------------------ +# +# python modules +# +#------------------------------------------------------------------------ import os +from gettext import gettext as _ import gtk +#------------------------------------------------------------------------ +# +# Set up logging +# +#------------------------------------------------------------------------ +import logging +LOG = logging.getLogger(".GedcomImport") + +#------------------------------------------------------------------------ +# +# GRAMPS modules +# +#------------------------------------------------------------------------ import Errors -from _GedcomParse import GedcomParser, StageOne +from _GedcomParse import GedcomParser +from _GedcomStageOne import StageOne from QuestionDialog import ErrorDialog, DBErrorDialog +try: + import Config + DEFAULT_SOURCE = Config.get(Config.DEFAULT_SOURCE) +except ImportError: + LOG.warn("No Config module available using defaults.") + DEFAULT_SOURCE = False + #------------------------------------------------------------------------- # -# +# importData # #------------------------------------------------------------------------- def importData(database, filename, callback=None, use_trans=False): - + """ + Try to handle ANSEL encoded files that are not really ANSEL encoded + """ try: ifile = open(filename, "r") except IOError: @@ -65,23 +94,30 @@ def importData(database, filename, callback=None, use_trans=False): dialog.destroy() else: code_set = "" - import2(database, filename, callback, code_set, use_trans) +#------------------------------------------------------------------------- +# +# import2 +# +#------------------------------------------------------------------------- def import2(database, filename, callback, code_set, use_trans): - # add some checking here + """ + Do the actual import of data + """ assert(type(code_set) == str or type(code_set) == unicode) try: ifile = open(filename,"rU") - np = StageOne(ifile) - np.parse() + stage_one = StageOne(ifile) + stage_one.parse() if code_set: - np.set_encoding(code_set) - ifile.seek(0) - gedparse = GedcomParser(database, ifile, filename, callback, np) + stage_one.set_encoding(code_set) + ifile.seek(0) + gedparse = GedcomParser(database, ifile, filename, callback, + stage_one, DEFAULT_SOURCE) except IOError, msg: ErrorDialog(_("%s could not be opened\n") % filename, str(msg)) return @@ -96,7 +132,7 @@ def import2(database, filename, callback, code_set, use_trans): try: read_only = database.readonly database.readonly = False - close = gedparse.parse_gedcom_file(use_trans) + gedparse.parse_gedcom_file(use_trans) database.readonly = read_only ifile.close() except IOError, msg: @@ -110,43 +146,3 @@ def import2(database, filename, callback, code_set, use_trans): ErrorDialog(_('Error reading GEDCOM file'), str(msg)) return -def import_from_string(database, text, callback, code_set, use_trans): - # add some checking here - - from cStringIO import StringIO - - ifile = StringIO(text) - - try: - np = NoteParser(ifile, False, code_set) - ifile.seek(0) - gedparse = GedcomParser(database, ifile, "inline-string", callback, - code_set, np.get_map(), np.get_lines(), - np.get_persons()) - except IOError, msg: - ErrorDialog(_("%s could not be opened\n") % "inline-string", str(msg)) - return - - if database.get_number_of_people() == 0: - use_trans = False - - try: - read_only = database.readonly - database.readonly = False - gedparse.parse_gedcom_file(use_trans) - database.readonly = read_only - ifile.close() - except IOError, msg: - msg = _("%s could not be opened\n") % 'inline-string' - ErrorDialog(msg, str(msg)) - return - except Errors.DbError, msg: - WarningDialog(_('Database corruption detected'), - _('A problem was detected with the database. Please ' - 'run the Check and Repair Database tool to fix the ' - 'problem.')) - return - except Errors.GedcomError, msg: - ErrorDialog(_('Error reading GEDCOM file'), str(msg)) - return - diff --git a/src/GrampsDbUtils/_ReadXML.py b/src/GrampsDbUtils/_ReadXML.py index eb8e20570..28ac70285 100644 --- a/src/GrampsDbUtils/_ReadXML.py +++ b/src/GrampsDbUtils/_ReadXML.py @@ -8,7 +8,7 @@ # 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, +# 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. @@ -33,14 +33,6 @@ from xml.parsers.expat import ExpatError, ParserCreate from gettext import gettext as _ import re -#------------------------------------------------------------------------ -# -# Set up logging -# -#------------------------------------------------------------------------ -import logging -log = logging.getLogger(".ReadXML") - #------------------------------------------------------------------------- # # Gramps Modules @@ -54,8 +46,8 @@ import Utils import DateHandler from BasicUtils import name_displayer from GrampsDb._GrampsDbConst import \ - PERSON_KEY,FAMILY_KEY,SOURCE_KEY,EVENT_KEY,\ - MEDIA_KEY,PLACE_KEY,REPOSITORY_KEY,NOTE_KEY + PERSON_KEY, FAMILY_KEY, SOURCE_KEY, EVENT_KEY, \ + MEDIA_KEY, PLACE_KEY, REPOSITORY_KEY, NOTE_KEY from BasicUtils import UpdateCallback #------------------------------------------------------------------------- @@ -65,23 +57,23 @@ from BasicUtils import UpdateCallback #------------------------------------------------------------------------- try: import gzip - gzip_ok = 1 + GZIP_OK = True except: - gzip_ok = 0 + GZIP_OK = False -personRE = re.compile(r"\s*\.images directory db_dir = os.path.abspath(os.path.dirname(database.get_save_path())) db_base = os.path.basename(database.get_save_path()) - img_dir = os.path.join(db_dir,db_base) + img_dir = os.path.join(db_dir, db_base) first = not os.path.exists(img_dir) for m_id in database.get_media_object_handles(): @@ -181,19 +173,19 @@ def importData(database, filename, callback=None,cl=0,use_trans=False): if first: os.mkdir(img_dir) first = 0 - newfile = os.path.join(img_dir,oldfile) + newfile = os.path.join(img_dir, oldfile) try: - oldfilename = os.path.join(basefile,oldfile) - shutil.copyfile(oldfilename,newfile) + oldfilename = os.path.join(basefile, oldfile) + shutil.copyfile(oldfilename, newfile) try: - shutil.copystat(oldfilename,newfile) + shutil.copystat(oldfilename, newfile) except: pass mobject.set_path(newfile) - database.commit_media_object(mobject,None,change) - except (IOError,OSError),msg: - ErrorDialog(_('Could not copy file'),str(msg)) + database.commit_media_object(mobject, None, change) + except (IOError, OSError), msg: + ErrorDialog(_('Could not copy file'), str(msg)) #------------------------------------------------------------------------- # @@ -205,7 +197,7 @@ def rs(text): return ' '.join(text.split()) def fix_spaces(text_list): - return '\n'.join(map(rs,text_list)) + return '\n'.join(map(rs, text_list)) #------------------------------------------------------------------------- # @@ -218,13 +210,13 @@ class LineParser: self.count = 0 self.person_count = 0 - if gzip_ok: + if GZIP_OK: use_gzip = 1 try: - f = gzip.open(filename,"r") + f = gzip.open(filename, "r") f.read(1) f.close() - except IOError,msg: + except IOError, msg: use_gzip = 0 except ValueError, msg: use_gzip = 1 @@ -233,16 +225,16 @@ class LineParser: try: if use_gzip: - f = gzip.open(filename,"rb") + ofile = gzip.open(filename, "rb") else: - f = open(filename,"r") + ofile = open(filename, "r") - for line in f: + for line in ofile: self.count += 1 - if personRE.match(line): + if PERSON_RE.match(line): self.person_count += 1 - f.close() + ofile.close() except: self.count = 0 self.person_count = 0 @@ -260,8 +252,8 @@ class LineParser: #------------------------------------------------------------------------- class GrampsParser(UpdateCallback): - def __init__(self,database,callback,base,change,filename): - UpdateCallback.__init__(self,callback) + def __init__(self, database, callback, base, change, filename): + UpdateCallback.__init__(self, callback) self.filename = filename self.stext_list = [] self.scomments_list = [] @@ -283,7 +275,7 @@ class GrampsParser(UpdateCallback): cursor = database.get_place_cursor() data = cursor.next() while data: - (handle,val) = data + (handle, val) = data self.place_names.add(val[2]) data = cursor.next() cursor.close() @@ -330,8 +322,8 @@ class GrampsParser(UpdateCallback): # List of new name formats and a dict for remapping them self.name_formats = [] self.name_formats_map = {} - self.taken_name_format_numbers = [num for (num,name,fmt_str,active) - in self.db.name_formats] + self.taken_name_format_numbers = [num[0] + for num in self.db.name_formats] self.event = None self.eventref = None @@ -356,106 +348,106 @@ class GrampsParser(UpdateCallback): self.eidswap = {} self.func_map = { - "address" : (self.start_address, self.stop_address), - "addresses" : (None,None), - "childlist" : (None,None), - "aka" : (self.start_name, self.stop_aka), - "attribute" : (self.start_attribute, self.stop_attribute), - "attr_type" : (None,self.stop_attr_type), - "attr_value" : (None,self.stop_attr_value), - "bookmark" : (self.start_bmark, None), - "bookmarks" : (None, None), - "format" : (self.start_format, None), - "name-formats" : (None, None), - "child" : (self.start_child,None), - "childof" : (self.start_childof,None), - "childref" : (self.start_childref,self.stop_childref), - "personref" : (self.start_personref,self.stop_personref), - "city" : (None, self.stop_city), - "county" : (None, self.stop_county), - "country" : (None, self.stop_country), - "comment" : (None, self.stop_comment), - "created" : (self.start_created, None), - "ref" : (None, self.stop_ref), - "database" : (self.start_database, self.stop_database), - "phone" : (None, self.stop_phone), - "date" : (None, self.stop_date), - "cause" : (None, self.stop_cause), - "description": (None, self.stop_description), - "event" : (self.start_event, self.stop_event), - "type" : (None, self.stop_type), - "witness" : (self.start_witness,self.stop_witness), - "eventref" : (self.start_eventref,self.stop_eventref), - "data_item" : (self.start_data_item, None), - "families" : (None, self.stop_families), - "family" : (self.start_family, self.stop_family), - "rel" : (self.start_rel, None), - "father" : (self.start_father, None), - "first" : (None, self.stop_first), - "call" : (None, self.stop_call), - "gender" : (None, self.stop_gender), - "header" : (None, None), - "last" : (self.start_last, self.stop_last), - "mother" : (self.start_mother,None), - "name" : (self.start_name, self.stop_name), - "nick" : (None, self.stop_nick), - "note" : (self.start_note, self.stop_note), - "noteref" : (self.start_noteref,None), - "p" : (None, self.stop_ptag), - "parentin" : (self.start_parentin,None), - "people" : (self.start_people, self.stop_people), - "person" : (self.start_person, self.stop_person), - "img" : (self.start_photo, self.stop_photo), - "objref" : (self.start_objref, self.stop_objref), - "object" : (self.start_object, self.stop_object), - "file" : (self.start_file, None), - "place" : (self.start_place, self.stop_place), - "dateval" : (self.start_dateval, None), - "daterange" : (self.start_daterange, None), - "datespan" : (self.start_datespan, None), - "datestr" : (self.start_datestr, None), - "places" : (None, self.stop_places), - "placeobj" : (self.start_placeobj,self.stop_placeobj), - "ptitle" : (None,self.stop_ptitle), - "location" : (self.start_location,None), - "lds_ord" : (self.start_lds_ord, self.stop_lds_ord), - "temple" : (self.start_temple, None), - "status" : (self.start_status, None), - "sealed_to" : (self.start_sealed_to, None), - "coord" : (self.start_coord,None), - "patronymic" : (None, self.stop_patronymic), - "pos" : (self.start_pos, None), - "postal" : (None, self.stop_postal), - "researcher" : (None, self.stop_research), - "resname" : (None, self.stop_resname ), - "resaddr" : (None, self.stop_resaddr ), - "rescity" : (None, self.stop_rescity ), - "resstate" : (None, self.stop_resstate ), - "rescountry" : (None, self.stop_rescountry), - "respostal" : (None, self.stop_respostal), - "resphone" : (None, self.stop_resphone), - "resemail" : (None, self.stop_resemail), - "sauthor" : (None, self.stop_sauthor), - "sabbrev" : (None, self.stop_sabbrev), - "scomments" : (None, self.stop_scomments), - "source" : (self.start_source, self.stop_source), - "sourceref" : (self.start_sourceref, self.stop_sourceref), - "sources" : (None, None), - "spage" : (None, self.stop_spage), - "spubinfo" : (None, self.stop_spubinfo), - "state" : (None, self.stop_state), - "stext" : (None, self.stop_stext), - "stitle" : (None, self.stop_stitle), - "street" : (None, self.stop_street), - "suffix" : (None, self.stop_suffix), - "title" : (None, self.stop_title), - "url" : (self.start_url, None), - "repository" : (self.start_repo,self.stop_repo), - "reporef" : (self.start_reporef,self.stop_reporef), - "rname" : (None, self.stop_rname), + "address" : (self.start_address, self.stop_address), + "addresses" : (None, None), + "childlist" : (None, None), + "aka" : (self.start_name, self.stop_aka), + "attribute" : (self.start_attribute, self.stop_attribute), + "attr_type" : (None, self.stop_attr_type), + "attr_value" : (None, self.stop_attr_value), + "bookmark" : (self.start_bmark, None), + "bookmarks" : (None, None), + "format" : (self.start_format, None), + "name-formats" : (None, None), + "child" : (self.start_child, None), + "childof" : (self.start_childof, None), + "childref" : (self.start_childref, self.stop_childref), + "personref" : (self.start_personref, self.stop_personref), + "city" : (None, self.stop_city), + "county" : (None, self.stop_county), + "country" : (None, self.stop_country), + "comment" : (None, self.stop_comment), + "created" : (self.start_created, None), + "ref" : (None, self.stop_ref), + "database" : (self.start_database, self.stop_database), + "phone" : (None, self.stop_phone), + "date" : (None, self.stop_date), + "cause" : (None, self.stop_cause), + "description": (None, self.stop_description), + "event" : (self.start_event, self.stop_event), + "type" : (None, self.stop_type), + "witness" : (self.start_witness, self.stop_witness), + "eventref" : (self.start_eventref, self.stop_eventref), + "data_item" : (self.start_data_item, None), + "families" : (None, self.stop_families), + "family" : (self.start_family, self.stop_family), + "rel" : (self.start_rel, None), + "father" : (self.start_father, None), + "first" : (None, self.stop_first), + "call" : (None, self.stop_call), + "gender" : (None, self.stop_gender), + "header" : (None, None), + "last" : (self.start_last, self.stop_last), + "mother" : (self.start_mother, None), + "name" : (self.start_name, self.stop_name), + "nick" : (None, self.stop_nick), + "note" : (self.start_note, self.stop_note), + "noteref" : (self.start_noteref, None), + "p" : (None, self.stop_ptag), + "parentin" : (self.start_parentin, None), + "people" : (self.start_people, self.stop_people), + "person" : (self.start_person, self.stop_person), + "img" : (self.start_photo, self.stop_photo), + "objref" : (self.start_objref, self.stop_objref), + "object" : (self.start_object, self.stop_object), + "file" : (self.start_file, None), + "place" : (self.start_place, self.stop_place), + "dateval" : (self.start_dateval, None), + "daterange" : (self.start_daterange, None), + "datespan" : (self.start_datespan, None), + "datestr" : (self.start_datestr, None), + "places" : (None, self.stop_places), + "placeobj" : (self.start_placeobj, self.stop_placeobj), + "ptitle" : (None, self.stop_ptitle), + "location" : (self.start_location, None), + "lds_ord" : (self.start_lds_ord, self.stop_lds_ord), + "temple" : (self.start_temple, None), + "status" : (self.start_status, None), + "sealed_to" : (self.start_sealed_to, None), + "coord" : (self.start_coord, None), + "patronymic" : (None, self.stop_patronymic), + "pos" : (self.start_pos, None), + "postal" : (None, self.stop_postal), + "researcher" : (None, self.stop_research), + "resname" : (None, self.stop_resname ), + "resaddr" : (None, self.stop_resaddr ), + "rescity" : (None, self.stop_rescity ), + "resstate" : (None, self.stop_resstate ), + "rescountry" : (None, self.stop_rescountry), + "respostal" : (None, self.stop_respostal), + "resphone" : (None, self.stop_resphone), + "resemail" : (None, self.stop_resemail), + "sauthor" : (None, self.stop_sauthor), + "sabbrev" : (None, self.stop_sabbrev), + "scomments" : (None, self.stop_scomments), + "source" : (self.start_source, self.stop_source), + "sourceref" : (self.start_sourceref, self.stop_sourceref), + "sources" : (None, None), + "spage" : (None, self.stop_spage), + "spubinfo" : (None, self.stop_spubinfo), + "state" : (None, self.stop_state), + "stext" : (None, self.stop_stext), + "stitle" : (None, self.stop_stitle), + "street" : (None, self.stop_street), + "suffix" : (None, self.stop_suffix), + "title" : (None, self.stop_title), + "url" : (self.start_url, None), + "repository" : (self.start_repo, self.stop_repo), + "reporef" : (self.start_reporef, self.stop_reporef), + "rname" : (None, self.stop_rname), } - def find_person_by_gramps_id(self,gramps_id): + def find_person_by_gramps_id(self, gramps_id): intid = self.gid2id.get(gramps_id) if intid: person = self.db.get_person_from_handle(intid) @@ -464,11 +456,11 @@ class GrampsParser(UpdateCallback): person = RelLib.Person() person.set_handle(intid) person.set_gramps_id(gramps_id) - self.db.add_person(person,self.trans) + self.db.add_person(person, self.trans) self.gid2id[gramps_id] = intid return person - def find_family_by_gramps_id(self,gramps_id): + def find_family_by_gramps_id(self, gramps_id): intid = self.gid2fid.get(gramps_id) if intid: family = self.db.get_family_from_handle(intid) @@ -477,11 +469,11 @@ class GrampsParser(UpdateCallback): family = RelLib.Family() family.set_handle(intid) family.set_gramps_id(gramps_id) - self.db.add_family(family,self.trans) + self.db.add_family(family, self.trans) self.gid2fid[gramps_id] = intid return family - def find_event_by_gramps_id(self,gramps_id): + def find_event_by_gramps_id(self, gramps_id): intid = self.gid2eid.get(gramps_id) if intid: event = self.db.get_event_from_handle(intid) @@ -490,11 +482,11 @@ class GrampsParser(UpdateCallback): event = RelLib.Event() event.set_handle(intid) event.set_gramps_id(gramps_id) - self.db.add_event(event,self.trans) + self.db.add_event(event, self.trans) self.gid2eid[gramps_id] = intid return event - def find_place_by_gramps_id(self,gramps_id): + def find_place_by_gramps_id(self, gramps_id): intid = self.gid2pid.get(gramps_id) if intid: place = self.db.get_place_from_handle(intid) @@ -503,11 +495,11 @@ class GrampsParser(UpdateCallback): place = RelLib.Place() place.set_handle(intid) place.set_gramps_id(gramps_id) - self.db.add_place(place,self.trans) + self.db.add_place(place, self.trans) self.gid2pid[gramps_id] = intid return place - def find_source_by_gramps_id(self,gramps_id): + def find_source_by_gramps_id(self, gramps_id): intid = self.gid2sid.get(gramps_id) if intid: source = self.db.get_source_from_handle(intid) @@ -516,11 +508,11 @@ class GrampsParser(UpdateCallback): source = RelLib.Source() source.set_handle(intid) source.set_gramps_id(gramps_id) - self.db.add_source(source,self.trans) + self.db.add_source(source, self.trans) self.gid2sid[gramps_id] = intid return source - def find_object_by_gramps_id(self,gramps_id): + def find_object_by_gramps_id(self, gramps_id): intid = self.gid2oid.get(gramps_id) if intid: obj = self.db.get_object_from_handle(intid) @@ -529,11 +521,11 @@ class GrampsParser(UpdateCallback): obj = RelLib.MediaObject() obj.set_handle(intid) obj.set_gramps_id(gramps_id) - self.db.add_object(obj,self.trans) + self.db.add_object(obj, self.trans) self.gid2oid[gramps_id] = intid return obj - def find_repository_by_gramps_id(self,gramps_id): + def find_repository_by_gramps_id(self, gramps_id): intid = self.gid2rid.get(gramps_id) if intid: repo = self.db.get_repository_from_handle(intid) @@ -542,11 +534,11 @@ class GrampsParser(UpdateCallback): repo = RelLib.Repository() repo.set_handle(intid) repo.set_gramps_id(gramps_id) - self.db.add_repository(repo,self.trans) + self.db.add_repository(repo, self.trans) self.gid2rid[gramps_id] = intid return repo - def find_note_by_gramps_id(self,gramps_id): + def find_note_by_gramps_id(self, gramps_id): intid = self.gid2nid.get(gramps_id) if intid: note = self.db.get_note_from_handle(intid) @@ -555,80 +547,93 @@ class GrampsParser(UpdateCallback): note = RelLib.Note() note.set_handle(intid) note.set_gramps_id(gramps_id) - self.db.add_note(note,self.trans) + self.db.add_note(note, self.trans) self.gid2nid[gramps_id] = intid return note - def map_gid(self,gramps_id): + def find_repo_by_gramps_id(self, gramps_id): + intid = self.gid2rid.get(gramps_id) + if intid: + repo = self.db.get_repository_from_handle(intid) + else: + intid = Utils.create_id() + repo = RelLib.Repository() + repo.set_handle(intid) + repo.set_gramps_id(gramps_id) + self.db.add_repository(repo, self.trans) + self.gid2rid[gramps_id] = intid + return repo + + def map_gid(self, gramps_id): if not self.idswap.get(gramps_id): - if self.db.has_gramps_id(PERSON_KEY,gramps_id): + if self.db.has_gramps_id(PERSON_KEY, gramps_id): self.idswap[gramps_id] = self.db.find_next_person_gramps_id() else: self.idswap[gramps_id] = gramps_id return self.idswap[gramps_id] - def map_fid(self,gramps_id): + def map_fid(self, gramps_id): if not self.fidswap.get(gramps_id): - if self.db.has_gramps_id(FAMILY_KEY,gramps_id): + if self.db.has_gramps_id(FAMILY_KEY, gramps_id): self.fidswap[gramps_id] = self.db.find_next_family_gramps_id() else: self.fidswap[gramps_id] = gramps_id return self.fidswap[gramps_id] - def map_eid(self,gramps_id): + def map_eid(self, gramps_id): if not self.eidswap.get(gramps_id): - if self.db.has_gramps_id(EVENT_KEY,gramps_id): + if self.db.has_gramps_id(EVENT_KEY, gramps_id): self.eidswap[gramps_id] = self.db.find_next_event_gramps_id() else: self.eidswap[gramps_id] = gramps_id return self.eidswap[gramps_id] - def map_pid(self,gramps_id): + def map_pid(self, gramps_id): if not self.pidswap.get(gramps_id): - if self.db.has_gramps_id(PLACE_KEY,gramps_id): + if self.db.has_gramps_id(PLACE_KEY, gramps_id): self.pidswap[gramps_id] = self.db.find_next_place_gramps_id() else: self.pidswap[gramps_id] = gramps_id return self.pidswap[gramps_id] - def map_sid(self,gramps_id): + def map_sid(self, gramps_id): if not self.sidswap.get(gramps_id): - if self.db.has_gramps_id(SOURCE_KEY,gramps_id): + if self.db.has_gramps_id(SOURCE_KEY, gramps_id): self.sidswap[gramps_id] = self.db.find_next_source_gramps_id() else: self.sidswap[gramps_id] = gramps_id return self.sidswap[gramps_id] - def map_oid(self,gramps_id): + def map_oid(self, gramps_id): if not self.oidswap.get(gramps_id): - if self.db.has_gramps_id(MEDIA_KEY,gramps_id): + if self.db.has_gramps_id(MEDIA_KEY, gramps_id): self.oidswap[gramps_id] = self.db.find_next_object_gramps_id() else: self.oidswap[gramps_id] = gramps_id return self.oidswap[gramps_id] - def map_rid(self,gramps_id): + def map_rid(self, gramps_id): if not self.ridswap.get(gramps_id): - if self.db.has_gramps_id(REPOSITORY_KEY,gramps_id): + if self.db.has_gramps_id(REPOSITORY_KEY, gramps_id): self.ridswap[gramps_id] = self.db.find_next_repository_gramps_id() else: self.ridswap[gramps_id] = gramps_id return self.ridswap[gramps_id] - def map_nid(self,gramps_id): + def map_nid(self, gramps_id): if not self.nidswap.get(gramps_id): - if self.db.has_gramps_id(NOTE_KEY,gramps_id): + if self.db.has_gramps_id(NOTE_KEY, gramps_id): self.nidswap[gramps_id] = self.db.find_next_note_gramps_id() else: self.nidswap[gramps_id] = gramps_id return self.nidswap[gramps_id] - def parse(self,file,use_trans=False,linecount=0,personcount=0): + def parse(self, ifile, use_trans=False, linecount=0, personcount=0): if personcount < 1000: no_magic = True else: no_magic = False - self.trans = self.db.transaction_begin("",batch=True,no_magic=no_magic) + self.trans = self.db.transaction_begin("", batch=True, no_magic=no_magic) self.set_total(linecount) self.db.disable_signals() @@ -637,7 +642,7 @@ class GrampsParser(UpdateCallback): self.p.StartElementHandler = self.startElement self.p.EndElementHandler = self.endElement self.p.CharacterDataHandler = self.characters - self.p.ParseFile(file) + self.p.ParseFile(ifile) if len(self.name_formats) > 0: # add new name formats to the existing table @@ -647,7 +652,7 @@ class GrampsParser(UpdateCallback): self.db.set_researcher(self.owner) if self.home != None: - person = self.db.find_person_from_handle(self.home,self.trans) + person = self.db.find_person_from_handle(self.home, self.trans) self.db.set_default_person_handle(person.handle) if self.tempDefault != None: gramps_id = self.map_gid(self.tempDefault) @@ -660,11 +665,11 @@ class GrampsParser(UpdateCallback): del self.func_map del self.func_list del self.p - self.db.transaction_commit(self.trans,_("GRAMPS XML import")) + self.db.transaction_commit(self.trans, _("GRAMPS XML import")) self.db.enable_signals() self.db.request_rebuild() - def start_lds_ord(self,attrs): + def start_lds_ord(self, attrs): self.ord = RelLib.LdsOrd() self.ord.set_type_from_xml(attrs['type']) self.ord.private = bool(attrs.get("priv")) @@ -673,13 +678,13 @@ class GrampsParser(UpdateCallback): elif self.family: self.family.lds_ord_list.append(self.ord) - def start_temple(self,attrs): + def start_temple(self, attrs): self.ord.set_temple(attrs['val']) - def start_data_item(self,attrs): - self.source.set_data_item(attrs['key'],attrs['value']) + def start_data_item(self, attrs): + self.source.set_data_item(attrs['key'], attrs['value']) - def start_status(self,attrs): + def start_status(self, attrs): try: # old xml with integer statuses self.ord.set_status(int(attrs['val'])) @@ -687,30 +692,30 @@ class GrampsParser(UpdateCallback): # string self.ord.set_status_from_xml(attrs['val']) - def start_sealed_to(self,attrs): + def start_sealed_to(self, attrs): try: - handle = attrs['hlink'].replace('_','') - self.db.check_family_from_handle(handle,self.trans) + handle = attrs['hlink'].replace('_', '') + self.db.check_family_from_handle(handle, self.trans) except KeyError: gramps_id = self.map_fid(attrs['ref']) family = self.find_family_by_gramps_id(gramps_id) handle = family.handle self.ord.set_family_handle(handle) - def start_place(self,attrs): + def start_place(self, attrs): try: self.placeobj = self.db.find_place_from_handle( - attrs['hlink'].replace('_',''),self.trans) + attrs['hlink'].replace('_', ''), self.trans) except KeyError: gramps_id = self.map_pid(attrs['ref']) self.placeobj = self.find_place_by_gramps_id(gramps_id) self.placeobj.private = bool(attrs.get("priv")) - def start_placeobj(self,attrs): + def start_placeobj(self, attrs): gramps_id = self.map_pid(attrs['id']) try: self.placeobj = self.db.find_place_from_handle( - attrs['handle'].replace('_',''),self.trans) + attrs['handle'].replace('_', ''), self.trans) self.placeobj.set_gramps_id(gramps_id) except KeyError: self.placeobj = self.find_place_by_gramps_id(gramps_id) @@ -718,30 +723,30 @@ class GrampsParser(UpdateCallback): self.placeobj.private = bool(attrs.get("priv")) # GRAMPS LEGACY: title in the placeobj tag - self.placeobj.title = attrs.get('title','') + self.placeobj.title = attrs.get('title', '') self.locations = 0 self.update(self.p.CurrentLineNumber) - def start_location(self,attrs): + def start_location(self, attrs): """Bypass the function calls for this one, since it appears to take up quite a bit of time""" loc = RelLib.Location() - loc.phone = attrs.get('phone','') - loc.postal = attrs.get('postal','') - loc.city = attrs.get('city','') - loc.street = attrs.get('street','') - loc.parish = attrs.get('parish','') - loc.state = attrs.get('state','') - loc.county = attrs.get('county','') - loc.country = attrs.get('country','') + loc.phone = attrs.get('phone', '') + loc.postal = attrs.get('postal', '') + loc.city = attrs.get('city', '') + loc.street = attrs.get('street', '') + loc.parish = attrs.get('parish', '') + loc.state = attrs.get('state', '') + loc.county = attrs.get('county', '') + loc.country = attrs.get('country', '') if self.locations > 0: self.placeobj.add_alternate_locations(loc) else: self.placeobj.set_main_location(loc) self.locations = self.locations + 1 - def start_witness(self,attrs): + def start_witness(self, attrs): # Parse witnesses created by older gramps self.in_witness = True self.witness_comment = "" @@ -751,13 +756,13 @@ class GrampsParser(UpdateCallback): note.set(_("Witness name: %s") % attrs['name']) note.type.set(RelLib.NoteType.EVENT) note.private = self.event.private - self.db.add_note(note,self.trans) + self.db.add_note(note, self.trans) self.event.add_note(note.handle) return try: - handle = attrs['hlink'].replace('_','') - person = self.db.find_person_from_handle(handle,self.trans) + handle = attrs['hlink'].replace('_', '') + person = self.db.find_person_from_handle(handle, self.trans) except KeyError: if attrs.has_key('ref'): person = self.find_person_by_gramps_id(self.map_gid(attrs["ref"])) @@ -771,13 +776,13 @@ class GrampsParser(UpdateCallback): event_ref.ref = self.event.handle event_ref.role.set(RelLib.EventRoleType.WITNESS) person.event_ref_list.append(event_ref) - self.db.commit_person(person,self.trans,self.change) + self.db.commit_person(person, self.trans, self.change) - def start_coord(self,attrs): - self.placeobj.lat = attrs.get('lat','') - self.placeobj.long = attrs.get('long','') + def start_coord(self, attrs): + self.placeobj.lat = attrs.get('lat', '') + self.placeobj.long = attrs.get('long', '') - def start_event(self,attrs): + def start_event(self, attrs): if self.person or self.family: # GRAMPS LEGACY: old events that were written inside # person or family objects. @@ -785,22 +790,22 @@ class GrampsParser(UpdateCallback): self.event.handle = Utils.create_id() self.event.type = RelLib.EventType() self.event.type.set_from_xml_str(attrs['type']) - self.db.add_event(self.event,self.trans) + self.db.add_event(self.event, self.trans) else: # This is new event, with ID and handle already existing self.update(self.p.CurrentLineNumber) gramps_id = self.map_eid(attrs["id"]) try: self.event = self.db.find_event_from_handle( - attrs['handle'].replace('_',''),self.trans) + attrs['handle'].replace('_', ''), self.trans) self.event.gramps_id = gramps_id except KeyError: self.event = self.find_event_by_gramps_id(gramps_id) self.event.private = bool(attrs.get("priv")) - def start_eventref(self,attrs): + def start_eventref(self, attrs): self.eventref = RelLib.EventRef() - self.eventref.ref = attrs['hlink'].replace('_','') + self.eventref.ref = attrs['hlink'].replace('_', '') self.eventref.private = bool(attrs.get('priv')) if attrs.has_key('role'): self.eventref.role.set_from_xml_str(attrs['role']) @@ -827,13 +832,13 @@ class GrampsParser(UpdateCallback): else: self.person.add_event_ref(self.eventref) - def start_attribute(self,attrs): + def start_attribute(self, attrs): self.attribute = RelLib.Attribute() self.attribute.private = bool(attrs.get("priv")) self.attribute.type = RelLib.AttributeType() if attrs.has_key('type'): self.attribute.type.set_from_xml_str(attrs["type"]) - self.attribute.value = attrs.get("value",'') + self.attribute.value = attrs.get("value", '') if self.photo: self.photo.add_attribute(self.attribute) elif self.object: @@ -849,18 +854,18 @@ class GrampsParser(UpdateCallback): elif self.family: self.family.add_attribute(self.attribute) - def start_address(self,attrs): + def start_address(self, attrs): self.address = RelLib.Address() self.address.private = bool(attrs.get("priv")) - def start_bmark(self,attrs): + def start_bmark(self, attrs): target = attrs.get('target') if not target: # Old XML. Can be either handle or id reference # and this is guaranteed to be a person bookmark try: - handle = attrs['hlink'].replace('_','') - self.db.check_person_from_handle(handle,self.trans) + handle = attrs['hlink'].replace('_', '') + self.db.check_person_from_handle(handle, self.trans) except KeyError: gramps_id = self.map_gid(attrs["ref"]) person = self.find_person_by_gramps_id(gramps_id) @@ -869,44 +874,44 @@ class GrampsParser(UpdateCallback): return # This is new XML, so we are guaranteed to have a handle ref - handle = attrs['hlink'].replace('_','') + handle = attrs['hlink'].replace('_', '') if target == 'person': - self.db.check_person_from_handle(handle,self.trans) + self.db.check_person_from_handle(handle, self.trans) self.db.bookmarks.append(handle) elif target == 'family': - self.db.check_family_from_handle(handle,self.trans) + self.db.check_family_from_handle(handle, self.trans) self.db.family_bookmarks.append(handle) elif target == 'event': - self.db.check_event_from_handle(handle,self.trans) + self.db.check_event_from_handle(handle, self.trans) self.db.event_bookmarks.append(handle) elif target == 'source': - self.db.check_source_from_handle(handle,self.trans) + self.db.check_source_from_handle(handle, self.trans) self.db.source_bookmarks.append(handle) elif target == 'place': - self.db.check_place_from_handle(handle,self.trans) + self.db.check_place_from_handle(handle, self.trans) self.db.place_bookmarks.append(handle) elif target == 'media': - self.db.check_object_from_handle(handle,self.trans) + self.db.check_object_from_handle(handle, self.trans) self.db.media_bookmarks.append(handle) elif target == 'repository': - self.db.check_repository_from_handle(handle,self.trans) + self.db.check_repository_from_handle(handle, self.trans) self.db.repo_bookmarks.append(handle) elif target == 'note': - self.db.check_note_from_handle(handle,self.trans) + self.db.check_note_from_handle(handle, self.trans) self.db.note_bookmarks.append(handle) - def start_format(self,attrs): + def start_format(self, attrs): number = int(attrs['number']) name = attrs['name'] fmt_str = attrs['fmt_str'] - active = bool(attrs.get('active',True)) + active = bool(attrs.get('active', True)) if number in self.taken_name_format_numbers: number = self.remap_name_format(number) - self.name_formats.append((number,name,fmt_str,active)) + self.name_formats.append((number, name, fmt_str, active)) - def remap_name_format(self,old_number): + def remap_name_format(self, old_number): if self.name_formats_map.has_key(old_number): # This should not happen return self.name_formats_map[old_number] # Find the lowest new number not taken yet: @@ -920,12 +925,12 @@ class GrampsParser(UpdateCallback): # Return new number return new_number - def start_person(self,attrs): + def start_person(self, attrs): self.update(self.p.CurrentLineNumber) new_id = self.map_gid(attrs['id']) try: self.person = self.db.find_person_from_handle( - attrs['handle'].replace('_',''),self.trans) + attrs['handle'].replace('_', ''), self.trans) self.person.set_gramps_id(new_id) except KeyError: self.person = self.find_person_by_gramps_id(new_id) @@ -935,34 +940,34 @@ class GrampsParser(UpdateCallback): if attrs.get('complete'): # this is only true for complete=1 self.person.marker.set(RelLib.MarkerType.COMPLETE) else: - self.person.marker.set_from_xml_str(attrs.get("marker",'')) + self.person.marker.set_from_xml_str(attrs.get("marker", '')) - def start_people(self,attrs): + def start_people(self, attrs): if attrs.has_key('home'): - self.home = attrs['home'].replace('_','') + self.home = attrs['home'].replace('_', '') - def start_father(self,attrs): + def start_father(self, attrs): try: - handle = attrs['hlink'].replace('_','') - self.db.check_person_from_handle(handle,self.trans) + handle = attrs['hlink'].replace('_', '') + self.db.check_person_from_handle(handle, self.trans) except KeyError: person = self.find_person_by_gramps_id(self.map_gid(attrs["ref"])) handle = person.handle self.family.set_father_handle(handle) - def start_mother(self,attrs): + def start_mother(self, attrs): try: - handle = attrs['hlink'].replace('_','') - self.db.check_person_from_handle(handle,self.trans) + handle = attrs['hlink'].replace('_', '') + self.db.check_person_from_handle(handle, self.trans) except KeyError: person = self.find_person_by_gramps_id(self.map_gid(attrs["ref"])) handle = person.handle self.family.set_mother_handle(handle) - def start_child(self,attrs): + def start_child(self, attrs): try: - handle = attrs['hlink'].replace('_','') - self.db.check_person_from_handle(handle,self.trans) + handle = attrs['hlink'].replace('_', '') + self.db.check_person_from_handle(handle, self.trans) except KeyError: person = self.find_person_by_gramps_id(self.map_gid(attrs["ref"])) handle = person.handle @@ -970,15 +975,15 @@ class GrampsParser(UpdateCallback): # Here we are handling the old XML, in which # frel and mrel belonged to the "childof" tag # If that were the case then childref_map has the childref ready - if self.childref_map.has_key((self.family.handle,handle)): + if self.childref_map.has_key((self.family.handle, handle)): self.family.add_child_ref( - self.childref_map[(self.family.handle,handle)]) + self.childref_map[(self.family.handle, handle)]) - def start_childref(self,attrs): + def start_childref(self, attrs): # Here we are handling the new XML, in which frel and mrel # belong to the "child" tag under family. self.childref = RelLib.ChildRef() - self.childref.ref = attrs['hlink'].replace('_','') + self.childref.ref = attrs['hlink'].replace('_', '') self.childref.private = bool(attrs.get('priv')) mrel = RelLib.ChildRefType() @@ -994,21 +999,21 @@ class GrampsParser(UpdateCallback): self.childref.set_father_relation(frel) self.family.add_child_ref(self.childref) - def start_personref(self,attrs): + def start_personref(self, attrs): self.personref = RelLib.PersonRef() - self.personref.ref = attrs['hlink'].replace('_','') + self.personref.ref = attrs['hlink'].replace('_', '') self.personref.private = bool(attrs.get('priv')) self.personref.rel = attrs['rel'] self.person.add_person_ref(self.personref) - def start_url(self,attrs): + def start_url(self, attrs): if not attrs.has_key("href"): return url = RelLib.Url() url.path = attrs["href"] - url.set_description(attrs.get("description",'')) + url.set_description(attrs.get("description", '')) url.private = bool(attrs.get('priv')) - url.type.set_from_xml_str(attrs.get('type','')) + url.type.set_from_xml_str(attrs.get('type', '')) if self.person: self.person.add_url(url) elif self.placeobj: @@ -1016,12 +1021,12 @@ class GrampsParser(UpdateCallback): elif self.repo: self.repo.add_url(url) - def start_family(self,attrs): + def start_family(self, attrs): self.update(self.p.CurrentLineNumber) gramps_id = self.map_fid(attrs["id"]) try: self.family = self.db.find_family_from_handle( - attrs['handle'].replace('_',''),self.trans) + attrs['handle'].replace('_', ''), self.trans) self.family.set_gramps_id(gramps_id) except KeyError: self.family = self.find_family_by_gramps_id(gramps_id) @@ -1037,29 +1042,29 @@ class GrampsParser(UpdateCallback): if attrs.get('complete'): # this is only true for complete=1 self.family.marker.set(RelLib.MarkerType.COMPLETE) else: - self.family.marker.set_from_xml_str(attrs.get("marker",'')) + self.family.marker.set_from_xml_str(attrs.get("marker", '')) - def start_rel(self,attrs): + def start_rel(self, attrs): if attrs.has_key('type'): self.family.type.set_from_xml_str(attrs["type"]) - def start_file(self,attrs): + def start_file(self, attrs): self.object.mime = attrs['mime'] if attrs.has_key('description'): self.object.desc = attrs['description'] else: self.object.desc = "" - drive,src = os.path.splitdrive(attrs["src"]) + drive, src = os.path.splitdrive(attrs["src"]) if src: if not drive and not os.path.isabs(src): fullpath = os.path.abspath(self.filename) - src = os.path.join(os.path.dirname(fullpath),src) + src = os.path.join(os.path.dirname(fullpath), src) self.object.path = src - def start_childof(self,attrs): + def start_childof(self, attrs): try: - handle = attrs["hlink"].replace('_','') - self.db.check_family_from_handle(handle,self.trans) + handle = attrs["hlink"].replace('_', '') + self.db.check_family_from_handle(handle, self.trans) except KeyError: family = self.find_family_by_gramps_id(self.map_fid(attrs["ref"])) handle = family.handle @@ -1079,19 +1084,19 @@ class GrampsParser(UpdateCallback): childref.set_mother_relation(mrel) if not frel.is_default(): childref.set_father_relation(frel) - self.childref_map[(handle,self.person.handle)] = childref + self.childref_map[(handle, self.person.handle)] = childref self.person.add_parent_family_handle(handle) - def start_parentin(self,attrs): + def start_parentin(self, attrs): try: - handle = attrs["hlink"].replace('_','') - self.db.check_family_from_handle(handle,self.trans) + handle = attrs["hlink"].replace('_', '') + self.db.check_family_from_handle(handle, self.trans) except KeyError: family = self.find_family_by_gramps_id(self.map_fid(attrs["ref"])) handle = family.handle self.person.add_family_handle(handle) - def start_name(self,attrs): + def start_name(self, attrs): if not self.in_witness: self.name = RelLib.Name() name_type = attrs['type'] @@ -1119,11 +1124,11 @@ class GrampsParser(UpdateCallback): except KeyError: pass - def start_last(self,attrs): - self.name.prefix = attrs.get('prefix','') - self.name.group_as = attrs.get('group','') + def start_last(self, attrs): + self.name.prefix = attrs.get('prefix', '') + self.name.group_as = attrs.get('group', '') - def start_note(self,attrs): + def start_note(self, attrs): self.in_note = 0 if 'handle' in attrs: # This is new note, with ID and handle already existing @@ -1131,12 +1136,12 @@ class GrampsParser(UpdateCallback): gramps_id = self.map_nid(attrs["id"]) try: self.note = self.db.find_note_from_handle( - attrs['handle'].replace('_',''),self.trans) + attrs['handle'].replace('_', ''), self.trans) self.note.gramps_id = gramps_id except KeyError: self.note = self.find_note_by_gramps_id(gramps_id) self.note.private = bool(attrs.get("priv")) - self.note.format = int(attrs.get('format',RelLib.Note.FLOWED)) + self.note.format = int(attrs.get('format', RelLib.Note.FLOWED)) self.note.type.set_from_xml_str(attrs['type']) else: # GRAMPS LEGACY: old notes that were written inside other objects @@ -1145,7 +1150,7 @@ class GrampsParser(UpdateCallback): # On stop_note the reference to this note will be added self.note = RelLib.Note() self.note.handle = Utils.create_id() - self.note.format = int(attrs.get('format',RelLib.Note.FLOWED)) + self.note.format = int(attrs.get('format', RelLib.Note.FLOWED)) if self.source_ref: self.note.type.set(RelLib.NoteType.SOURCEREF) self.note.private = self.source_ref.private @@ -1153,7 +1158,7 @@ class GrampsParser(UpdateCallback): self.note.type.set(RelLib.NoteType.ADDRESS) self.note.private = self.address.private elif self.ord: - self.note.type.set(elLib.NoteType.LDS) + self.note.type.set(RelLib.NoteType.LDS) self.note.private = self.ord.private elif self.attribute: self.note.type.set(RelLib.NoteType.ATTRIBUTE) @@ -1201,11 +1206,11 @@ class GrampsParser(UpdateCallback): self.note.type.set(RelLib.NoteType.REPOREF) self.note.private = self.reporef.private - self.db.add_note(self.note,self.trans) + self.db.add_note(self.note, self.trans) - def start_noteref(self,attrs): - handle = attrs['hlink'].replace('_','') - self.db.check_note_from_handle(handle,self.trans) + def start_noteref(self, attrs): + handle = attrs['hlink'].replace('_', '') + self.db.check_note_from_handle(handle, self.trans) if self.source_ref: self.source_ref.add_note(handle) @@ -1244,17 +1249,17 @@ class GrampsParser(UpdateCallback): elif self.reporef: self.reporef.add_note(handle) - def start_sourceref(self,attrs): + def start_sourceref(self, attrs): self.source_ref = RelLib.SourceRef() try: - handle = attrs["hlink"].replace('_','') - self.db.check_source_from_handle(handle,self.trans) + handle = attrs["hlink"].replace('_', '') + self.db.check_source_from_handle(handle, self.trans) except KeyError: source = self.find_source_by_gramps_id(self.map_sid(attrs["ref"])) handle = source.handle self.source_ref.ref = handle - self.source_ref.confidence = int(attrs.get("conf",self.conf)) + self.source_ref.confidence = int(attrs.get("conf", self.conf)) self.source_ref.private = bool(attrs.get("priv")) if self.photo: @@ -1284,39 +1289,39 @@ class GrampsParser(UpdateCallback): elif self.person: self.person.add_source_reference(self.source_ref) - def start_source(self,attrs): + def start_source(self, attrs): self.update(self.p.CurrentLineNumber) gramps_id = self.map_sid(attrs["id"]) try: self.source = self.db.find_source_from_handle( - attrs['handle'].replace('_',''),self.trans) + attrs['handle'].replace('_', ''), self.trans) self.source.set_gramps_id(gramps_id) except KeyError: self.source = self.find_source_by_gramps_id(gramps_id) self.source.private = bool(attrs.get("priv")) - def start_reporef(self,attrs): + def start_reporef(self, attrs): self.reporef = RelLib.RepoRef() try: - handle = attrs['hlink'].replace('_','') - self.db.check_repository_from_handle(handle,self.trans) + handle = attrs['hlink'].replace('_', '') + self.db.check_repository_from_handle(handle, self.trans) except KeyError: repo = self.find_repo_by_gramps_id(self.map_rid(attrs['ref'])) handle = repo.handle self.reporef.ref = handle - self.reporef.call_number = attrs.get('callno','') + self.reporef.call_number = attrs.get('callno', '') self.reporef.media_type.set_from_xml_str(attrs['medium']) self.reporef.private = bool(attrs.get("priv")) # we count here on self.source being available # reporefs can only be found within source self.source.add_repo_reference(self.reporef) - def start_objref(self,attrs): + def start_objref(self, attrs): self.objref = RelLib.MediaRef() try: - handle = attrs['hlink'].replace('_','') - self.db.check_object_from_handle(handle,self.trans) + handle = attrs['hlink'].replace('_', '') + self.db.check_object_from_handle(handle, self.trans) except KeyError: obj = self.find_object_by_gramps_id(self.map_oid(attrs['ref'])) handle = obj.handle @@ -1334,11 +1339,11 @@ class GrampsParser(UpdateCallback): elif self.placeobj: self.placeobj.add_media_reference(self.objref) - def start_object(self,attrs): + def start_object(self, attrs): gramps_id = self.map_oid(attrs['id']) try: self.object = self.db.find_object_from_handle( - attrs['handle'].replace('_',''),self.trans) + attrs['handle'].replace('_', ''), self.trans) self.object.set_gramps_id(gramps_id) except KeyError: self.object = self.find_object_by_gramps_id(gramps_id) @@ -1346,48 +1351,48 @@ class GrampsParser(UpdateCallback): # GRAMPS LEGACY: src, mime, and description attributes # now belong to the tag. Here we are supporting # the old format of - self.object.mime = attrs.get('mime','') - self.object.desc = attrs.get('description','') + self.object.mime = attrs.get('mime', '') + self.object.desc = attrs.get('description', '') self.object.private = bool(attrs.get("priv")) - src = attrs.get("src",'') + src = attrs.get("src", '') if src: if not os.path.isabs(src): fullpath = os.path.abspath(self.filename) - src = os.path.join(os.path.dirname(fullpath),src) + src = os.path.join(os.path.dirname(fullpath), src) self.object.path = src - def start_repo(self,attrs): + def start_repo(self, attrs): gramps_id = self.map_rid(attrs['id']) try: self.repo = self.db.find_repository_from_handle( - attrs['handle'].replace('_',''),self.trans) + attrs['handle'].replace('_', ''), self.trans) self.repo.set_gramps_id(gramps_id) except KeyError: self.repo = self.find_repository_by_gramps_id(gramps_id) self.repo.private = bool(attrs.get("priv")) - def stop_people(self,*tag): + def stop_people(self, *tag): pass - def stop_database(self,*tag): + def stop_database(self, *tag): self.update(self.p.CurrentLineNumber) - def stop_object(self,*tag): - self.db.commit_media_object(self.object,self.trans,self.change) + def stop_object(self, *tag): + self.db.commit_media_object(self.object, self.trans, self.change) self.object = None - def stop_objref(self,*tag): + def stop_objref(self, *tag): self.objref = None - def stop_repo(self,*tag): - self.db.commit_repository(self.repo,self.trans,self.change) + def stop_repo(self, *tag): + self.db.commit_repository(self.repo, self.trans, self.change) self.repo = None - def stop_reporef(self,*tag): + def stop_reporef(self, *tag): self.reporef = None - def start_photo(self,attrs): + def start_photo(self, attrs): self.photo = RelLib.MediaObject() self.pref = RelLib.MediaRef() self.pref.set_reference_handle(self.photo.get_handle()) @@ -1401,14 +1406,14 @@ class GrampsParser(UpdateCallback): elif key == "src": src = attrs["src"] if not os.path.isabs(src): - self.photo.set_path(os.path.join(self.base,src)) + self.photo.set_path(os.path.join(self.base, src)) else: self.photo.set_path(src) else: - a = RelLib.Attribute() - a.set_type(key) - a.set_value(attrs[key]) - self.photo.add_attribute(a) + attr = RelLib.Attribute() + attr.set_type(key) + attr.set_value(attrs[key]) + self.photo.add_attribute(attr) self.photo.set_mime_type(Mime.get_type(self.photo.get_path())) self.db.add_object(self.photo) if self.family: @@ -1420,58 +1425,58 @@ class GrampsParser(UpdateCallback): elif self.placeobj: self.placeobj.add_media_reference(self.pref) - def start_daterange(self,attrs): - self.start_compound_date(attrs,RelLib.Date.MOD_RANGE) + def start_daterange(self, attrs): + self.start_compound_date(attrs, RelLib.Date.MOD_RANGE) - def start_datespan(self,attrs): - self.start_compound_date(attrs,RelLib.Date.MOD_SPAN) + def start_datespan(self, attrs): + self.start_compound_date(attrs, RelLib.Date.MOD_SPAN) - def start_compound_date(self,attrs,mode): + def start_compound_date(self, attrs, mode): if self.source_ref: - dv = self.source_ref.get_date_object() + date_value = self.source_ref.get_date_object() elif self.ord: - dv = self.ord.get_date_object() + date_value = self.ord.get_date_object() elif self.object: - dv = self.object.get_date_object() + date_value = self.object.get_date_object() elif self.address: - dv = self.address.get_date_object() + date_value = self.address.get_date_object() elif self.name: - dv = self.name.get_date_object() + date_value = self.name.get_date_object() else: - dv = self.event.get_date_object() + date_value = self.event.get_date_object() start = attrs['start'].split('-') stop = attrs['stop'].split('-') try: - y = int(start[0]) + year = int(start[0]) except ValueError: - y = 0 + year = 0 try: - m = int(start[1]) + month = int(start[1]) except: - m = 0 + month = 0 try: - d = int(start[2]) + day = int(start[2]) except: - d = 0 + day = 0 try: - ry = int(stop[0]) + rng_year = int(stop[0]) except: - ry = 0 + rng_year = 0 try: - rm = int(stop[1]) + rng_month = int(stop[1]) except: - rm = 0 + rng_month = 0 try: - rd = int(stop[2]) + rng_day = int(stop[2]) except: - rd = 0 + rng_day = 0 if attrs.has_key("cformat"): cal = RelLib.Date.calendar.index(attrs['calendar']) @@ -1489,42 +1494,44 @@ class GrampsParser(UpdateCallback): else: qual = RelLib.Date.QUAL_NONE - dv.set(qual,mode,cal,(d,m,y,False,rd,rm,ry,False)) + date_value.set(qual, mode, cal, + (day, month, year, False, rng_day, + rng_month, rng_year, False)) - def start_dateval(self,attrs): + def start_dateval(self, attrs): if self.source_ref: - dv = self.source_ref.get_date_object() + date_value = self.source_ref.get_date_object() elif self.ord: - dv = self.ord.get_date_object() + date_value = self.ord.get_date_object() elif self.object: - dv = self.object.get_date_object() + date_value = self.object.get_date_object() elif self.address: - dv = self.address.get_date_object() + date_value = self.address.get_date_object() elif self.name: - dv = self.name.get_date_object() + date_value = self.name.get_date_object() else: - dv = self.event.get_date_object() + date_value = self.event.get_date_object() - bc = 1 + bce = 1 val = attrs['val'] if val[0] == '-': - bc = -1 + bce = -1 val = val[1:] start = val.split('-') try: - y = int(start[0])*bc + year = int(start[0])*bce except: - y = 0 + year = 0 try: - m = int(start[1]) + month = int(start[1]) except: - m = 0 + month = 0 try: - d = int(start[2]) + day = int(start[2]) except: - d = 0 + day = 0 if attrs.has_key("cformat"): cal = RelLib.Date.calendar_names.index(attrs['cformat']) @@ -1553,25 +1560,25 @@ class GrampsParser(UpdateCallback): else: qual = RelLib.Date.QUAL_NONE - dv.set(qual,mod,cal,(d,m,y,False)) + date_value.set(qual, mod, cal, (day, month, year, False)) - def start_datestr(self,attrs): + def start_datestr(self, attrs): if self.source_ref: - dv = self.source_ref.get_date_object() + date_value = self.source_ref.get_date_object() elif self.ord: - dv = self.ord.get_date_object() + date_value = self.ord.get_date_object() elif self.object: - dv = self.object.get_date_object() + date_value = self.object.get_date_object() elif self.address: - dv = self.address.get_date_object() + date_value = self.address.get_date_object() elif self.name: - dv = self.name.get_date_object() + date_value = self.name.get_date_object() else: - dv = self.event.get_date_object() + date_value = self.event.get_date_object() - dv.set_as_text(attrs['val']) + date_value.set_as_text(attrs['val']) - def start_created(self,attrs): + def start_created(self, attrs): if attrs.has_key('sources'): self.num_srcs = int(attrs['sources']) else: @@ -1581,7 +1588,7 @@ class GrampsParser(UpdateCallback): else: self.num_places = 0 - def start_database(self,attrs): + def start_database(self, attrs): try: # This is a proper way to get the XML version xmlns = attrs.get('xmlns') @@ -1591,20 +1598,20 @@ class GrampsParser(UpdateCallback): # so we're setting it to 1.0.0 self.version_string = '1.0.0' - def start_pos(self,attrs): + def start_pos(self, attrs): self.person.position = (int(attrs["x"]), int(attrs["y"])) - def stop_attribute(self,*tag): + def stop_attribute(self, *tag): self.attribute = None - def stop_comment(self,tag): + def stop_comment(self, tag): # Parse witnesses created by older gramps if tag.strip(): self.witness_comment = tag else: self.witness_comment = "" - def stop_witness(self,tag): + def stop_witness(self, tag): # Parse witnesses created by older gramps if self.witness_comment: text = self.witness_comment @@ -1619,33 +1626,33 @@ class GrampsParser(UpdateCallback): note.set(_("Witness comment: %s") % text) note.type.set(RelLib.NoteType.EVENT) note.private = self.event.private - self.db.add_note(note,self.trans) + self.db.add_note(note, self.trans) self.event.add_note(note.handle) self.in_witness = False - def stop_attr_type(self,tag): + def stop_attr_type(self, tag): self.attribute.set_type(tag) - def stop_attr_value(self,tag): + def stop_attr_value(self, tag): self.attribute.set_value(tag) - def stop_address(self,*tag): + def stop_address(self, *tag): if self.person: self.person.add_address(self.address) elif self.repo: self.repo.add_address(self.address) self.address = None - def stop_places(self,*tag): + def stop_places(self, *tag): self.placeobj = None - def stop_photo(self,*tag): + def stop_photo(self, *tag): self.photo = None - def stop_ptitle(self,tag): + def stop_ptitle(self, tag): self.placeobj.title = tag - def stop_placeobj(self,*tag): + def stop_placeobj(self, *tag): if self.placeobj.title == "": loc = self.placeobj.get_main_location() self.placeobj.title = build_place_title(loc) @@ -1653,14 +1660,14 @@ class GrampsParser(UpdateCallback): if self.placeobj.title in self.place_names: self.placeobj.title += " [%s]" % self.placeobj.gramps_id - self.db.commit_place(self.placeobj,self.trans,self.change) + self.db.commit_place(self.placeobj, self.trans, self.change) self.placeobj = None - def stop_family(self,*tag): - self.db.commit_family(self.family,self.trans,self.change) + def stop_family(self, *tag): + self.db.commit_family(self.family, self.trans, self.change) self.family = None - def stop_type(self,tag): + def stop_type(self, tag): if self.event: # Event type self.event.type.set_from_xml_str(tag) @@ -1668,16 +1675,16 @@ class GrampsParser(UpdateCallback): # Repository type self.repo.type.set_from_xml_str(tag) - def stop_childref(self,tag): + def stop_childref(self, tag): self.childref = None - def stop_personref(self,tag): + def stop_personref(self, tag): self.personref = None - def stop_eventref(self,tag): + def stop_eventref(self, tag): self.eventref = None - def stop_event(self,*tag): + def stop_event(self, *tag): if self.family: ref = RelLib.EventRef() ref.ref = self.event.handle @@ -1701,23 +1708,23 @@ class GrampsParser(UpdateCallback): if self.event.get_description() == "" and \ self.event.get_type() != RelLib.EventType.CUSTOM: if self.family: - text = _event_family_str % { - 'event_name' : str(self.event.get_type()), - 'family' : Utils.family_name(self.family,self.db), + text = EVENT_FAMILY_STR % { + 'event_name' : str(self.event.get_type()), + 'family' : Utils.family_name(self.family, self.db), } elif self.person: - text = _event_person_str % { - 'event_name' : str(self.event.get_type()), - 'person' : name_displayer.display(self.person), + text = EVENT_PERSON_STR % { + 'event_name' : str(self.event.get_type()), + 'person' : name_displayer.display(self.person), } else: text = u'' self.event.set_description(text) - self.db.commit_event(self.event,self.trans,self.change) + self.db.commit_event(self.event, self.trans, self.change) self.event = None - def stop_name(self,tag): + def stop_name(self, tag): if self.in_witness: # Parse witnesses created by older gramps note = RelLib.Note() @@ -1725,7 +1732,7 @@ class GrampsParser(UpdateCallback): note.set(_("Witness name: %s") % tag) note.type.set(RelLib.NoteType.EVENT) note.private = self.event.private - self.db.add_note(note,self.trans) + self.db.add_note(note, self.trans) self.event.add_note(note.handle) elif self.alt_name: # former aka tag -- alternate name @@ -1738,11 +1745,11 @@ class GrampsParser(UpdateCallback): self.person.set_primary_name (self.name) self.name = None - def stop_rname(self,tag): + def stop_rname(self, tag): # Repository name self.repo.name = tag - def stop_ref(self,tag): + def stop_ref(self, tag): # Parse witnesses created by older gramps person = self.find_person_by_gramps_id(self.map_gid(tag)) # Add an EventRef from that person @@ -1751,9 +1758,9 @@ class GrampsParser(UpdateCallback): event_ref.ref = self.event.handle event_ref.role.set(RelLib.EventRoleType.WITNESS) person.event_ref_list.append(event_ref) - self.db.commit_person(person,self.trans,self.change) + self.db.commit_person(person, self.trans, self.change) - def stop_place(self,tag): + def stop_place(self, tag): if self.placeobj == None: if self.place_map.has_key(tag): self.placeobj = self.place_map[tag] @@ -1766,40 +1773,40 @@ class GrampsParser(UpdateCallback): self.object.set_place_handle(self.placeobj.get_handle()) else: self.event.set_place_handle(self.placeobj.get_handle()) - self.db.commit_place(self.placeobj,self.trans,self.change) + self.db.commit_place(self.placeobj, self.trans, self.change) self.placeobj = None - def stop_date(self,tag): + def stop_date(self, tag): if tag: if self.address: - DateHandler.set_date(self.address,tag) + DateHandler.set_date(self.address, tag) else: - DateHandler.set_date(self.event,tag) + DateHandler.set_date(self.event, tag) - def stop_first(self,tag): + def stop_first(self, tag): self.name.set_first_name(tag) - def stop_call(self,tag): + def stop_call(self, tag): self.name.set_call_name(tag) - def stop_families(self,*tag): + def stop_families(self, *tag): self.family = None - def stop_person(self,*tag): - self.db.commit_person(self.person,self.trans,self.change) + def stop_person(self, *tag): + self.db.commit_person(self.person, self.trans, self.change) self.person = None - def stop_description(self,tag): + def stop_description(self, tag): self.event.description = tag - def stop_cause(self,tag): + def stop_cause(self, tag): # The old event's cause is now an attribute attr = RelLib.Attribute() attr.set_type(RelLib.AttributeType.CAUSE) attr.set_value(tag) self.event.add_attribute(attr) - def stop_gender(self,tag): + def stop_gender(self, tag): t = tag if t == "M": self.person.set_gender (RelLib.Person.MALE) @@ -1808,53 +1815,53 @@ class GrampsParser(UpdateCallback): else: self.person.set_gender (RelLib.Person.UNKNOWN) - def stop_stitle(self,tag): + def stop_stitle(self, tag): self.source.title = tag - def stop_sourceref(self,*tag): + def stop_sourceref(self, *tag): self.source_ref = None - def stop_source(self,*tag): - self.db.commit_source(self.source,self.trans,self.change) + def stop_source(self, *tag): + self.db.commit_source(self.source, self.trans, self.change) self.source = None - def stop_sauthor(self,tag): + def stop_sauthor(self, tag): self.source.author = tag - def stop_phone(self,tag): + def stop_phone(self, tag): self.address.phone = tag - def stop_street(self,tag): + def stop_street(self, tag): self.address.street = tag - def stop_city(self,tag): + def stop_city(self, tag): self.address.city = tag - def stop_county(self,tag): + def stop_county(self, tag): self.address.county = tag - def stop_state(self,tag): + def stop_state(self, tag): self.address.state = tag - def stop_country(self,tag): + def stop_country(self, tag): self.address.country = tag - def stop_postal(self,tag): + def stop_postal(self, tag): self.address.set_postal_code(tag) - def stop_spage(self,tag): + def stop_spage(self, tag): self.source_ref.set_page(tag) - def stop_lds_ord(self,*tag): + def stop_lds_ord(self, *tag): self.ord = None - def stop_spubinfo(self,tag): + def stop_spubinfo(self, tag): self.source.set_publication_info(tag) - def stop_sabbrev(self,tag): + def stop_sabbrev(self, tag): self.source.set_abbreviation(tag) - def stop_stext(self,tag): + def stop_stext(self, tag): if self.use_p: self.use_p = 0 text = fix_spaces(self.stext_list) @@ -1867,10 +1874,10 @@ class GrampsParser(UpdateCallback): note.private = self.source_ref.private note.set(text) note.type.set(RelLib.NoteType.SOURCE_TEXT) - self.db.add_note(note,self.trans) + self.db.add_note(note, self.trans) self.source_ref.add_note(note.handle) - def stop_scomments(self,tag): + def stop_scomments(self, tag): if self.use_p: self.use_p = 0 text = fix_spaces(self.scomments_list) @@ -1881,33 +1888,33 @@ class GrampsParser(UpdateCallback): note.private = self.source_ref.private note.set(text) note.type.set(RelLib.NoteType.SOURCEREF) - self.db.add_note(note,self.trans) + self.db.add_note(note, self.trans) self.source_ref.add_note(note.handle) - def stop_last(self,tag): + def stop_last(self, tag): if self.name: self.name.set_surname(tag) - def stop_suffix(self,tag): + def stop_suffix(self, tag): if self.name: self.name.set_suffix(tag) - def stop_patronymic(self,tag): + def stop_patronymic(self, tag): if self.name: self.name.set_patronymic(tag) - def stop_title(self,tag): + def stop_title(self, tag): if self.name: self.name.set_title(tag) - def stop_nick(self,tag): + def stop_nick(self, tag): if self.person: attr = RelLib.Attribute() attr.set_type(RelLib.AttributeType.NICKNAME) attr.set_value(tag) self.person.add_attribute(attr) - def stop_note(self,tag): + def stop_note(self, tag): self.in_note = 0 if self.use_p: self.use_p = 0 @@ -1951,38 +1958,38 @@ class GrampsParser(UpdateCallback): elif self.reporef: self.reporef.add_note(self.note.handle) - self.db.commit_note(self.note,self.trans,self.change) + self.db.commit_note(self.note, self.trans, self.change) self.note = None - def stop_research(self,tag): - self.owner.set(self.resname, self.resaddr, self.rescity, self.resstate, + def stop_research(self, tag): + self.owner.set(self.resname, self.resaddr, self.rescity, self.resstate, self.rescon, self.respos, self.resphone, self.resemail) - def stop_resname(self,tag): + def stop_resname(self, tag): self.resname = tag - def stop_resaddr(self,tag): + def stop_resaddr(self, tag): self.resaddr = tag - def stop_rescity(self,tag): + def stop_rescity(self, tag): self.rescity = tag - def stop_resstate(self,tag): + def stop_resstate(self, tag): self.resstate = tag - def stop_rescountry(self,tag): + def stop_rescountry(self, tag): self.rescon = tag - def stop_respostal(self,tag): + def stop_respostal(self, tag): self.respos = tag - def stop_resphone(self,tag): + def stop_resphone(self, tag): self.resphone = tag - def stop_resemail(self,tag): + def stop_resemail(self, tag): self.resemail = tag - def stop_ptag(self,tag): + def stop_ptag(self, tag): self.use_p = 1 if self.in_note: self.note_list.append(tag) @@ -1991,39 +1998,39 @@ class GrampsParser(UpdateCallback): elif self.in_scomments: self.scomments_list.append(tag) - def stop_aka(self,tag): + def stop_aka(self, tag): self.person.add_alternate_name(self.name) if self.name.get_type() == "": self.name.set_type(RelLib.NameType.AKA) self.name = None - def startElement(self,tag,attrs): - self.func_list[self.func_index] = (self.func,self.tlist) + def startElement(self, tag, attrs): + self.func_list[self.func_index] = (self.func, self.tlist) self.func_index += 1 self.tlist = [] try: - f,self.func = self.func_map[tag] + f, self.func = self.func_map[tag] if f: f(attrs) except KeyError: - self.func_map[tag] = (None,None) + self.func_map[tag] = (None, None) self.func = None - def endElement(self,tag): + def endElement(self, tag): if self.func: self.func(''.join(self.tlist)) self.func_index -= 1 - self.func,self.tlist = self.func_list[self.func_index] + self.func, self.tlist = self.func_list[self.func_index] def characters(self, data): if self.func: self.tlist.append(data) -def append_value(orig,val): +def append_value(orig, val): if orig: - return "%s, %s" % (orig,val) + return "%s, %s" % (orig, val) else: return val @@ -2033,56 +2040,11 @@ def build_place_title(loc): if loc.parish: value = loc.parish if loc.city: - value = append_value(value,loc.city) + value = append_value(value, loc.city) if loc.county: - value = append_value(value,loc.county) + value = append_value(value, loc.county) if loc.state: - value = append_value(value,loc.state) + value = append_value(value, loc.state) if loc.country: - value = append_value(value,loc.country) + value = append_value(value, loc.country) return value - -if __name__ == "__main__": - import sys - import hotshot#, hotshot.stats - from GrampsDb import gramps_db_factory - - def callback(val): - print val - - codeset = None - - db_class = gramps_db_factory(const.app_gramps) - database = db_class() - database.load("test.grdb",lambda x: None, mode="w") - - filename = os.path.normpath(sys.argv[1]) - basefile = os.path.dirname(filename) - change = os.path.getmtime(filename) - - parser = GrampsParser(database,callback,basefile,change,filename) - - linecounter = LineParser(filename) - lc = linecounter.get_count() - - xml_file = gzip.open(filename,"rb") - - if True: - pr = hotshot.Profile('mystats.profile') - print "Start" - pr.runcall(parser.parse,xml_file,False,lc) - print "Finished" - pr.close() -## print "Loading profile" -## stats = hotshot.stats.load('mystats.profile') -## print "done" -## stats.strip_dirs() -## stats.sort_stats('time','calls') -## stats.print_stats(100) - else: - import time - t = time.time() - parser.parse(xml_file,False,lc) - print time.time() - t - - xml_file.close() diff --git a/src/GrampsDbUtils/_WriteXML.py b/src/GrampsDbUtils/_WriteXML.py index 5c6dbbe8b..da5b7d9bf 100644 --- a/src/GrampsDbUtils/_WriteXML.py +++ b/src/GrampsDbUtils/_WriteXML.py @@ -8,7 +8,7 @@ # 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, +# 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. @@ -45,10 +45,13 @@ import ExportOptions #------------------------------------------------------------------------- # -# +# export_data # #------------------------------------------------------------------------- -def exportData(database, filename, person, option_box, callback=None): +def export_data(database, filename, person, option_box, callback=None): + """ + Calls the XML writer with the syntax expected by the export plugin + """ return GrampsDb.exportData(database, filename, person, option_box, callback, const.version) @@ -62,19 +65,18 @@ class XmlWriter(GrampsDb.GrampsDbXmlWriter): Writes a database to the XML file. """ - def __init__(self, db, callback, strip_photos, compress=1): - GrampsDb.GrampsDbXmlWriter.__init__(self, db, strip_photos, compress, - const.version, callback) + def __init__(self, dbase, callback, strip_photos, compress=1): + GrampsDb.GrampsDbXmlWriter.__init__( + self, dbase, strip_photos, compress, const.version, callback) - def write(self,filename): + def write(self, filename): """ Write the database to the specified file. """ try: - ret = GramspDb.GrampsDbXmlWriter.write(self, filename) + ret = GrampsDb.GrampsDbXmlWriter.write(self, filename) except GrampsDb.GrampsDbWriteFailure, val: - ErrorDialog(val[0],val[1]) - + ErrorDialog(val[0], val[1]) return ret #------------------------------------------------------------------------- @@ -82,12 +84,12 @@ class XmlWriter(GrampsDb.GrampsDbXmlWriter): # # #------------------------------------------------------------------------- -_title = _('GRAMPS _XML database') -_description = _('The GRAMPS XML database is a format used by older ' +TITLE = _('GRAMPS _XML database') +DESCRIPTION = _('The GRAMPS XML database is a format used by older ' 'versions of GRAMPS. It is read-write compatible with ' 'the present GRAMPS database format.') -_config = (_('GRAMPS XML export options'), ExportOptions.WriterOptionBox) -_filename = 'gramps' +CONFIG = (_('GRAMPS XML export options'), ExportOptions.WriterOptionBox) +FILENAME = 'gramps' from PluginUtils import register_export -register_export(exportData,_title,_description,_config,_filename) +register_export(export_data, TITLE, DESCRIPTION, CONFIG, FILENAME)