Author: jmorliaguet
Date: Sun Apr  9 00:47:47 2006
New Revision: 2793

Added:
   cpsskins/branches/jmo-perspectives/standard/io/settings.py   (contents, 
props changed)
   cpsskins/branches/jmo-perspectives/standard/io/settings.zcml   (contents, 
props changed)
Modified:
   cpsskins/branches/jmo-perspectives/standard/io/configure.zcml
   cpsskins/branches/jmo-perspectives/ui/panels/io.pt
   cpsskins/branches/jmo-perspectives/ui/screens/sitemanager/configure.zcml
   cpsskins/branches/jmo-perspectives/ui/screens/sitemanager/views.py
Log:

- added a site exporter



Modified: cpsskins/branches/jmo-perspectives/standard/io/configure.zcml
==============================================================================
--- cpsskins/branches/jmo-perspectives/standard/io/configure.zcml       
(original)
+++ cpsskins/branches/jmo-perspectives/standard/io/configure.zcml       Sun Apr 
 9 00:47:47 2006
@@ -25,6 +25,8 @@
 
   <include file="setting.zcml" />
 
+  <include file="settings.zcml" />
+
   <include file="storage.zcml" />
 
   <include file="theme.zcml" />

Added: cpsskins/branches/jmo-perspectives/standard/io/settings.py
==============================================================================
--- (empty file)
+++ cpsskins/branches/jmo-perspectives/standard/io/settings.py  Sun Apr  9 
00:47:47 2006
@@ -0,0 +1,67 @@
+##############################################################################
+#
+# Copyright (c) 2005-2006 Nuxeo and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+from xml.dom.minidom import parseString
+
+from zope.app import pagetemplate
+from zope.app.interface import queryType
+from zope.component import adapts, getMultiAdapter, getUtilitiesFor
+from zope.interface import implements
+
+from cpsskins.setup.interfaces import IDataExporter, IDataImporter
+from cpsskins.setup.interfaces import IResource, ISettings, IResourceType
+from cpsskins.setup.interfaces import ISetting
+
+settings_xml = u"""<?xml version="1.0"?>
+<settings>
+  %(content)s
+</settings>
+"""
+
+class Exporter(object):
+    """Export all settings
+    """
+    implements(IDataExporter)
+
+    def __call__(self):
+        context = self.context
+        request = self.request
+
+        xml = u''
+        for name, setting in getUtilitiesFor(ISetting, context=context):
+            exporter = getMultiAdapter((setting, request), IDataExporter)
+            xml += exporter().replace('<?xml version="1.0"?>', '')
+
+        return settings_xml % {'content': xml}
+
+class Importer(object):
+    """Import settings
+    """
+    adapts(ISettings)
+    implements(IDataImporter)
+
+    def __init__(self, context):
+        self.context = context
+
+    def load(self, xml=u''):
+        context = self.context
+        dom = parseString(xml)
+        # TODO
+        print dom
+

Added: cpsskins/branches/jmo-perspectives/standard/io/settings.zcml
==============================================================================
--- (empty file)
+++ cpsskins/branches/jmo-perspectives/standard/io/settings.zcml        Sun Apr 
 9 00:47:47 2006
@@ -0,0 +1,18 @@
+<configure
+    xmlns="http://namespaces.zope.org/zope";
+    xmlns:browser="http://namespaces.zope.org/browser";>
+
+  <!-- XML export -->
+  <browser:view
+      provides="cpsskins.setup.interfaces.IDataExporter"
+      for="cpsskins.setup.interfaces.ISettings"
+      class=".settings.Exporter"
+      permission="zope.ManageContent"
+  />
+
+  <!-- XML import -->
+  <adapter
+      factory=".settings.Importer"
+  />
+
+</configure>

Modified: cpsskins/branches/jmo-perspectives/ui/panels/io.pt
==============================================================================
--- cpsskins/branches/jmo-perspectives/ui/panels/io.pt  (original)
+++ cpsskins/branches/jmo-perspectives/ui/panels/io.pt  Sun Apr  9 00:47:47 2006
@@ -5,6 +5,7 @@
   <span class="description">export, import themes and settings ...</span>
 
   <ul>
+    <li><a href="@@exportSite">Export site</a></li>
     <li><a href="@@exportThemes">Export themes</a></li>
     <li><a href="@@exportSettings">Export settings</a></li>
     <li><a href="@@exportStorage?name=relations">Export relations</a></li>

Modified: 
cpsskins/branches/jmo-perspectives/ui/screens/sitemanager/configure.zcml
==============================================================================
--- cpsskins/branches/jmo-perspectives/ui/screens/sitemanager/configure.zcml    
(original)
+++ cpsskins/branches/jmo-perspectives/ui/screens/sitemanager/configure.zcml    
Sun Apr  9 00:47:47 2006
@@ -72,6 +72,11 @@
         attribute="exportThemes"
     />
 
+    <page
+        name="exportSite"
+        attribute="exportSite"
+    />
+
   </pages>
 
 </configure>

Modified: cpsskins/branches/jmo-perspectives/ui/screens/sitemanager/views.py
==============================================================================
--- cpsskins/branches/jmo-perspectives/ui/screens/sitemanager/views.py  
(original)
+++ cpsskins/branches/jmo-perspectives/ui/screens/sitemanager/views.py  Sun Apr 
 9 00:47:47 2006
@@ -17,6 +17,12 @@
 """
 __docformat__ = "reStructuredText"
 
+import time
+
+from StringIO import StringIO
+from tarfile import TarFile
+from tarfile import TarInfo
+
 from zope.app.interface import queryType
 from zope.app.publisher.browser import BrowserView
 from zope.component import getUtilitiesFor, getUtility, getMultiAdapter
@@ -24,26 +30,26 @@
 
 from cpsskins import minjson as json
 from cpsskins.elements.interfaces import IType
-from cpsskins.setup.interfaces import ISetting, IGlobalSetting, IDataExporter
+from cpsskins.setup.interfaces import ISettings, ISetting, IGlobalSetting
+from cpsskins.setup.interfaces import IGlobalSetting, IDataExporter
 from cpsskins.setup.interfaces import IResourceType, IResourceManager, 
IResource
 from cpsskins.setup.registration import reloadSetting, refreshSettings
 from cpsskins.utils import getThemeManager
 
-settings_xml = """<?xml version="1.0"?>
-<settings>
-  %(content)s
-</settings>
-"""
-
 themes_xml = """<?xml version="1.0"?>
 <themes>
   %(content)s
 </themes>
 """
 
-
 class SiteDesignerView(BrowserView):
 
+    def _setAttachment(self, filename=u'', content_type=u''):
+        response = self.request.response
+        response.setHeader('content-type', content_type)
+        response.setHeader('Content-disposition',
+                           'attachment; filename=%s' % filename)
+
     ###################################################################
     # Settings
     ###################################################################
@@ -85,7 +91,7 @@
         resources = getUtility(IResourceManager)
         resources.unregister(name=name, context=self.context)
 
-    def exportSetting(self, name=u''):
+    def exportSetting(self, name=u'', interactive=True):
         """Export the setting as an XML file.
         """
         context = self.context
@@ -97,14 +103,8 @@
         resource = IResource(setting).getResource()
         resource_type = queryType(resource, IResourceType)
 
-        filename = name.replace(' ', '_')
-        setting_filename = '%s.xml' % filename
-
-        response = request.response
-        response.setHeader('content-type', 'text/xml')
-        response.setHeader('Content-disposition',
-                           'attachment; filename=%s' % setting_filename)
-
+        if interactive:
+            self._setAttachment(u'%s.xml' % name.replace(' ', '_'), 'text/xml')
         exporter = getMultiAdapter((setting, request), IDataExporter)
         return exporter()
 
@@ -118,31 +118,38 @@
         """
         refreshSettings()
 
-    def exportSettings(self):
-        """Export the storage as an XML file.
+    def exportSettings(self, interactive=True):
+        """Export the settings as an XML file.
         """
         context = self.context
         request = self.request
 
-        settings_filename = u'settings.xml'
+        if interactive:
+            self._setAttachment(u'settings.xml', u'text/xml')
 
-        response = request.response
-        response.setHeader('content-type', 'text/xml')
-        response.setHeader('Content-disposition',
-                           'attachment; filename=%s' % settings_filename)
-        xml = u''
-        for name, setting in getUtilitiesFor(ISetting, context=context):
-            exporter = getMultiAdapter((setting, request), IDataExporter)
-            xml += exporter().replace('<?xml version="1.0"?>', '')
+        settings = getUtility(ISettings, context=context)
+        exporter = getMultiAdapter((settings, request), IDataExporter)
+        return exporter()
 
-        return settings_xml % {'content': xml}
+    def exportSite(self):
+        """Export the entire site an XML file.
+        """
+        archive = TarArchive('site.tgz')
 
+        archive[u'themes.xml'] = self.exportThemes(False)
+        archive[u'settings.xml'] = self.exportSettings(False)
+
+        for storage in u'formats', u'displays', u'relations', u'portlets':
+            archive['%s.xml' % storage] = self.exportStorage(storage, False)
+
+        self._setAttachment(u'site.tgz', 'application/gz')
+        return archive()
 
     ###################################################################
     # Storage
     ###################################################################
 
-    def exportStorage(self, name=u''):
+    def exportStorage(self, name=u'', interactive=True):
         """Export the storage as an XML file.
         """
         context = self.context
@@ -153,13 +160,8 @@
         mgr = getThemeManager()
         storage = mgr[name]
 
-        filename = name.replace(' ', '_')
-        storage_filename = '%s.xml' % filename
-
-        response = request.response
-        response.setHeader('content-type', 'text/xml')
-        response.setHeader('Content-disposition',
-                           'attachment; filename=%s' % storage_filename)
+        if interactive:
+            self._setAttachment(u'%s.xml' % name.replace(' ', '_'), 'text/xml')
 
         exporter = getMultiAdapter((storage, request), IDataExporter)
         return exporter()
@@ -168,18 +170,14 @@
     # Themes
     ###################################################################
 
-    def exportThemes(self):
+    def exportThemes(self, interactive=True):
         """Export the themes
         """
         context = self.context
         request = self.request
 
-        themes_filename = u'themes.xml'
-
-        response = request.response
-        response.setHeader('content-type', 'text/xml')
-        response.setHeader('Content-disposition',
-                           'attachment; filename=%s' % themes_filename)
+        if interactive:
+            self._setAttachment(u'themes.xml', 'text/xml')
 
         xml = u''
         for theme in getThemeManager().getThemes():
@@ -187,3 +185,21 @@
             xml += exporter().replace('<?xml version="1.0"?>', '')
 
         return themes_xml % {'content': xml}
+
+
+class TarArchive:
+
+    def __init__(self, filename=u''):
+        self.stream = StringIO()
+        self.archive = TarFile.open(str(filename), 'w:gz', self.stream)
+
+    def __setitem__(self, k, v):
+        stream = StringIO(v)
+        info = TarInfo(k)
+        info.size = len(v)
+        info.mtime = time.time()
+        self.archive.addfile(info, stream)
+
+    def __call__(self):
+        self.archive.close()
+        return self.stream.getvalue()
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to