# # Gramps - a GTK+/GNOME based genealogy program # # Copyright (C) 2000 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 # from RelLib import * import const import string import Config import time import shutil import os from Date import SingleDate try: import gzip gzip_ok = 1 except: gzip_ok = 0 fileroot = "" strip_photo = 0 #------------------------------------------------------------------------- # # # #------------------------------------------------------------------------- def sortById(first,second): fid = first.getId() sid = second.getId() if fid < sid: return -1 else: return fid != sid #------------------------------------------------------------------------- # # # #------------------------------------------------------------------------- def fix(line): l = string.strip(line) l = string.replace(l,'&','&') l = string.replace(l,'>','>') l = string.replace(l,'<','<') return string.replace(l,'"','"') #------------------------------------------------------------------------- # # # #------------------------------------------------------------------------- def write_note(g,val,note,indent=0): if not note: return if indent != 0: g.write(" " * indent) g.write("<" + val + ">") g.write(fix(string.rstrip(note))) g.write("\n") #------------------------------------------------------------------------- # # # #------------------------------------------------------------------------- def dump_event(g,event,index=1): if event: dump_my_event(g,event.getName(),event,index) #------------------------------------------------------------------------- # # # #------------------------------------------------------------------------- def conf_priv(obj): if obj.getPrivacy() != 0: return ' priv="%d"' % obj.getPrivacy() else: return '' #------------------------------------------------------------------------- # # # #------------------------------------------------------------------------- def dump_my_event(g,name,event,index=1): if not event: return date = event.getDateObj() place = event.getPlace() description = event.getDescription() cause = event.getCause() if (not name or name == "Birth" or name == "Death") and \ date.isEmpty() and not place and not description: return sp = " " * index g.write('%s\n' % (sp,fix(name),conf_priv(event))) write_date(g,event.getDateObj(),index+1) write_ref(g,"place",place,index+1) write_line(g,"cause",cause,index+1) write_line(g,"description",description,index+1) if event.getNote() != "": write_note(g,"note",event.getNote(),index+1) for s in event.getSourceRefList(): dump_source_ref(g,s,index+1) g.write("%s\n" % sp) #------------------------------------------------------------------------- # # # #------------------------------------------------------------------------- def dump_ordinance(g,name,ord,index=1): if not ord: return sp = " " * index sp2 = " " * (index+1) g.write('%s\n' % (sp,fix(name))) dateobj = ord.getDateObj() if dateobj != None and not dateobj.isEmpty(): write_date(g,dateobj,index+1) if ord.getTemple(): g.write('%s\n' % (sp2,fix(ord.getTemple()))) write_ref(g,"place",ord.getPlace(),index+1) if ord.getStatus() != 0: g.write('%s\n' % (sp2,ord.getStatus())) if ord.getFamily(): g.write('%s\n' % (sp2,fix(ord.getFamily().getId()))) if ord.getNote() != "": write_note(g,"note",ord.getNote(),index+1) for s in ord.getSourceRefList(): dump_source_ref(g,s,index+1) g.write('%s\n' % sp) #------------------------------------------------------------------------- # # # #------------------------------------------------------------------------- def dump_source_ref(g,source_ref,index=1): source = source_ref.getBase() if source: p = source_ref.getPage() c = source_ref.getComments() t = source_ref.getText() d = source_ref.getDate() q = source_ref.getConfidence() g.write(" " * index) if p == "" and c == "" and t == "" and d.isEmpty() and q == 2: g.write('\n' % source.getId()) else: if q == 2: g.write('\n' % source.getId()) else: g.write('\n' % (source.getId(),q)) write_line(g,"spage",p,index+1) write_note(g,"scomments",c,index+1) write_note(g,"stext",t,index+1) write_date(g,d,index+1) g.write("%s\n" % (" " * index)) #------------------------------------------------------------------------- # # # #------------------------------------------------------------------------- def write_ref(g,label,person,index=1): if person: g.write('%s<%s ref="%s"/>\n' % (" "*index,label,person.getId())) #------------------------------------------------------------------------- # # # #------------------------------------------------------------------------- def write_id(g,label,person,index=1): if person: g.write('%s<%s id="%s">\n' % (" "*index,label,person.getId())) #------------------------------------------------------------------------- # # # #------------------------------------------------------------------------- def write_family_id(g,family,index=1): if family: rel = family.getRelationship() sp = " " * index if rel != "": g.write('%s\n' % (sp,family.getId(),rel)) else: g.write('%s\n' % (sp,family.getId())) #------------------------------------------------------------------------- # # # #------------------------------------------------------------------------- def write_line(g,label,value,indent=1): if value: g.write('%s<%s>%s\n' % (' '*indent,label,fix(value),label)) #------------------------------------------------------------------------- # # # #------------------------------------------------------------------------- def write_date(g,date,indent=1): sp = ' '*indent if date.isEmpty(): return cal = date.get_calendar() if cal != 0: calstr = ' calendar="%s"' % fix(str(cal)) else: calstr = '' if date.isRange(): d1 = date.get_start_date().getIsoDate() d2 = date.get_stop_date().getIsoDate() g.write('%s\n' % (sp,d1,d2,calstr)) elif date.isValid(): d1 = date.get_start_date() mode = d1.getModeVal() dstr = d1.getIsoDate() if mode == SingleDate.before: pref = ' type="before"' elif mode == SingleDate.after: pref = ' type="after"' elif mode == SingleDate.about: pref = ' type="about"' else: pref = "" g.write('%s\n' % (sp,dstr,pref,calstr)) else: g.write('%s\n' %(sp,date.getText())) #------------------------------------------------------------------------- # # # #------------------------------------------------------------------------- def write_force_line(g,label,value,indent=1): if value != None: g.write('%s<%s>%s\n' % (' '*indent,label,fix(value),label)) #------------------------------------------------------------------------- # # # #------------------------------------------------------------------------- def dump_name(g,label,name,index=1): sp = " "*index g.write('%s<%s%s>\n' % (sp,label,conf_priv(name))) write_line(g,"first",name.getFirstName(),index+1) write_line(g,"last",name.getSurname(),index+1) write_line(g,"suffix",name.getSuffix(),index+1) write_line(g,"title",name.getTitle(),index+1) if name.getNote() != "": write_note(g,"note",name.getNote(),index+1) for s in name.getSourceRefList(): dump_source_ref(g,s,index+1) g.write('%s\n' % (sp,label)) def append_value(orig,val): if orig: return "%s, %s" % (orig,val) else: return val def build_place_title(loc): "Builds a title from a location" city = fix(loc.get_city()) parish = fix(loc.get_parish()) state = fix(loc.get_state()) country = fix(loc.get_country()) county = fix(loc.get_county()) value = "" if city: value = city if parish: value = append_value(value,parish) if county: value = append_value(value,county) if state: value = append_value(value,state) if country: value = append_value(value,country) return value def dump_location(g,loc): "Writes the location information to the output file" city = fix(loc.get_city()) parish = fix(loc.get_parish()) state = fix(loc.get_state()) country = fix(loc.get_country()) county = fix(loc.get_county()) if not city and not state and not parish and not county and not country: return g.write(' \n') def write_attribute_list(g, list, indent=3): sp = ' ' * indent for attr in list: g.write('%s\n') else: g.write('>\n') for s in attr.getSourceRefList(): dump_source_ref(g,s,indent+1) write_note(g,"note",attr.getNote(),4) g.write('%s\n' % sp) def write_photo_list(g,list,indent=3): sp = ' '*indent for photo in list: mobj = photo.getReference() g.write('%s\n") else: g.write(">\n") write_attribute_list(g,proplist,indent+1) write_note(g,"note",photo.getNote(),indent+1) g.write('%s\n' % sp) def write_url_list(g, list): for url in list: g.write(' \n') def write_place_obj(g,place): title = fix(place.get_title()) long = place.get_longitude() lat = place.get_latitude() id = place.getId() main_loc = place.get_main_location() llen = len(place.get_alternate_locations()) + len(place.getUrlList()) + \ len(place.getPhotoList()) + len(place.getSourceRefList()) ml_empty = main_loc.is_empty() note = place.getNote() if title == "": title = fix(build_place_title(place.get_main_location())) g.write(' 0 or note: g.write('>\n') else: g.write('/>\n') return if long or lat: g.write(' \n' % (fix(long),fix(lat))) dump_location(g,main_loc) for loc in place.get_alternate_locations(): dump_location(g,loc) write_photo_list(g,place.getPhotoList()) write_url_list(g, place.getUrlList()) if note != "": write_note(g,"note",note,3) for s in place.getSourceRefList(): dump_source_ref(g,s,3) g.write(" \n") def write_object(g,object): id = object.getId() type = object.getMimeType() path = object.getPath() if strip_photo: path = os.path.basename(path) else: l = len(fileroot) if len(path) >= l: if fileroot == path[0:l]: path = path[l+1:] g.write(' \n') else: g.write('>\n') write_attribute_list(g,alist) if note != "": write_note(g,"note",note,3) for s in slist: dump_source_ref(g,s,3) g.write(" \n") #------------------------------------------------------------------------- # # # #------------------------------------------------------------------------- def exportData(database, filename, callback): global fileroot fileroot = os.path.dirname(filename) if os.path.isfile(filename): shutil.copy(filename, filename + ".bak") if Config.uncompress ==0 and gzip_ok == 1: try: g = gzip.open(filename,"wb") except: g = open(filename,"w") else: g = open(filename,"w") try: write_xml_data(database, g, callback, 0) g.close() except: from gnome.ui import GnomeErrorDialog import traceback from intl import gettext _ = gettext traceback.print_exc() fname = os.path.expanduser("~/gramps.err") errfile = open(fname,"w") traceback.print_exc(file=errfile) errfile.close() GnomeErrorDialog(_("Failure writing %s, original file restored") % filename) shutil.copy(filename + ".bak", filename) def quick_write(database, filename): global fileroot fileroot = os.path.dirname(filename) g = gzip.open(filename,"wb") write_xml_data(database, g, None, 0) g.close() def write_xml_data(database, g, callback, sp): global strip_photo strip_photo = sp date = string.split(time.ctime(time.time())) owner = database.getResearcher() personList = database.getPersonMap().values() personList.sort(sortById) familyList = database.getFamilyMap().values() familyList.sort(sortById) sourceList = database.getSourceMap().values() placeList = database.getPlaceMap().values() placeList.sort(sortById) objList = database.getObjectMap().values() objList.sort(sortById) total = len(personList) + len(familyList) g.write('\n') g.write('\n') g.write("\n") g.write("
\n") g.write(" \n" % len(database.getFamilyMap().values())) g.write(" \n") write_line(g,"resname",owner.getName(),3) write_line(g,"resaddr",owner.getAddress(),3) write_line(g,"rescity",owner.getCity(),3) write_line(g,"resstate",owner.getState(),3) write_line(g,"rescountry",owner.getCountry(),3) write_line(g,"respostal",owner.getPostalCode(),3) write_line(g,"resphone",owner.getPhone(),3) write_line(g,"resemail",owner.getEmail(),3) g.write(" \n") g.write("
\n") if len(personList) > 0: g.write(" \n") total = len(personList) + len(familyList) delta = max(int(total/50),1) count = 0 for person in personList: if callback and count % delta == 0: callback(float(count)/float(total)) count = count + 1 write_id(g,"person",person,2) if person.getGender() == Person.male: write_line(g,"gender","M",3) elif person.getGender() == Person.female: write_line(g,"gender","F",3) else: write_line(g,"gender","U",3) dump_name(g,"name",person.getPrimaryName(),3) for name in person.getAlternateNames(): dump_name(g,"aka",name,3) write_line(g,"uid",person.getPafUid(),3) write_line(g,"nick",person.getNickName(),3) pos = person.getPosition() if pos != None: g.write(' \n'% pos) dump_my_event(g,"Birth",person.getBirth(),3) dump_my_event(g,"Death",person.getDeath(),3) for event in person.getEventList(): dump_event(g,event,3) dump_ordinance(g,"baptism",person.getLdsBaptism(),3) dump_ordinance(g,"endowment",person.getLdsEndowment(),3) dump_ordinance(g,"sealed_to_parents",person.getLdsSeal(),3) write_photo_list(g,person.getPhotoList()) if len(person.getAddressList()) > 0: for address in person.getAddressList(): g.write(' \n' % conf_priv(address)) write_date(g,address.getDateObj(),4) write_line(g,"street",address.getStreet(),4) write_line(g,"city",address.getCity(),4) write_line(g,"state",address.getState(),4) write_line(g,"country",address.getCountry(),4) write_line(g,"postal",address.getPostal(),4) if address.getNote() != "": write_note(g,"note",address.getNote(),4) for s in address.getSourceRefList(): dump_source_ref(g,s,4) g.write(' \n') write_attribute_list(g,person.getAttributeList()) write_url_list(g,person.getUrlList()) write_ref(g,"childof",person.getMainFamily(),3) for alt in person.getAltFamilyList(): if alt[1] != "": mrel=' mrel="%s"' % alt[1] else: mrel='' if alt[2] != "": frel=' frel="%s"' % alt[2] else: frel='' g.write(" \n" % \ (alt[0].getId(), mrel, frel)) for family in person.getFamilyList(): write_ref(g,"parentin",family,3) write_note(g,"note",person.getNote(),3) g.write(" \n") g.write(" \n") if len(familyList) > 0: g.write(" \n") for family in familyList: if callback and count % delta == 0: callback(float(count)/float(total)) count = count + 1 write_family_id(g,family,2) write_ref(g,"father",family.getFather(),3) write_ref(g,"mother",family.getMother(),3) pos = family.getPosition() if pos != None: g.write(' \n'% pos) for event in family.getEventList(): dump_event(g,event,3) dump_ordinance(g,"sealed_to_spouse",family.getLdsSeal(),3) write_photo_list(g,family.getPhotoList()) if len(family.getChildList()) > 0: for person in family.getChildList(): write_ref(g,"child",person,3) write_attribute_list(g,family.getAttributeList()) write_note(g,"note",family.getNote(),3) g.write("
\n") g.write(" \n") if len(sourceList) > 0: g.write(" \n") for source in sourceList: g.write(" \n") write_force_line(g,"stitle",source.getTitle(),3) write_line(g,"sauthor",source.getAuthor(),3) write_line(g,"spubinfo",source.getPubInfo(),3) write_line(g,"scallno",source.getCallNumber(),3) if source.getNote() != "": write_note(g,"note",source.getNote(),3) write_photo_list(g,source.getPhotoList()) g.write(" \n") g.write(" \n") if len(placeList) > 0: g.write(" \n") for place in placeList: write_place_obj(g,place) g.write(" \n") if len(objList) > 0: g.write(" \n") for object in objList: write_object(g,object) g.write(" \n") if len(database.getBookmarks()) > 0: g.write(" \n") for person in database.getBookmarks(): g.write(' \n' % person.getId()) g.write(" \n") g.write("\n")