Back to index

plone3  3.1.7
export.py
Go to the documentation of this file.
00001 # Marshall: A framework for pluggable marshalling policies
00002 # Copyright (C) 2004-2006 Enfold Systems, LLC
00003 #
00004 # This program is free software; you can redistribute it and/or modify
00005 # it under the terms of the GNU General Public License as published by
00006 # the Free Software Foundation; either version 2 of the License, or
00007 # (at your option) any later version.
00008 #
00009 # This program is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 # GNU General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU General Public License
00015 # along with this program; if not, write to the Free Software
00016 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 #
00018 """
00019 $Id: export.py 6805 2006-08-25 12:57:16Z yenzenz $
00020 """
00021 
00022 import os
00023 import tempfile
00024 import zipfile
00025 import shutil
00026 from cStringIO import StringIO
00027 from Globals import InitializeClass
00028 from ExtensionClass import Base
00029 from AccessControl import ClassSecurityInfo
00030 from Acquisition import aq_base
00031 from Products.CMFCore.utils import getToolByName
00032 from Products.CMFCore.permissions import ManagePortal
00033 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
00034 
00035 class Export(Base):
00036 
00037     security = ClassSecurityInfo()
00038 
00039     security.declareProtected(ManagePortal, 'atxml_template')
00040     atxml_template = PageTemplateFile(
00041         'www/atxml', globals(),
00042         __name__ = 'atxml_template')
00043 
00044     security.declareProtected(ManagePortal, 'marshall_data')
00045     def marshall_data(self, obj):
00046         from Products.Marshall.registry import getComponent
00047         marshaller = getComponent('primary_field')
00048         return self.marshall(obj, marshaller)
00049 
00050     security.declareProtected(ManagePortal, 'marshall_metadata')
00051     def marshall_metadata(self, obj):
00052         from Products.Marshall.registry import getComponent
00053         marshaller = getComponent('atxml')
00054         return self.marshall(obj, marshaller)
00055 
00056     security.declareProtected(ManagePortal, 'marshall')
00057     def marshall(self, obj, marshaller):
00058         REQUEST = obj.REQUEST
00059         RESPONSE = REQUEST.RESPONSE
00060         ddata = marshaller.marshall(obj, REQUEST=REQUEST,
00061                                     RESPONSE=RESPONSE)
00062         if hasattr(aq_base(obj), 'marshall_hook') \
00063            and obj.marshall_hook:
00064             ddata = obj.marshall_hook(ddata)
00065 
00066         content_type, length, data = ddata
00067 
00068         if type(data) is type(''): return StringIO(data)
00069 
00070         s = StringIO()
00071         while data is not None:
00072             s.write(data.data)
00073             data=data.next
00074         s.seek(0)
00075         return s
00076 
00077     security.declareProtected(ManagePortal, 'export')
00078     def export(self, context, paths):
00079         data = StringIO()
00080         out = zipfile.ZipFile(data, 'w')
00081 
00082         for path in paths:
00083             filename = os.path.basename(path)
00084             dir_path = os.path.dirname(path)
00085             obj = context.restrictedTraverse(path)
00086 
00087             # Write data
00088             fpath = os.path.join(dir_path, filename)
00089             stream = self.marshall_data(obj)
00090             out.writestr(fpath, stream.read())
00091 
00092             # Write metadata
00093             metadata_path = os.path.join(dir_path, '.metadata')
00094             fpath = os.path.join(metadata_path, filename)
00095             stream = self.marshall_metadata(obj)
00096             out.writestr(fpath, stream.read())
00097         out.close()
00098         data.seek(0)
00099         return data
00100 
00101     security.declareProtected(ManagePortal, 'export_info')
00102     def export_info(self, context, info):
00103         data = StringIO()
00104         out = zipfile.ZipFile(data, 'w')
00105 
00106         for d in info:
00107             path = d.get('path', d.get('id'))
00108             filename = os.path.basename(path)
00109             dir_path = os.path.dirname(path)
00110 
00111             # Write data
00112             _d = d.get('data', '')
00113             fpath = os.path.join(dir_path, filename)
00114             out.writestr(fpath, _d)
00115 
00116             metadata = d.copy()
00117             for name in ('data', 'path'):
00118                 if metadata.has_key(name):
00119                     del metadata[name]
00120             # Write metadata
00121             metadata_path = os.path.join(dir_path, '.metadata')
00122             fpath = os.path.join(metadata_path, filename)
00123             _d = str(self.atxml_template(info=metadata))
00124             out.writestr(fpath, _d)
00125 
00126         out.close()
00127         data.seek(0)
00128         return data
00129 
00130 InitializeClass(Export)