From 374e117c179b771888fd8c15d80332fd7d65e1ab Mon Sep 17 00:00:00 2001 From: Alex Roitman Date: Fri, 23 Sep 2005 21:41:23 +0000 Subject: [PATCH] Merge changes done in the TOOL_OPT branch as follows: * src/Tool.py: Add to CVS: Generic tool interface. * src/ArgHandler.py: Support command line mode for tools. * src/PluginMgr.py: New tool registration scheme. * src/Plugins.py: Convert to tool registration scheme. * src/const.py.in: Add tool categories. * src/plugins/ChangeTypes.py: Convert to new scheme. * src/Options.py: Add to CVS: generic options interface. * src/ReportOptions.py: Keep only report-specific option handling. * src/Makefile.am: Ship new files. * src/plugins/Checkpoint.py: Minor improvements for the RCS. * src/plugins/Makefile.am: Ship Checkpoint.py. svn: r5220 --- gramps2/ChangeLog | 17 + gramps2/src/ArgHandler.py | 28 +- gramps2/src/Makefile.am | 2 + gramps2/src/Options.py | 514 +++++++++++++++++++++++++++++ gramps2/src/PluginMgr.py | 57 +++- gramps2/src/Plugins.py | 44 +-- gramps2/src/ReportOptions.py | 423 ++++-------------------- gramps2/src/Tool.py | 252 ++++++++++++++ gramps2/src/const.py.in | 17 + gramps2/src/gramps_main.py | 7 +- gramps2/src/plugins/ChangeTypes.py | 155 ++++++--- 11 files changed, 1078 insertions(+), 438 deletions(-) create mode 100644 gramps2/src/Options.py create mode 100644 gramps2/src/Tool.py diff --git a/gramps2/ChangeLog b/gramps2/ChangeLog index c49c3f261..dd15ff884 100644 --- a/gramps2/ChangeLog +++ b/gramps2/ChangeLog @@ -1,3 +1,20 @@ +2005-09-23 Alex Roitman + Merge changes done in the TOOL_OPT branch as follows: + * src/Tool.py: Add to CVS: Generic tool interface. + * src/ArgHandler.py: Support command line mode for tools. + * src/PluginMgr.py: New tool registration scheme. + * src/Plugins.py: Convert to tool registration scheme. + * src/const.py.in: Add tool categories. + * src/plugins/ChangeTypes.py: Convert to new scheme. + * src/Options.py: Add to CVS: generic options interface. + * src/ReportOptions.py: Keep only report-specific option + handling. + * src/Makefile.am: Ship new files. + +2005-09-23 Alex Roitman + * src/plugins/Checkpoint.py: Minor improvements for the RCS. + * src/plugins/Makefile.am: Ship Checkpoint.py. + 2005-09-22 Don Allingham * doc/gramps-manual/C/*.xml diff --git a/gramps2/src/ArgHandler.py b/gramps2/src/ArgHandler.py index a69c3068c..03b7b68b0 100644 --- a/gramps2/src/ArgHandler.py +++ b/gramps2/src/ArgHandler.py @@ -50,6 +50,7 @@ import GrampsKeys import RecentFiles import PluginMgr import Report +import Tool #------------------------------------------------------------------------- # @@ -211,7 +212,7 @@ class ArgHandler: self.exports.append((outfname,outformat)) elif o in ( '-a', '--action' ): action = v - if action not in ( 'check', 'summary', 'report' ): + if action not in ( 'check', 'summary', 'report', 'tool' ): print "Unknown action: %s. Ignoring." % action continue options_str = "" @@ -619,6 +620,31 @@ class ArgHandler: print "Unknown report name. Available names are:" for item in PluginMgr.cl_list: print " %s" % item[0] + elif action == "tool": + try: + options_str_dict = dict( [ tuple(chunk.split('=')) for + chunk in options_str.split(',') ] ) + except: + options_str_dict = {} + print "Ignoring invalid options string." + + name = options_str_dict.pop('name',None) + if not name: + print "Tool name not given. Please use name=toolname" + os._exit(1) + + for item in PluginMgr.cli_tool_list: + if name == item[0]: + category = item[1] + tool_class = item[2] + options_class = item[3] + Tool.cli_tool(self.parent.db,name,category, + tool_class,options_class,options_str_dict) + return + + print "Unknown tool name. Available names are:" + for item in PluginMgr.cli_tool_list: + print " %s" % item[0] else: print "Unknown action: %s." % action os._exit(1) diff --git a/gramps2/src/Makefile.am b/gramps2/src/Makefile.am index 189e111c0..82d9ede13 100644 --- a/gramps2/src/Makefile.am +++ b/gramps2/src/Makefile.am @@ -122,6 +122,8 @@ gdir_PYTHON = \ ReportOptions.py\ ReadGrdb.py\ WriteGrdb.py\ + Options.py\ + Tool.py\ Spell.py # Could use GNU make's ':=' syntax for nice wildcard use. diff --git a/gramps2/src/Options.py b/gramps2/src/Options.py new file mode 100644 index 000000000..e64a41ba1 --- /dev/null +++ b/gramps2/src/Options.py @@ -0,0 +1,514 @@ +# +# 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 + +""" +General 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 Utils + +#------------------------------------------------------------------------- +# +# List of options for a single module +# +#------------------------------------------------------------------------- +class OptionList: + """ + Implements a set of options to parse and store for a given module. + """ + + def __init__(self): + self.options = {} + + 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) + +#------------------------------------------------------------------------- +# +# Collection of option lists +# +#------------------------------------------------------------------------- +class OptionListCollection: + """ + Implements a collection of option lists. + """ + + def __init__(self,filename): + """ + Creates an OptionListCollection instance from the list defined + in the specified file. + @param filename: XML file that contains option definitions + @type filename: str + """ + + self.filename = os.path.expanduser(filename) + self.option_list_map = {} + self.init_common() + self.parse() + + def init_common(self): + pass + + def get_option_list_map(self): + """ + Returns the map of module names to option lists. + @returns: Returns the map of module names to option lists. + @rtype: dictionary + """ + return self.option_list_map + + def get_option_list(self,name): + """ + Returns the option_list associated with the module name + @param name: name associated with the desired module. + @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_module_names(self): + """ + Returns a list of all the module names in the OptionListCollection + @returns: returns the list of module 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 module 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 write_common(self,f): + """ + Stub function for common options. Overridden by reports. + """ + pass + + def write_module_common(self,f,option_list): + """ + Stub function for common options. Overridden by reports. + """ + pass + + def save(self): + """ + Saves the current OptionListCollection to the associated file. + """ + f = open(self.filename,"w") + f.write("\n") + f.write('\n') + + self.write_common(f) + + for module_name in self.get_module_names(): + option_list = self.get_option_list(module_name) + f.write('\n' % module_name) + options = option_list.get_options() + for option_name in options.keys(): + if type(options[option_name]) in (type(list()),type(tuple())): + f.write(' \n') + else: + f.write(' \n') + + f.write('\n') + f.close() + + def parse(self): + """ + Loads the OptionList from the associated file, if it exists. + """ + try: + p = make_parser() + p.setContentHandler(OptionParser(self)) + p.parse('file://' + self.filename) + except (IOError,OSError,SAXParseException): + pass + +#------------------------------------------------------------------------- +# +# OptionParser +# +#------------------------------------------------------------------------- +class OptionParser(handler.ContentHandler): + """ + SAX parsing class for the OptionListCollection XML file. + """ + + def __init__(self,collection): + """ + Creates a OptionParser class that populates the passed collection. + + collection: OptionListCollection to be loaded from the file. + """ + handler.ContentHandler.__init__(self) + self.collection = collection + + self.mname = None + self.option_list = None + self.oname = None + self.o = None + self.an_o = None + self.list_class = OptionList + + def startElement(self,tag,attrs): + """ + Overridden class that handles the start of a XML element + """ + if tag in ("report","module"): + self.mname = attrs['name'] + self.option_list = self.list_class() + self.o = {} + elif tag == "option": + self.oname = attrs['name'] + if attrs.has_key('length'): + self.an_o = [] + else: + self.an_o = attrs['value'] + elif tag == "listitem": + self.an_o.append(attrs['value']) + + def endElement(self,tag): + "Overridden class that handles the end of a XML element" + if tag == "option": + self.o[self.oname] = self.an_o + elif tag in ("report","module"): + self.option_list.set_options(self.o) + self.collection.set_option_list(self.mname,self.option_list) + +#------------------------------------------------------------------------- +# +# Class handling options for plugins +# +#------------------------------------------------------------------------- +class OptionHandler: + """ + Implements handling of the options for the plugins. + """ + + def __init__(self,module_name,options_dict,person_id=None): + self.module_name = module_name + self.default_options_dict = options_dict.copy() + self.options_dict = options_dict + + # Retrieve our options from whole collection + self.init_subclass() + self.option_list_collection = self.collection_class(self.filename) + self.init_common() + self.saved_option_list = self.option_list_collection.get_option_list(module_name) + self.person_id = person_id + + # Whatever was found should override the defaults + if self.saved_option_list: + self.set_options() + else: + # If nothing was found, set up the option list + self.saved_option_list = self.list_class() + self.option_list_collection.set_option_list(module_name, + self.saved_option_list) + + def init_subclass(self): + self.collection_class = OptionListCollection + self.list_class = OptionList + self.filename = None + + def init_common(self): + pass + + def set_options(self): + """ + Sets options to be used in this plugin according to the passed + options dictionary. + + Dictionary values are all strings, since they were read from XML. + Here we need to convert them to the needed types. We use default + values to determine the type. + """ + # First we set options_dict values based on the saved options + options = self.saved_option_list.get_options() + bad_opts = [] + for option_name in options.keys(): + if not self.options_dict.has_key(option_name): + print "Option %s is present in the %s but is not known "\ + "to the module." % (option_name, + self.option_list_collection.filename) + print "Ignoring..." + bad_opts.append(option_name) + continue + try: + converter = Utils.get_type_converter(self.options_dict[option_name]) + self.options_dict[option_name] = converter(options[option_name]) + except ValueError: + pass + + for option_name in bad_opts: + options.pop(option_name) + + # Then we set common options from whatever was found + self.set_common_options() + + def set_common_options(self): + pass + + def save_options(self): + """ + Saves options to file. + + We need to only store non-default options. Therefore, we remove all + options whose values are the defaults prior to saving. + """ + + # First we save options from options_dict + for option_name in self.options_dict.keys(): + if self.options_dict[option_name] == self.default_options_dict[option_name]: + self.saved_option_list.remove_option(option_name) + else: + self.saved_option_list.set_option(option_name,self.options_dict[option_name]) + + # Handle common options + self.save_common_options() + + # Finally, save the whole collection into file + self.option_list_collection.save() + + def save_common_options(self): + pass + + def get_filter_number(self): + if self.default_options_dict.has_key('filter'): + return self.options_dict.get('filter', + self.default_options_dict['filter']) + else: + return None + + def set_filter_number(self,val): + self.options_dict['filter'] = val + + def get_person_id(self): + return self.person_id + + def set_person_id(self,val): + self.person_id = val + +#------------------------------------------------------------------------ +# +# Base Options class +# +#------------------------------------------------------------------------ +class Options: + + """ + Defines options and provides handling interface. + + This is a base Options class for the modules. All modules' options + classes should derive from it. + """ + + def __init__(self,name,person_id=None): + """ + Initializes the class, performing usual house-keeping tasks. + Subclasses MUST call this in their __init__() method. + """ + self.set_new_options() + self.enable_options() + + if self.enable_dict: + self.options_dict.update(self.enable_dict) + print name + self.handler = OptionHandler(name,self.options_dict,person_id) + + def set_new_options(self): + """ + Sets options specific for this module. + + Modules that need custom options need to override this method. + Two dictionaries MUST be defined here: + + self.options_dict + This is a dictionary whose keys are option names + and values are the default option values. + + self.options_help + This is a dictionary whose keys are option names + and values are 3- or 4- lists or tuples: + ('=example','Short description',VALUES,DO_PREPEND) + The VALUES is either a single string (in that case + the DO_PREPEND does not matter) or a list/tuple of + strings to list. In that case, if DO_PREPEND evaluates + as True then each string will be preneded with the ordinal + number when help is printed on the command line. + + NOTE: Both dictionaries must have identical keys. + + NOTE: If a particular module does not use custom options, + then it should not override this method. + """ + self.options_dict = {} + self.options_help = {} + + def enable_options(self): + """ + Enables semi-common options for this module. + + The semi-common option is the option which GRAMPS is aware of, + but not common enough to be present in all modules. Here's the list + of possible keys for semi-commons: + + 'filter' - Filter number, selected among filters + available for this module. If defined, + get_module_filters() method must be defined + which returns the list of available filters. + + A self.enable_dict dictionary MUST be defined here, whose keys + are the valid semi-common keys above, and whose values are the + desired default values for semi-commons. + + NOTE: If a particular module does not use semi-common options, + then it should not override this method. + """ + self.enable_dict = {} + + def add_user_options(self,dialog): + """ + Sets up UI controls (widgets) for the options specific for this modul. + + This method MUST be overridden by modules that define new options. + The single argument 'dialog' is the Report.BareReportDialog instance. + Any attribute of the dialog is available. + + After the widgets are defined, they MUST be added to the dialog + using the following call: + dialog.add_options(LABEL,widget) + + NOTE: To really have any effect besides looking pretty, each widget + set up here must be also parsed in the parse_user_options() + method below. + """ + pass + + def parse_user_options(self,dialog): + """ + Parses UI controls (widgets) for the options specific for this module. + + This method MUST be overridden by modules that define new options. + The single argument 'dialog' is the Report.BareReportDialog instance. + Any attribute of the dialog is available. + + After obtaining values from the widgets, they MUST be used to set the + appropriate options_dict values. Otherwise the values will not have + any user-visible effect. + + NOTE: Any widget parsed here MUST be defined and added to the dialog + in the add_user_options() method above. + """ + pass + + def get_filter_number(self): + """ + Return number of a filter to use. + + This method MUST NOT be overridden by subclasses. + """ + return self.handler.get_filter_number() diff --git a/gramps2/src/PluginMgr.py b/gramps2/src/PluginMgr.py index 4f5540d4c..9c9a32934 100644 --- a/gramps2/src/PluginMgr.py +++ b/gramps2/src/PluginMgr.py @@ -45,6 +45,7 @@ from gettext import gettext as _ #------------------------------------------------------------------------- import const import Errors +import Tool #------------------------------------------------------------------------- # @@ -64,6 +65,7 @@ drawdoc_list = [] failmsg_list = [] bkitems_list = [] cl_list = [] +cli_tool_list = [] _success_list = [] @@ -77,6 +79,7 @@ _relcalc_class = Relationship.RelationshipCalculator #------------------------------------------------------------------------- # +# Constants # #------------------------------------------------------------------------- _unavailable = _("No description was provided"), @@ -154,16 +157,48 @@ def register_import(task, ffilter, mime=None, native_format=0, format_name=""): import_list.append((task, ffilter, mime, native_format, format_name)) +#------------------------------------------------------------------------- +# +# Tool registration +# +#------------------------------------------------------------------------- def register_tool( - task, name, - category=_("Uncategorized"), - description=_unavailable, + category, + tool_class, + options_class, + modes, + translated_name, status=_("Unknown"), + description=_unavailable, author_name=_("Unknown"), author_email=_("Unknown") ): - """Register a tool with the plugin system""" + """ + Register a tool with the plugin system. + + This function should be used to register tool in GUI and/or + command-line mode. The low-level functions (starting with '_') + should not be used on their own. Instead, this functino will call + them as needed. + """ + + (junk,gui_task) = divmod(modes,2**Tool.MODE_GUI) + if gui_task: + _register_gui_tool(tool_class,options_class,translated_name, + name,category,description, + status,author_name,author_email) + + (junk,cli_task) = divmod(modes-gui_task,2**Tool.MODE_CLI) + if cli_task: + _register_cli_tool(name,category,tool_class,options_class) + +def _register_gui_tool(tool_class,options_class,translated_name, + name,category, + description=_unavailable, + status=_("Unknown"), + author_name=_("Unknown"), + author_email=_("Unknown")): del_index = -1 for i in range(0,len(tool_list)): val = tool_list[i] @@ -171,8 +206,15 @@ def register_tool( del_index = i if del_index != -1: del tool_list[del_index] - tool_list.append((task, category, name, description, - status, author_name, author_name)) + tool_list.append((tool_class,options_class,translated_name, + category,name,description,status, + author_name,author_email)) + +def _register_cli_tool(name,category,tool_class,options_class): + for n in cli_tool_list: + if n[0] == name: + return + cli_tool_list.append((name,category,tool_class,options_class)) #------------------------------------------------------------------------- # @@ -194,7 +236,8 @@ def register_report( """ Registers report for all possible flavors. - This function should be used to register report as a stand-alone, book item, or command-line flavor in any combination of those. + This function should be used to register report as a stand-alone, + book item, or command-line flavor in any combination of those. The low-level functions (starting with '_') should not be used on their own. Instead, this function will call them as needed. """ diff --git a/gramps2/src/Plugins.py b/gramps2/src/Plugins.py index b18032521..ef2663783 100644 --- a/gramps2/src/Plugins.py +++ b/gramps2/src/Plugins.py @@ -1,7 +1,7 @@ # # Gramps - a GTK+/GNOME based genealogy program # -# Copyright (C) 2000-2004 Donald N. Allingham +# Copyright (C) 2000-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 @@ -59,6 +59,7 @@ import Utils import GrampsKeys import Errors import Report +import Tool import PluginMgr #------------------------------------------------------------------------- @@ -421,16 +422,19 @@ class PluginStatus: # Building pulldown menus # #------------------------------------------------------------------------- -def build_menu(top_menu,list,callback): +def build_tools_menu(top_menu,callback): report_menu = gtk.Menu() report_menu.show() hash_data = {} - for report in list: - if hash_data.has_key(report[1]): - hash_data[report[1]].append((report[2],report[0])) + for tool in PluginMgr.tool_list: + tool_category = const.tool_categories[tool[3]] + if hash_data.has_key(tool_category): + hash_data[tool_category].append( + (tool[0],tool[1],tool[2],tool[4],tool[3])) else: - hash_data[report[1]] = [(report[2],report[0])] + hash_data[tool_category] = [ + (tool[0],tool[1],tool[2],tool[4],tool[3])] catlist = hash_data.keys() catlist.sort() @@ -442,11 +446,12 @@ def build_menu(top_menu,list,callback): submenu.show() entry.set_submenu(submenu) lst = hash_data[key] - lst.sort() + lst.sort(by_menu_name) for name in lst: - subentry = gtk.MenuItem("%s..." % name[0]) + subentry = gtk.MenuItem("%s..." % name[2]) subentry.show() - subentry.connect("activate",callback,name[1]) + subentry.connect("activate",callback,Tool.gui_tool, + name[0],name[1],name[2],name[3],name[4]) submenu.append(subentry) top_menu.set_submenu(report_menu) @@ -456,8 +461,6 @@ def build_menu(top_menu,list,callback): # #------------------------------------------------------------------------- def build_report_menu(top_menu,callback): -# build_menu(top_menu,_reports,callback) -#def build_menu(top_menu,list,callback): report_menu = gtk.Menu() report_menu.show() @@ -488,7 +491,8 @@ def build_report_menu(top_menu,callback): for name in lst: subentry = gtk.MenuItem("%s..." % name[2]) subentry.show() - subentry.connect("activate",callback,Report.report,name[0],name[1],name[2],name[3],name[4]) + subentry.connect("activate",callback,Report.report, + name[0],name[1],name[2],name[3],name[4]) submenu.append(subentry) top_menu.set_submenu(report_menu) @@ -501,8 +505,8 @@ def by_menu_name(a,b): # build_tools_menu # #------------------------------------------------------------------------- -def build_tools_menu(top_menu,callback): - build_menu(top_menu,PluginMgr.tool_list,callback) +#def build_tools_menu(top_menu,callback): +# build_menu(top_menu,PluginMgr.tool_list,callback) #------------------------------------------------------------------------- # @@ -724,9 +728,9 @@ def reload_plugins(obj=None,junk1=None,junk2=None,junk3=None): # Register the plugin reloading tool # #------------------------------------------------------------------------- -PluginMgr.register_tool( - reload_plugins, - _("Reload plugins"), - category=_("Debug"), - description=_("Attempt to reload plugins. Note: This tool itself is not reloaded!"), - ) +## PluginMgr.register_tool( +## reload_plugins, +## _("Reload plugins"), +## category=_("Debug"), +## description=_("Attempt to reload plugins. Note: This tool itself is not reloaded!"), +## ) diff --git a/gramps2/src/ReportOptions.py b/gramps2/src/ReportOptions.py index 2014b2b81..7323f34b6 100644 --- a/gramps2/src/ReportOptions.py +++ b/gramps2/src/ReportOptions.py @@ -52,70 +52,26 @@ import const import GrampsKeys import Utils import BaseDoc +import Options #------------------------------------------------------------------------- # # List of options for a single report # #------------------------------------------------------------------------- -class OptionList: +class OptionList(Options.OptionList): """ Implements a set of options to parse and store for a given report. """ def __init__(self): - self.options = {} + Options.OptionList.__init__(self) 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. @@ -203,26 +159,20 @@ class OptionList: # Collection of option lists # #------------------------------------------------------------------------- -class OptionListCollection: +class OptionListCollection(Options.OptionListCollection): + """ + Implements a collection of option lists. + """ + def __init__(self,filename): + Options.OptionListCollection.__init__(self,filename) - # 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) + def init_common(self): + # Default values for common options + self.default_style_name = "default" + self.default_paper_name = GrampsKeys.get_paper_preference() + self.default_template_name = "" + self.default_orientation = BaseDoc.PAPER_PORTRAIT + self.default_format_name = 'print' self.last_paper_name = self.default_paper_name self.last_orientation = self.default_orientation @@ -230,45 +180,6 @@ class OptionListCollection: 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. @@ -330,14 +241,7 @@ class OptionListCollection: """ 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') - + def write_common(self,f): f.write('\n') if self.get_last_paper_name() != self.default_paper_name: f.write(' \n' % self.get_last_paper_name() ) @@ -348,42 +252,24 @@ class OptionListCollection: if self.get_last_orientation() != self.default_orientation: f.write(' \n' % self.get_last_orientation() ) f.write('\n') - - for report_name in self.get_report_names(): - option_list = self.get_option_list(report_name) - f.write('\n' % report_name) - options = option_list.get_options() - for option_name in options.keys(): - if type(options[option_name]) in (type(list()),type(tuple())): - f.write(' \n') - else: - f.write('