fixed PickeItemExporter bug, added unittest, and added pickle to suported feed exports formats

This commit is contained in:
Pablo Hoffman 2011-10-25 02:36:51 -02:00
parent 8bdf288428
commit c38c49d56a
4 changed files with 28 additions and 4 deletions

View File

@ -64,6 +64,14 @@ XML
* :setting:`FEED_FORMAT`: ``xml``
* Exporter used: :class:`~scrapy.contrib.exporter.XmlItemExporter`
.. _topics-feed-format-pickle:
Pickle
------
* :setting:`FEED_FORMAT`: ``pickle``
* Exporter used: :class:`~scrapy.contrib.exporter.PickleItemExporter`
.. _topics-feed-format-marshal:
Marshal

View File

@ -5,7 +5,7 @@ Item Exporters are used to export/serialize items into different formats.
import csv
import pprint
import marshal
from cPickle import Pickler
import cPickle as pickle
from xml.sax.saxutils import XMLGenerator
from scrapy.utils.py26 import json
@ -178,12 +178,14 @@ class CsvItemExporter(BaseItemExporter):
class PickleItemExporter(BaseItemExporter):
def __init__(self, file, protocol=0, **kwargs):
def __init__(self, file, protocol=2, **kwargs):
self._configure(kwargs)
self.pickler = Pickler(file, protocol)
self.file =file
self.protocol = protocol
def export_item(self, item):
self.pickler.dump(dict(self._get_serialized_fields(item)))
d = dict(self._get_serialized_fields(item))
pickle.dump(d, self.file, self.protocol)
class MarshalItemExporter(BaseItemExporter):

View File

@ -164,6 +164,7 @@ FEED_EXPORTERS_BASE = {
'csv': 'scrapy.contrib.exporter.CsvItemExporter',
'xml': 'scrapy.contrib.exporter.XmlItemExporter',
'marshal': 'scrapy.contrib.exporter.MarshalItemExporter',
'pickle': 'scrapy.contrib.exporter.PickleItemExporter',
}
HTTPCACHE_ENABLED = False

View File

@ -88,6 +88,19 @@ class PickleItemExporterTest(BaseItemExporterTest):
def _check_output(self):
self._assert_expected_item(pickle.loads(self.output.getvalue()))
def test_export_multiple_items(self):
i1 = TestItem(name='hello', age='world')
i2 = TestItem(name='bye', age='world')
f = StringIO()
ie = PickleItemExporter(f)
ie.start_exporting()
ie.export_item(i1)
ie.export_item(i2)
ie.finish_exporting()
f.reset()
self.assertEqual(pickle.load(f), i1)
self.assertEqual(pickle.load(f), i2)
class CsvItemExporterTest(BaseItemExporterTest):