# # Gramps - a GTK+/GNOME based genealogy program # # Copyright (C) 2004-2005 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 """ Report option handling, including saving and parsing. """ #------------------------------------------------------------------------- # # Standard Python modules # #------------------------------------------------------------------------- import os from gettext import gettext as _ #------------------------------------------------------------------------- # # SAX interface # #------------------------------------------------------------------------- try: from xml.sax import make_parser,handler,SAXParseException except: from _xmlplus.sax import make_parser,handler,SAXParseException #------------------------------------------------------------------------- # # gramps modules # #------------------------------------------------------------------------- import const import GrampsKeys import Utils import BaseDoc #------------------------------------------------------------------------- # # List of options for a single report # #------------------------------------------------------------------------- class OptionList: """ Implements a set of options to parse and store for a given report. """ def __init__(self): self.options = {} self.style_name = None self.paper_name = None self.orientation = None self.template_name = None self.format_name = None def set_options(self,options): """ Sets the whole bunch of options for the OptionList. @param options: list of options to set. @type options: list """ self.options = options def get_options(self): """ Returns the whole bunch of options for the OptionList. @returns: list of options @rtype: list """ return self.options def set_option(self,name,value): """ Sets a particular option in the OptionList. @param name: name of the option to set. @type name: str @param value: value of the option to set. @type str """ self.options[name] = value def remove_option(self,name): """ Removes a particular option from the OptionList. @param name: name of the option to remove. @type name: str """ if self.options.has_key(name): del self.options[name] def get_option(self,name): """ Returns the value of a particular option in the OptionList. @param name: name of the option to retrieve @type name: str @returns: value associated with the passed option @rtype: str """ return self.options.get(name,None) def set_style_name(self,style_name): """ Sets the style name for the OptionList. @param style_name: name of the style to set. @type style_name: str """ self.style_name = style_name def get_style_name(self): """ Returns the style name of the OptionList. @returns: string representing the style name @rtype: str """ return self.style_name def set_paper_name(self,paper_name): """ Sets the paper name for the OptionList. @param paper_name: name of the paper to set. @type paper_name: str """ self.paper_name = paper_name def get_paper_name(self): """ Returns the paper name of the OptionList. @returns: returns the paper name @rtype: str """ return self.paper_name def set_orientation(self,orientation): """ Sets the orientation for the OptionList. @param orientation: orientation to set. Possible values are BaseDoc.PAPER_LANDSCAPE or BaseDoc.PAPER_PORTRAIT @type orientation: int """ self.orientation = orientation def get_orientation(self): """ Returns the orientation for the OptionList. @returns: returns the selected orientation. Valid values are BaseDoc.PAPER_LANDSCAPE or BaseDoc.PAPER_PORTRAIT @rtype: int """ return self.orientation def set_template_name(self,template_name): """ Sets the template name for the OptionList. @param template_name: name of the template to set. @type template_name: str """ self.template_name = template_name def get_template_name(self): """ Returns the template name of the OptionList. @returns: template name @rtype: str """ return self.template_name def set_format_name(self,format_name): """ Sets the format name for the OptionList. @param format_name: name of the format to set. @type format_name: str """ self.format_name = format_name def get_format_name(self): """ Returns the format name of the OptionList. @returns: returns the format name @rtype: str """ return self.format_name #------------------------------------------------------------------------- # # Collection of option lists # #------------------------------------------------------------------------- class OptionListCollection: # Default values for common options default_style_name = "default" default_paper_name = GrampsKeys.get_paper_preference() default_template_name = "" default_orientation = BaseDoc.PAPER_PORTRAIT default_format_name = 'print' def __init__(self,filename=None): """ Creates an OptionListCollection instance from the list defined in the specified file. @param filename: XML file that contains option definitions @type filename: str """ if not filename or not os.path.isfile(filename): filename = const.report_options self.filename = os.path.expanduser(filename) self.last_paper_name = self.default_paper_name self.last_orientation = self.default_orientation self.last_template_name = self.default_template_name self.last_format_name = self.default_format_name self.option_list_map = {} self.parse() def get_option_list_map(self): """ Returns the map of reports names to option lists. @returns: Returns the map of reports names to option lists. @rtype: dictionary """ return self.option_list_map def get_option_list(self,name): """ Returns the option_list associated with the report name @param name: name associated with the desired report. @type name: str @returns: returns the option list associated with the name, or None of no such option exists @rtype: str """ return self.option_list_map.get(name,None) def get_report_names(self): """ Returns a list of all the report names in the OptionListCollection @returns: returns the list of report names @rtype: list """ return self.option_list_map.keys() def set_option_list(self,name,option_list): """ Adds or replaces an option_list in the OptionListCollection. @param name: name assocated with the report to add or replace. @type name: str @param option_list: list of options @type option_list: str """ self.option_list_map[name] = option_list def set_last_paper_name(self,paper_name): """ Sets the last paper name used for the any report in this collection. @param paper_name: name of the paper to set. @type paper_name: str """ self.last_paper_name = paper_name def get_last_paper_name(self): """ Returns the last paper name used for the any report in this collection. @returns: returns the name of the paper @rtype: str """ return self.last_paper_name def set_last_orientation(self,orientation): """ Sets the last orientation used for the any report in this collection. @param orientation: orientation to set. @type orientation: int """ self.last_orientation = orientation def get_last_orientation(self): """ Returns the last orientation used for the any report in this collection. @returns: last orientation used @rtype: int """ return self.last_orientation def set_last_template_name(self,template_name): """ Sets the last template used for the any report in this collection. template_name: name of the style to set. """ self.last_template_name = template_name def get_last_template_name(self): """ Returns the last template used for the any report in this collection. """ return self.last_template_name def set_last_format_name(self,format_name): """ Sets the last format used for the any report in this collection. format_name: name of the format to set. """ self.last_format_name = format_name def get_last_format_name(self): """ Returns the last format used for the any report in this collection. """ return self.last_format_name def save(self): """ Saves the current OptionListCollection to the associated file. """ f = open(self.filename,"w") f.write("\n") f.write('\n') f.write('\n') if self.get_last_paper_name() != self.default_paper_name: f.write(' \n' % self.get_last_paper_name() ) if self.get_last_template_name() != self.default_template_name: f.write('