# # Gramps - a GTK+/GNOME based genealogy program # # Copyright (C) 2003-2006 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$ # Written by Alex Roitman, # largely based on the BaseDoc classes by Don Allingham #------------------------------------------------------------------------- # # Standard Python modules # #------------------------------------------------------------------------- import os from gettext import gettext as _ #------------------------------------------------------------------------ # # Set up logging # #------------------------------------------------------------------------ import logging log = logging.getLogger(".BookReport") #------------------------------------------------------------------------- # # SAX interface # #------------------------------------------------------------------------- try: from xml.sax import make_parser,handler,SAXParseException except: from _xmlplus.sax import make_parser,handler,SAXParseException #------------------------------------------------------------------------- # # GTK/Gnome modules # #------------------------------------------------------------------------- from gtk import RESPONSE_OK import gtk.glade #------------------------------------------------------------------------- # # gramps modules # #------------------------------------------------------------------------- from RelLib import Person import Utils import ListModel import PluginMgr from PluginUtils import Report, ReportOptions import BaseDoc from QuestionDialog import WarningDialog import Plugins #------------------------------------------------------------------------ # # Book Item class # #------------------------------------------------------------------------ class BookItem: """ Interface into the book item -- a smallest element of the book. """ def __init__(self,name=None): """ Creates a new empty BookItem. name: if not None then the book item is retreived from the book item registry using name for lookup """ if name: self.get_registered_item(name) else: self.clear() def clear(self): """ Clear the contents of the book item. Everything gets set to empty values except for the style_name""" self.translated_name = "" self.category = "" self.write_item = None self.option_class = None self.style_file = "" self.style_name = "default" self.make_default_style = None self.name = "" def get_registered_item(self,name): """ Retrieve the item from the book item registry. name: a name used for lookup. """ self.clear() for item in PluginMgr.bkitems_list: if item[4] == name: self.translated_name = item[0] if item[5]: self.category = Plugins.UNSUPPORTED else: self.category = item[1] self.write_item = item[2] self.name = item[4] self.option_class = item[3](self.name) def get_name(self): """ Returns the name of the item. """ return self.name def get_translated_name(self): """ Returns the translated name of the item. """ return self.translated_name def get_category(self): """ Returns the category of the item. """ return self.category def get_write_item(self): """ Returns the report-writing function of the item. """ return self.write_item def set_style_name(self,style_name): """ Sets the style name for the item. style_name: name of the style to set. """ self.style_name = style_name def get_style_name(self): """ Returns the style name of the item. """ return self.style_name def get_style_file(self): """ Returns the style file name for the item. """ return self.style_file def get_make_default_style(self): """ Returns the function to make default style for the item. """ return self.make_default_style #------------------------------------------------------------------------ # # Book class # #------------------------------------------------------------------------ class Book: """ Interface into the user-defined book -- a collection of book items. """ def __init__(self,obj=None): """ Creates a new empty Book. obj: if not None, creates the Book from the values in obj, instead of creating an empty Book. """ self.name = "" self.dbname = "" if obj: self.item_list = obj.item_list else: self.item_list = [] def set_name(self,name): """ Sets the name of the book. name: the name to set. """ self.name = name def get_name(self): """ Returns the name of the book. """ return self.name def get_dbname(self): """ Returns the name of the database file used for the book. """ return self.dbname def set_dbname(self,name): """ Sets the name of the database file used for the book. name: a filename to set. """ self.dbname = name def clear(self): """ Clears the contents of the book. """ self.item_list = [] def append_item(self,item): """ Adds an item to the book. item: an item to append. """ self.item_list.append(item) def insert_item(self,index,item): """ Inserts an item into the given position in the book. index: a position index. item: an item to append. """ self.item_list.insert(index,item) def pop_item(self,index): """ Pop an item from given position in the book. index: a position index. """ return self.item_list.pop(index) def get_item(self,index): """ Returns an item at a given position in the book. index: a position index. """ return self.item_list[index] def set_item(self,index,item): """ Sets an item at a given position in the book. index: a position index. item: an item to set. """ self.item_list[index] = item def get_item_list(self): """ Returns list of items in the current book. """ return self.item_list #------------------------------------------------------------------------ # # BookList class # #------------------------------------------------------------------------ class BookList: """ Interface into the user-defined list of books. BookList is loaded from a specified XML file if it exists. """ def __init__(self,filename): """ Creates a new BookList from the books that may be defined in the specified file. file: XML file that contains book items definitions """ self.bookmap = {} self.file = os.path.expanduser("~/.gramps/" + filename) self.parse() def delete_book(self,name): """ Removes a book from the list. Since each book must have a unique name, the name is used to delete the book. name: name of the book to delete """ del self.bookmap[name] def get_book_map(self): """ Returns the map of names to books. """ return self.bookmap def get_book(self,name): """ Returns the Book associated with the name name: name associated with the desired Book. """ return self.bookmap[name] def get_book_names(self): "Returns a list of all the book names in the BookList" return self.bookmap.keys() def set_book(self,name,book): """ Adds or replaces a Book in the BookList. name: name assocated with the Book to add or replace. book: definition of the Book """ self.bookmap[name] = book def save(self): """ Saves the current BookList to the associated file. """ f = open(self.file,"w") f.write("\n") f.write('\n') for name in self.bookmap.keys(): book = self.get_book(name) dbname = book.get_dbname() f.write('\n' % (name,dbname) ) for item in book.get_item_list(): f.write(' \n' % (item.get_name(),item.get_translated_name() ) ) option_handler = item.option_class.handler for option_name in option_handler.options_dict.keys(): option_value = option_handler.options_dict[option_name] if type(option_value) in (list,tuple): f.write(' \n') else: option_type = Utils.type_name(option_value) f.write('