gramps/src/Plugins.py

414 lines
13 KiB
Python
Raw Normal View History

2001-05-13 01:56:57 +00:00
#
# 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
#
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
import gtk
2001-05-13 01:56:57 +00:00
import libglade
from intl import gettext
_ = gettext
2001-05-13 01:56:57 +00:00
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
import os
import sys
from re import compile
2001-05-13 01:56:57 +00:00
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
import const
import utils
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
_reports = []
_tools = []
_imports = []
_exports = []
_success = []
_failed = []
_attempt = []
_loaddir = []
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
DOCSTRING = "d"
IMAGE = "i"
TASK = "f"
TITLE = "t"
2001-05-13 01:56:57 +00:00
pymod = compile(r"^(.*)\.py$")
2001-05-13 01:56:57 +00:00
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
class ReportPlugins:
def __init__(self,db,active):
self.db = db
self.active = active
self.dialog = libglade.GladeXML(const.pluginsFile,"report")
self.dialog.signal_autoconnect({
"on_report_apply_clicked" : self.on_report_apply_clicked,
"on_report_ok_clicked" : self.on_report_apply_clicked,
"destroy_passed_object" : utils.destroy_passed_object
2001-05-13 01:56:57 +00:00
})
tree = self.dialog.get_widget("tree1")
2001-05-13 01:56:57 +00:00
self.run_tool = None
build_tree(tree,_reports,self.on_report_node_selected)
def on_report_apply_clicked(self,obj):
utils.destroy_passed_object(obj)
if self.run_tool:
self.run_tool(self.db,self.active)
def on_report_node_selected(self,obj):
doc = obj.get_data(DOCSTRING)
xpm = obj.get_data(IMAGE)
title = obj.get_data(TITLE)
img = self.dialog.get_widget("image")
self.dialog.get_widget("description").set_text(doc)
i,m = gtk.create_pixmap_from_xpm_d(gtk.GtkWindow(),None,xpm)
img.set(i,m)
self.dialog.get_widget("report_title").set_text(title)
self.run_tool = obj.get_data(TASK)
2001-05-13 01:56:57 +00:00
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
class ToolPlugins:
def __init__(self,db,active,update):
self.db = db
self.active = active
self.update = update
self.dialog = libglade.GladeXML(const.pluginsFile,"pluginsel")
self.dialog.signal_autoconnect({
"on_apply_clicked" : self.on_apply_clicked,
"on_ok_clicked" : self.on_apply_clicked,
2001-05-13 01:56:57 +00:00
"destroy_passed_object" : utils.destroy_passed_object
})
tree = self.dialog.get_widget("tree")
2001-05-13 01:56:57 +00:00
self.run_tool = None
build_tree(tree,_tools,self.on_node_selected)
def on_apply_clicked(self,obj):
utils.destroy_passed_object(obj)
if self.run_tool:
self.run_tool(self.db,self.active,self.update)
def on_node_selected(self,obj):
doc = obj.get_data(DOCSTRING)
title = obj.get_data(TITLE)
self.dialog.get_widget("description").set_text(doc)
self.dialog.get_widget("pluginTitle").set_text(title)
self.run_tool = obj.get_data(TASK)
2001-05-13 01:56:57 +00:00
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def build_tree(tree,list,task):
item_hash = {}
for report in list:
item = gtk.GtkTreeItem(report[2])
item.connect("select",task)
item.set_data(TASK,report[0])
item.set_data(TITLE,report[2])
item.set_data(DOCSTRING,report[3])
item.set_data(IMAGE,report[4])
if item_hash.has_key(report[1]):
item_hash[report[1]].append(item)
else:
item_hash[report[1]] = [item]
key_list = item_hash.keys()
key_list.sort()
for key in key_list:
top_item = gtk.GtkTreeItem(key)
top_item.show()
tree.append(top_item)
subtree = gtk.GtkTree()
subtree.show()
top_item.set_subtree(subtree)
subtree.show()
for item in item_hash[key]:
item.show()
subtree.append(item)
2001-05-13 01:56:57 +00:00
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def load_plugins(dir):
global _success,_failed,_attempt,_loaddir
2001-05-13 01:56:57 +00:00
if not os.path.isdir(dir):
return
if dir not in _loaddir:
_loaddir.append(dir)
2001-05-13 01:56:57 +00:00
sys.path.append(dir)
for file in os.listdir(dir):
name = os.path.split(file)
match = pymod.match(name[1])
if not match:
continue
_attempt.append(file)
2001-09-01 17:13:15 +00:00
plugin = match.groups()[0]
2001-05-13 01:56:57 +00:00
try:
a = __import__(plugin)
_success.append(a)
2001-05-13 01:56:57 +00:00
except:
2001-09-01 17:13:15 +00:00
print _("Failed to load the module: %s") % plugin
import traceback
traceback.print_exc()
_failed.append(plugin)
2001-05-13 01:56:57 +00:00
def reload_plugins(obj):
for plugin in _success:
try:
reload(plugin)
except:
print _("Failed to load the module: %s") % plugin
import traceback
traceback.print_exc()
_failed.append(plugin)
for plugin in _failed:
try:
__import__(plugin)
except:
print _("Failed to load the module: %s") % plugin
import traceback
traceback.print_exc()
_failed.append(plugin)
2001-05-13 01:56:57 +00:00
for dir in _loaddir:
for file in os.listdir(dir):
name = os.path.split(file)
match = pymod.match(name[1])
if not match:
continue
_attempt.append(file)
plugin = match.groups()[0]
try:
a = __import__(plugin)
_success.append(a)
except:
print _("Failed to load the module: %s") % plugin
import traceback
traceback.print_exc()
_failed.append(plugin)
2001-05-13 01:56:57 +00:00
#-------------------------------------------------------------------------
#
# Plugin registering
2001-05-13 01:56:57 +00:00
#
#-------------------------------------------------------------------------
def register_export(task, name):
_exports.append((task, name))
def register_import(task, name):
_imports.append((task, name))
def register_report(task, name, category=None, description=None, xpm=None):
if xpm == None:
xpm_data = no_image()
elif type(xpm) == type([]):
xpm_data = xpm
else:
xpm_data = xpm
if category == None:
category = _("Uncategorized")
if description == None:
description = _("No description was provided")
_reports.append((task, category, name, description, xpm_data))
def register_tool(task, name, category=None, description=None, xpm=None):
if xpm == None:
xpm_data = no_image()
elif type(xpm) == type([]):
xpm_data = xpm
else:
xpm_data = xpm
if category == None:
category = _("Uncategorized")
if description == None:
description = _("No description was provided")
_tools.append((task, category, name, description, xpm_data))
#-------------------------------------------------------------------------
#
# Building pulldown menus
#
#-------------------------------------------------------------------------
def build_menu(top_menu,list,callback):
report_menu = gtk.GtkMenu()
report_menu.show()
hash = {}
for report in list:
if hash.has_key(report[1]):
hash[report[1]].append((report[2],report[0]))
else:
hash[report[1]] = [(report[2],report[0])]
catlist = hash.keys()
catlist.sort()
for key in catlist:
entry = gtk.GtkMenuItem(key)
entry.show()
report_menu.append(entry)
submenu = gtk.GtkMenu()
submenu.show()
entry.set_submenu(submenu)
list = hash[key]
list.sort()
for name in list:
subentry = gtk.GtkMenuItem(name[0])
subentry.show()
subentry.connect("activate",callback,name[1])
submenu.append(subentry)
top_menu.set_submenu(report_menu)
def build_report_menu(top_menu,callback):
build_menu(top_menu,_reports,callback)
def build_tools_menu(top_menu,callback):
build_menu(top_menu,_tools,callback)
2001-05-13 01:56:57 +00:00
def build_export_menu(top_menu,callback):
myMenu = gtk.GtkMenu()
for report in _exports:
item = gtk.GtkMenuItem(report[1])
item.connect("activate", callback ,report[0])
item.show()
myMenu.append(item)
top_menu.set_submenu(myMenu)
def build_import_menu(top_menu,callback):
myMenu = gtk.GtkMenu()
for report in _imports:
item = gtk.GtkMenuItem(report[1])
item.connect("activate", callback ,report[0])
item.show()
myMenu.append(item)
top_menu.set_submenu(myMenu)
2001-05-13 01:56:57 +00:00
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def no_image():
return [
"48 48 5 1",
" c None",
". c #999999",
"+ c #FFFFCC",
"@ c #000000",
"# c #CCCCCC",
" ",
" ",
" ",
" ",
" ",
" ",
" .......... ",
" .++++++++. ",
" .++++++++. ",
" @@@.++++++++. ",
" @##.++++++++. ",
" @# .++++++++. ",
" .......... @# .......... ",
" .++++++++. @# ",
" .++++++++. @# ",
" @@@.++++++++.@@@@# ",
" @##.++++++++.###@# .......... ",
" @# .++++++++. @# .++++++++. ",
" @# .......... @# .++++++++. ",
" @# @@@.++++++++. ",
" @# ##.++++++++. ",
" @# .++++++++. ",
" .......... @# .......... ",
" .++++++++. @# ",
" .++++++++. @# ",
" .++++++++.@@@@# ",
" .++++++++.###@# ",
" .++++++++. @# .......... ",
" .......... @# .++++++++. ",
" @# .++++++++. ",
" @# @@@.++++++++. ",
" @# @##.++++++++. ",
" @# .......... @# .++++++++. ",
" @# .++++++++. @# .......... ",
" @# .++++++++. @# ",
" @@@.++++++++.@@@@# ",
" ##.++++++++.###@# ",
" .++++++++. @# .......... ",
" .......... @# .++++++++. ",
" @# .++++++++. ",
" @@@.++++++++. ",
" ##.++++++++. ",
" .++++++++. ",
" .......... ",
" ",
" ",
" ",
" "]