Commit ed773d61 authored by Jesse van den Kieboom's avatar Jesse van den Kieboom

Added import and export functionality to snippets. Importing and exporting

	* plugins/snippets/snippets/Importer.py:
	* plugins/snippets/snippets/Exporter.py:
	* plugins/snippets/snippets/snippets.glade:
	* plugins/snippets/snippets/Manager.py:
	* plugins/snippets/snippets/Makefile.am:
	
	Added import and export functionality to snippets. Importing and 
	exporting can be done through the new GUI or by drag and drop

svn path=/trunk/; revision=5729
parent ca955357
2007-07-19 Jesse van den Kieboom <jesse@icecrew.nl>
* plugins/snippets/snippets/Importer.py:
* plugins/snippets/snippets/Exporter.py:
* plugins/snippets/snippets/snippets.glade:
* plugins/snippets/snippets/Manager.py:
* plugins/snippets/snippets/Makefile.am:
Added import and export functionality to snippets. Importing and
exporting can be done through the new GUI or by drag and drop
2007-07-19 Jesse van den Kieboom <jesse@icecrew.nl>
* plugins/snippets/snippets/Helper.py: set use-markup in message_dialog
......
import os
import tempfile
import sys
import shutil
from snippets.Library import *
import ElementTree as et
from Helper import *
class Exporter:
def __init__(self, filename, snippets):
self.filename = filename
self.set_snippets(snippets)
def set_snippets(self, snippets):
self.snippets = {}
for snippet in snippets:
lang = snippet.language()
if self.snippets.has_key(lang):
self.snippets[lang].append(snippet)
else:
self.snippets[lang] = [snippet]
def export_xml(self, dirname, language, snippets):
# Create the root snippets node
root = et.Element('snippets')
# Create filename based on language
if language:
filename = os.path.join(dirname, language + '.xml')
# Set the language attribute
root.attrib['language'] = language
else:
filename = os.path.join(dirname, 'global.xml')
# Add all snippets to the root node
for snippet in snippets:
root.append(snippet.to_xml())
# Write xml
write_xml(root, filename, ('text', 'accelerator'))
def export_archive(self, cmd):
dirname = tempfile.mkdtemp()
# Save current working directory and change to temporary directory
curdir = os.getcwd()
try:
os.chdir(dirname)
# Write snippet xml files
for language, snippets in self.snippets.items():
self.export_xml(dirname, language , snippets)
# Archive files
status = os.system('%s "%s" *.xml' % (cmd, self.filename))
finally:
os.chdir(curdir)
if status != 0:
return _('The archive `%s` could not be created' % self.filename)
# Remove the temporary directory
shutil.rmtree(dirname)
def export_targz(self):
self.export_archive('tar -c --gzip -f')
def export_tarbz2(self):
self.export_archive('tar -c --bzip2 -f')
def export_tar(self):
self.export_archive('tar -cf')
def run(self):
dirname = os.path.dirname(self.filename)
if not os.path.exists(dirname):
return _('Target directory `%s` does not exist') % dirname
if not os.path.isdir(dirname):
return _('Target directory `%s` is not a valid directory') % dirname
(root, ext) = os.path.splitext(self.filename)
actions = {'.tar.gz': self.export_targz,
'.tar.bz2': self.export_tarbz2,
'.tar': self.export_tar}
for k, v in actions.items():
if self.filename.endswith(k):
return v()
return self.export_targz()
# ex:ts=8:et:
import os
import tempfile
import sys
import shutil
from snippets.Library import *
class Importer:
def __init__(self, filename):
self.filename = filename
def import_destination(self, filename):
userdir = Library().userdir
filename = os.path.basename(filename)
(root, ext) = os.path.splitext(filename)
filename = os.path.join(userdir, root + ext)
i = 1
while os.path.exists(filename):
filename = os.path.join(userdir, root + '_' + str(i) + ext)
i += 1
return filename
def import_file(self, filename):
if not os.path.exists(filename):
return _('File `%s` does not exist') % filename
if not os.path.isfile(filename):
return _('File `%s` is not a valid snippets file') % filename
# Find destination for file to copy to
dest = self.import_destination(filename)
# Copy file
shutil.copy(filename, dest)
# Add library
if not Library().add_user_library(dest):
return _('Imported file `%s` is not a valid snippets file') % os.path.basename(dest)
def import_xml(self):
return self.import_file(self.filename)
def import_archive(self, cmd):
dirname = tempfile.mkdtemp()
status = os.system('cd %s; %s "%s"' % (dirname, cmd, self.filename))
if status != 0:
return _('The archive `%s` could not be extracted' % self.filename)
errors = []
# Now import all the files from the archive
for f in os.listdir(dirname):
f = os.path.join(dirname, f)
if os.path.isfile(f):
if self.import_file(f):
errors.append(os.path.basename(f))
else:
sys.stderr.write('Skipping %s, not a valid snippets file' % os.path.basename(f))
# Remove the temporary directory
shutil.rmtree(dirname)
if len(errors) > 0:
return _('The following files could not be imported: %s') % ', '.join(errors)
def import_targz(self):
self.import_archive('tar -x --gzip -f')
def import_tarbz2(self):
self.import_archive('tar -x --bzip2 -f')
def import_tar(self):
self.import_archive('tar -xf')
def run(self):
if not os.path.exists(self.filename):
return _('File `%s` does not exist') % self.filename
if not os.path.isfile(self.filename):
return _('File `%s` is not a valid snippets archive') % self.filename
(root, ext) = os.path.splitext(self.filename)
actions = {'.tar.gz': self.import_targz,
'.tar.bz2': self.import_tarbz2,
'.xml': self.import_xml,
'.tar': self.import_tar}
for k, v in actions.items():
if self.filename.endswith(k):
return v()
return _('File `%s` is not a valid snippets archive') % self.filename
# ex:ts=8:et:
......@@ -13,11 +13,13 @@ plugin_PYTHON = \
Manager.py \
SnippetComplete.py \
Helper.py \
SubstitutionParser.py
SubstitutionParser.py \
Importer.py \
Exporter.py
plugin_DATA = snippets.glade
EXTRA_DIST = snippets.glade
CLEANFILES = *.bak *.gladep
DISTCLEANFILES = *.bak *.gladep
CLEANFILES = *.bak *.gladep *.pyc
DISTCLEANFILES = *.bak *.gladep *.pyc
This diff is collapsed.
......@@ -8,8 +8,8 @@
<property name="type">GTK_WINDOW_TOPLEVEL</property>
<property name="window_position">GTK_WIN_POS_NONE</property>
<property name="modal">False</property>
<property name="default_width">600</property>
<property name="default_height">375</property>
<property name="default_width">750</property>
<property name="default_height">500</property>
<property name="resizable">True</property>
<property name="destroy_with_parent">True</property>
<property name="decorated">True</property>
......@@ -72,7 +72,7 @@
<property name="border_width">6</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="position">250</property>
<property name="position">275</property>
<child>
<widget class="GtkVBox" id="vbox_selection">
......@@ -143,24 +143,93 @@
<child>
<widget class="GtkHBox" id="hbox_buttons">
<property name="visible">True</property>
<property name="homogeneous">True</property>
<property name="homogeneous">False</property>
<property name="spacing">6</property>
<child>
<widget class="GtkButton" id="button_new_snippet">
<property name="visible">True</property>
<property name="tooltip" translatable="yes">Create new snippet</property>
<property name="can_default">True</property>
<property name="can_focus">True</property>
<property name="label">gtk-new</property>
<property name="use_stock">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_button_new_snippet_clicked" last_modification_time="Tue, 20 Dec 2005 19:50:58 GMT"/>
<child>
<widget class="GtkImage" id="image1">
<property name="visible">True</property>
<property name="stock">gtk-new</property>
<property name="icon_size">4</property>
<property name="xalign">0.5</property>
<property name="yalign">0.5</property>
<property name="xpad">0</property>
<property name="ypad">0</property>
</widget>
</child>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="button_import_snippets">
<property name="visible">True</property>
<property name="tooltip" translatable="yes">Import snippets</property>
<property name="can_default">True</property>
<property name="can_focus">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_button_import_snippets_clicked" last_modification_time="Tue, 10 Jul 2007 18:37:11 GMT"/>
<child>
<widget class="GtkImage" id="image5">
<property name="visible">True</property>
<property name="stock">gtk-open</property>
<property name="icon_size">4</property>
<property name="xalign">0.5</property>
<property name="yalign">0.5</property>
<property name="xpad">0</property>
<property name="ypad">0</property>
</widget>
</child>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="button_export_snippets">
<property name="visible">True</property>
<property name="tooltip" translatable="yes">Export selected snippets</property>
<property name="can_default">True</property>
<property name="can_focus">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_button_export_snippets_clicked" last_modification_time="Tue, 10 Jul 2007 18:37:25 GMT"/>
<child>
<widget class="GtkImage" id="image4">
<property name="visible">True</property>
<property name="stock">gtk-save</property>
<property name="icon_size">4</property>
<property name="xalign">0.5</property>
<property name="yalign">0.5</property>
<property name="xpad">0</property>
<property name="ypad">0</property>
</widget>
</child>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
......@@ -168,18 +237,30 @@
<widget class="GtkButton" id="button_remove_snippet">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="tooltip" translatable="yes">Delete selected snippet</property>
<property name="can_default">True</property>
<property name="can_focus">True</property>
<property name="label">gtk-remove</property>
<property name="use_stock">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_button_remove_snippet_clicked" last_modification_time="Mon, 19 Dec 2005 13:15:14 GMT"/>
<child>
<widget class="GtkImage" id="image_remove">
<property name="visible">True</property>
<property name="stock">gtk-delete</property>
<property name="icon_size">4</property>
<property name="xalign">0.5</property>
<property name="yalign">0.5</property>
<property name="xpad">0</property>
<property name="ypad">0</property>
</widget>
</child>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="pack_type">GTK_PACK_END</property>
</packing>
</child>
</widget>
......@@ -211,7 +292,7 @@
<child>
<widget class="GtkLabel" id="label4">
<property name="visible">True</property>
<property name="label" translatable="yes">_Edit snippet:</property>
<property name="label" translatable="yes">_Edit:</property>
<property name="use_underline">True</property>
<property name="use_markup">False</property>
<property name="justify">GTK_JUSTIFY_LEFT</property>
......@@ -367,35 +448,6 @@
</packing>
</child>
<child>
<widget class="GtkLabel" id="label_accelerator">
<property name="visible">True</property>
<property name="label" translatable="yes">S_hortcut key:</property>
<property name="use_underline">True</property>
<property name="use_markup">False</property>
<property name="justify">GTK_JUSTIFY_LEFT</property>
<property name="wrap">False</property>
<property name="selectable">False</property>
<property name="xalign">0.5</property>
<property name="yalign">0.5</property>
<property name="xpad">0</property>
<property name="ypad">0</property>
<property name="mnemonic_widget">entry_accelerator</property>
<property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
<property name="width_chars">-1</property>
<property name="single_line_mode">False</property>
<property name="angle">0</property>
</widget>
<packing>
<property name="left_attach">0</property>
<property name="right_attach">1</property>
<property name="top_attach">1</property>
<property name="bottom_attach">2</property>
<property name="x_options">fill</property>
<property name="y_options"></property>
</packing>
</child>
<child>
<widget class="GtkEntry" id="entry_tab_trigger">
<property name="visible">True</property>
......@@ -444,6 +496,35 @@
<property name="y_options"></property>
</packing>
</child>
<child>
<widget class="GtkLabel" id="label_accelerator">
<property name="visible">True</property>
<property name="label" translatable="yes">S_hortcut key:</property>
<property name="use_underline">True</property>
<property name="use_markup">False</property>
<property name="justify">GTK_JUSTIFY_LEFT</property>
<property name="wrap">False</property>
<property name="selectable">False</property>
<property name="xalign">0</property>
<property name="yalign">0.5</property>
<property name="xpad">0</property>
<property name="ypad">0</property>
<property name="mnemonic_widget">entry_accelerator</property>
<property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
<property name="width_chars">-1</property>
<property name="single_line_mode">False</property>
<property name="angle">0</property>
</widget>
<packing>
<property name="left_attach">0</property>
<property name="right_attach">1</property>
<property name="top_attach">1</property>
<property name="bottom_attach">2</property>
<property name="x_options">fill</property>
<property name="y_options"></property>
</packing>
</child>
</widget>
<packing>
<property name="padding">0</property>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment