/usr/share/bauble/plugins/imex/xml.py is in bauble 0.9.7-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | #
# XML import/export plugin
#
# Description: handle import and exporting from a simple XML format
#
import os
import csv
import traceback
import gtk.gdk
import gobject
from sqlalchemy import *
import bauble
import bauble.db as db
import bauble.utils as utils
import bauble.pluginmgr as pluginmgr
import bauble.task
from bauble.utils import xml_safe
from bauble.utils.log import log, debug
# <tableset>
# <table name="tablename">
# <row>
# <column name='colname'>
# ...
# </column>
# </row>
# </table>
# <table>
# </tablest>
# TODO: single file or one file per table
def ElementFactory(parent, name, **kwargs):
try:
text = kwargs.pop('text')
except KeyError:
text = None
el = etree.SubElement(parent, name, **kwargs)
try:
if text is not None:
el.text = unicode(text, 'utf8')
except (AssertionError, TypeError), e:
el.text = unicode(str(text), 'utf8')
return el
class XMLExporter:
def __init__(self):
pass
def start(self, path=None):
d = gtk.Dialog('Bauble - XML Exporter', bauble.gui.window,
gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
box = gtk.VBox(spacing=20)
d.vbox.pack_start(box, padding=10)
file_chooser = gtk.FileChooserButton(_('Select a directory'))
file_chooser.set_select_multiple(False)
file_chooser.set_action(gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER)
box.pack_start(file_chooser)
check = gtk.CheckButton(_('Save all data in one file'))
check.set_active(True)
box.pack_start(check)
d.connect('response', self.on_dialog_response,
file_chooser.get_filename(), check.get_active())
d.show_all()
d.run()
d.hide()
def on_dialog_response(self, dialog, response, filename, one_file):
debug('on_dialog_response(%s, %s)' % (filename, one_file))
if response == gtk.RESPONSE_ACCEPT:
self.__export_task(filename, one_file)
dialog.destroy()
def __export_task(self, path, one_file=True):
ntables = len(db.metadata.tables)
steps_so_far = 0
if not one_file:
tableset_el = etree.Element('tableset')
for table_name, table in tables.iteritems():
if one_file:
tableset_el = etree.Element('tableset')
log.info('exporting %s...' % table_name)
table_el = ElementFactory(tableset_el, 'table',
attrib={'name': table_name})
results = table.select().execute().fetchall()
columns = table.c.keys()
try:
for row in results:
row_el = ElementFactory(table_el, 'row')
for col in columns:
ElementFactory(row_el, 'column', attrib={'name': col},
text=row[col])
except ValueError, e:
utils.message_details_dialog(utils.xml_safe_utf8(e),
traceback.format_exc(),
gtk.MESSAGE_ERROR)
return
else:
if one_file:
tree = etree.ElementTree(tableset_el)
filename = os.path.join(path, '%s.xml' % table_name)
# TODO: can figure out why this keeps crashing
tree.write(filename, encoding='utf8', xml_declaration=True)
if not one_file:
tree = etree.ElementTree(tableset_el)
filename = os.path.join(path, 'bauble.xml')
tree.write(filename, encoding='utf8', xml_declaration=True)
class XMLExportCommandHandler(pluginmgr.CommandHandler):
command = 'exxml'
def __call__(self, arg):
debug('XMLExportCommandHandler(%s)' % arg)
exporter = XMLExporter()
debug('starting')
exporter.start(arg)
debug('started')
class XMLExportTool(pluginmgr.Tool):
category = "Export"
label = "XML"
@classmethod
def start(cls):
c = XMLExporter()
c.start()
class XMLImexPlugin(pluginmgr.Plugin):
tools = [XMLExportTool]
commands = [XMLExportCommandHandler]
try:
import lxml.etree as etree
except ImportError:
utils.message_dialog('The <i>lxml</i> package is required for the '\
'XML Import/Exporter plugin')
|