[CMF-checkins] SVN: CMF/trunk/C Add export/import adapters for objects which handle their own DAV PUT/manageFTPget.

Tres Seaver tseaver at palladion.com
Tue Sep 27 22:24:50 EDT 2005


Log message for revision 38659:
  Add export/import adapters for objects which handle their own DAV PUT/manageFTPget.

Changed:
  U   CMF/trunk/CHANGES.txt
  U   CMF/trunk/CMFCore/exportimport.py
  U   CMF/trunk/CMFCore/interfaces/_exportimport.py
  U   CMF/trunk/CMFCore/tests/test_exportimport.py

-=-
Modified: CMF/trunk/CHANGES.txt
===================================================================
--- CMF/trunk/CHANGES.txt	2005-09-28 01:57:49 UTC (rev 38658)
+++ CMF/trunk/CHANGES.txt	2005-09-28 02:24:49 UTC (rev 38659)
@@ -2,6 +2,10 @@
 
   New Features
 
+    - CMFCore.exportimport:  Added framework and interfaces for exporting
+      and importing content using the export / import contexts provided by
+      GenericSetup.
+
     - CMFSetup: Split off GenericSetup.
       GenericSetup allows to use CMFSetup functionality outside CMF. See the
       README.txt of GenericSetup for details.

Modified: CMF/trunk/CMFCore/exportimport.py
===================================================================
--- CMF/trunk/CMFCore/exportimport.py	2005-09-28 01:57:49 UTC (rev 38658)
+++ CMF/trunk/CMFCore/exportimport.py	2005-09-28 02:24:49 UTC (rev 38659)
@@ -259,3 +259,55 @@
                                 'no .ini file for %s/%s' % (subdir, cid))
         else:
             self.context.put_ini(data)
+
+
+class FauxDAVRequest:
+
+    def __init__(self, **kw):
+        self._data = {}
+        self._headers = {}
+        self._data.update(kw)
+
+    def get(self, key, default=None):
+        return self._data.get(key, default)
+
+    def get_header(self, key, default=None):
+        return self._headers.get(key, default)
+
+class FauxDAVResponse:
+    pass
+
+class DAVAwareFileAdapter(object):
+    """ Exporter/importer for content who handle their own FTP / DAV PUTs.
+    """
+    implements(IFilesystemExporter, IFilesystemImporter)
+
+    def __init__(self, context):
+        self.context = context
+
+    def export(self, export_context, subdir):
+        """ See IFilesystemExporter.
+        """
+        export_context.writeDataFile('%s' % self.context.getId(),
+                                     self.context.manage_FTPget(),
+                                     'text/plain',
+                                     subdir,
+                                    )
+
+    def listExportableItems(self):
+        """ See IFilesystemExporter.
+        """
+        return ()
+
+    def import_(self, import_context, subdir):
+        """ See IFilesystemImporter.
+        """
+        cid = self.context.getId()
+        data = import_context.readDataFile('%s' % cid, subdir)
+        if data is None:
+            import_context.note('SGAIFA',
+                                'no .ini file for %s/%s' % (subdir, cid))
+        else:
+            request = FauxDAVRequest(body=data)
+            response = FauxDAVResponse()
+            self.context.PUT(request, response)

Modified: CMF/trunk/CMFCore/interfaces/_exportimport.py
===================================================================
--- CMF/trunk/CMFCore/interfaces/_exportimport.py	2005-09-28 01:57:49 UTC (rev 38658)
+++ CMF/trunk/CMFCore/interfaces/_exportimport.py	2005-09-28 02:24:49 UTC (rev 38659)
@@ -74,3 +74,23 @@
         o 'stream_or_text' must be either a string, or else a stream
           directly parseable by ConfigParser.
         """
+
+class IDAVAware(Interface):
+    """ Interface for objects which handle their own FTP / DAV operations.
+    """
+    def getId():
+        """ Return the Zope id of the object.
+        """
+
+    def manage_FTPget():
+        """ Return a string representing the object as a file.
+        """
+
+    def PUT(REQUEST, RESPONSE):
+        """ Parse file content and update the object.
+
+        o 'REQUEST' will have a 'get' method, which will have the 
+          content object in its "BODY" key.  It will also have 'get_header'
+          method, whose headers (e.g., "Content-Type") may affect the
+          processing of the body.
+        """

Modified: CMF/trunk/CMFCore/tests/test_exportimport.py
===================================================================
--- CMF/trunk/CMFCore/tests/test_exportimport.py	2005-09-28 01:57:49 UTC (rev 38658)
+++ CMF/trunk/CMFCore/tests/test_exportimport.py	2005-09-28 02:24:49 UTC (rev 38659)
@@ -602,7 +602,7 @@
 
 class INIAwareFileAdapterTests(unittest.TestCase,
                                ConformsToIFilesystemExporter,
-                               ConformsToIFilesystemImporter
+                               ConformsToIFilesystemImporter,
                                ):
 
     def _getTargetClass(self):
@@ -640,6 +640,41 @@
         self.assertEqual(parser.get('DEFAULT', 'description'), 'abc')
 
 
+class DAVAwareFileAdapterTests(unittest.TestCase,
+                               ConformsToIFilesystemExporter,
+                               ConformsToIFilesystemImporter,
+                               ):
+
+    def _getTargetClass(self):
+        from Products.CMFCore.exportimport import DAVAwareFileAdapter
+        return DAVAwareFileAdapter
+
+    def _makeOne(self, context, *args, **kw):
+        return self._getTargetClass()(context, *args, **kw)
+
+    def test_export_dav_file(self):
+        dav_file = _makeDAVAware('dav_file.html')
+        adapter = self._makeOne(dav_file)
+        context = DummyExportContext(None)
+        adapter.export(context, 'subpath/to')
+
+        self.assertEqual(len(context._wrote), 1)
+        filename, text, content_type = context._wrote[0]
+        self.assertEqual(filename, 'subpath/to/dav_file.html')
+        self.assertEqual(content_type, 'text/plain')
+        self.assertEqual(text.strip(), dav_file.manage_FTPget().strip())
+
+    def test_import_dav_file(self):
+        VALUES = ('Title: dav_file', 'Description: abc', 'body goes here')
+        dav_file = _makeDAVAware('dav_file.html')
+        adapter = self._makeOne(dav_file)
+        context = DummyImportContext(None)
+        context._files['subpath/to/dav_file.html'] = KNOWN_DAV % VALUES
+
+        adapter.import_(context, 'subpath/to')
+        text = dav_file._was_put == KNOWN_DAV % VALUES
+
+
 TEST_CSV_AWARE = 'Test CSV Aware'
 KNOWN_CSV = """\
 one,two,three
@@ -707,6 +742,43 @@
     return aware
 
 
+TEST_DAV_AWARE = 'Test DAV Aware'
+KNOWN_DAV = """\
+Title: %s
+Description: %s
+
+%s
+"""
+
+def _makeDAVAware(id):
+    from OFS.SimpleItem import SimpleItem
+    from zope.interface import implements
+    from Products.CMFCore.interfaces import IDynamicType
+    from Products.CMFCore.interfaces import IDAVAware
+
+    class _TestDAVAware(SimpleItem):
+        implements(IDynamicType, IDAVAware)
+        _was_put = None
+        title = 'DAV title'
+        description = 'DAV description'
+        body = 'DAV body'
+        portal_type = TEST_DAV_AWARE
+
+        def getPortalTypeName(self):
+            return self.portal_type
+
+        def manage_FTPget(self):
+            return KNOWN_DAV % (self.title, self.description, self.body)
+
+        def PUT(self, REQUEST, RESPONSE):
+            self._was_put = REQUEST.get('BODY', '')
+
+    aware = _TestDAVAware()
+    aware._setId(id)
+
+    return aware
+
+
 TEST_CONTENT = 'Test Content'
 
 def _makeItem(self):
@@ -776,6 +848,7 @@
     suite.addTest(unittest.makeSuite(Test_globpattern))
     suite.addTest(unittest.makeSuite(CSVAwareFileAdapterTests))
     suite.addTest(unittest.makeSuite(INIAwareFileAdapterTests))
+    suite.addTest(unittest.makeSuite(DAVAwareFileAdapterTests))
     return suite
 
 if __name__ == '__main__':



More information about the CMF-checkins mailing list