Refactor export plugin framework. All importers and exporters are now plugins.

svn: r11243
This commit is contained in:
Brian Matherly
2008-11-04 04:12:51 +00:00
parent b51caa5844
commit 2e9a488e3e
45 changed files with 746 additions and 1387 deletions

View File

@@ -10,6 +10,7 @@ pkgdatadir = $(datadir)/@PACKAGE@/gen/plug
pkgdata_PYTHON = \
__init__.py \
_export.py \
_import.py \
_manager.py \
_plugin.py

View File

@@ -24,5 +24,6 @@ The "plug" package for handling plugins in Gramps.
from _plugin import Plugin
from _manager import PluginManager
from _import import ImportPlugin
from _export import ExportPlugin
__all__ = [ "menu", Plugin, PluginManager, ImportPlugin ]
__all__ = [ "menu", Plugin, PluginManager, ImportPlugin, ExportPlugin ]

102
src/gen/plug/_export.py Normal file
View File

@@ -0,0 +1,102 @@
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2008 Brian G. Matherly
#
# 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$
"""
This module provides the Plugin class for export plugins.
"""
from gen.plug import Plugin
class ExportPlugin(Plugin):
"""
This class represents a plugin for exporting data from Gramps
"""
def __init__(self, name, description, export_function,
extension, config=None):
"""
@param name: A friendly name to call this plugin.
Example: "GEDCOM Export"
@type name: string
@param description: An short description of the plugin.
Example: "This plugin will export a GEDCOM file from database"
@type description: string
@param export_function: A function to call to perform the export.
The function must take the form:
def export_function(database, filename, option_box, callback):
where:
"db" is a Gramps database to import the data into
"filename" is the file that the data will be exported to
"callback" is a callable object that takes two parameters.
The first parameter is a progress indicator.
The second parameter is a text string.
@type export_function: callable
@param extension: The extension for the output file.
Example: "ged"
@type extension: str
@param config: Options for the exporter
@type config: tuple (??,??)
@return: nothing
"""
Plugin.__init__(self, name, description)
self.__name = name
self.__export_func = export_function
self.__extension = extension
self.__config = config
def get_module_name(self):
"""
Get the name of the module that this plugin lives in.
@return: a string representing the name of the module for this plugin
"""
return self.__export_func.__module__
def get_name(self):
"""
Get the short name for this plugins.
@return: str
"""
return self.__name
def get_export_function(self):
"""
Get the import function for this plugins.
@return: the callable import_function passed into __init__
"""
return self.__export_func
def get_extension(self):
"""
Get the file extension for the export file.
@return: str
"""
return self.__extension
def get_config(self):
"""
Get the config.
@return: (??,??)
"""
return self.__config

View File

@@ -29,7 +29,7 @@ class ImportPlugin(Plugin):
"""
This class represents a plugin for importing data into Gramps
"""
def __init__(self, name, description, import_function, mime_types=None):
def __init__(self, name, description, import_function, extension):
"""
@param name: A friendly name to call this plugin.
Example: "GEDCOM Import"
@@ -47,14 +47,14 @@ class ImportPlugin(Plugin):
The first parameter is a progress indicator.
The second parameter is a text string.
@type import_function: callable
@param mime_types: A list of mime types that apply to the file type.
Example: "['application/x-gramps']"
@type mime_types: [str] (list of strings)
@param extension: The extension for the files imported by this plugin.
Example: "ged"
@type extension: str
@return: nothing
"""
Plugin.__init__(self, name, description)
self.__import_func = import_function
self.__mime_types = mime_types
self.__extension = extension
def get_module_name(self):
"""
@@ -72,10 +72,10 @@ class ImportPlugin(Plugin):
"""
return self.__import_func
def get_mime_types(self):
def get_extension(self):
"""
Get the list of mime types that apply to the import file.
Get the extension for the files imported by this plugin.
@return: [str] (list of strings)
@return: str
"""
return self.__mime_types
return self.__extension

View File

@@ -93,8 +93,8 @@ class PluginManager(gen.utils.Callback):
self.__report_list = []
self.__quick_report_list = []
self.__tool_list = []
self.__export_list = []
self.__import_plugins = []
self.__export_plugins = []
self.__attempt_list = []
self.__loaddir_list = []
self.__textdoc_list = []
@@ -257,10 +257,6 @@ class PluginManager(gen.utils.Callback):
""" Return the list of command line tool plugins. """
return self.__cli_tool_list
def get_export_list(self):
""" Return the list of export plugins. """
return self.__export_list
def get_module_description(self, module):
""" Given a module name, return the module description. """
return self.__mod2text.get(module, '')
@@ -274,6 +270,9 @@ class PluginManager(gen.utils.Callback):
if isinstance(plugin, gen.plug.ImportPlugin):
self.__import_plugins.append(plugin)
self.__mod2text[plugin.get_module_name()] = plugin.get_description()
if isinstance(plugin, gen.plug.ExportPlugin):
self.__export_plugins.append(plugin)
self.__mod2text[plugin.get_module_name()] = plugin.get_description()
def get_import_plugins(self):
"""
@@ -282,24 +281,14 @@ class PluginManager(gen.utils.Callback):
@return: [gen.plug.ImportPlugin] (a list of ImportPlugin instances)
"""
return self.__import_plugins
def register_export(self, export_data, title, description='', config=None,
filename=''):
"""
Register an export filter, taking the task, file filter,
and the list of patterns for the filename matching.
"""
if description and filename:
del_index = -1
for i in range(0, len(self.__export_list)):
if self.__export_list[i][1] == title:
del_index = i
if del_index != -1:
del self.__export_list[del_index]
self.__export_list.append(
(export_data, title, description, config, filename))
self.__mod2text[export_data.__module__] = description
def get_export_plugins(self):
"""
Get the list of export plugins.
@return: [gen.plug.ExportPlugin] (a list of ExportPlugin instances)
"""
return self.__export_plugins
def register_tool(self, name, category, tool_class, options_class,
modes, translated_name, status=_("Unknown"),
@@ -545,8 +534,8 @@ class PluginManager(gen.utils.Callback):
for filename, junk in self.__failmsg_list
]
self.__export_list[:] = [ item for item in self.__export_list
if item[0].__module__ not in failed_module_names ][:]
self.__export_plugins[:] = [ item for item in self.__export_plugins
if item.get_module_name not in failed_module_names ][:]
self.__import_plugins[:] = [ item for item in self.__import_plugins
if item.get_module_name not in failed_module_names ][:]
self.__tool_list[:] = [ item for item in self.__tool_list