[4789] | 1 | import re |
---|
[3527] | 2 | import csv |
---|
[4789] | 3 | import grok |
---|
| 4 | import cPickle |
---|
| 5 | import zope.xmlpickle |
---|
| 6 | from cStringIO import StringIO |
---|
| 7 | from xml.dom.minidom import Document, getDOMImplementation |
---|
| 8 | from zope.interface import Interface |
---|
| 9 | from waeup.csvfile.interfaces import ICSVFile |
---|
| 10 | from waeup.interfaces import (IWAeUPObject, IWAeUPExporter, IWAeUPXMLExporter, |
---|
| 11 | IWAeUPXMLImporter, IWAeUPCSVImporter) |
---|
[3527] | 12 | |
---|
| 13 | def readFile(f): |
---|
| 14 | """read a CSV file""" |
---|
| 15 | headers = [] |
---|
| 16 | rows = [] |
---|
| 17 | f = csv.reader(f) |
---|
| 18 | headers = f.next() |
---|
| 19 | for line in f: |
---|
| 20 | rows.append(line) |
---|
| 21 | return (headers, rows) |
---|
| 22 | |
---|
| 23 | def writeFile(data): |
---|
| 24 | writer = csv.writer(open("export.csv", "wb")) |
---|
| 25 | writer.writerows(data) |
---|
[4789] | 26 | |
---|
| 27 | class Exporter(grok.Adapter): |
---|
| 28 | """Export a WAeUP object as pickle. |
---|
| 29 | |
---|
| 30 | This all-purpose exporter exports attributes defined in schemata |
---|
| 31 | and contained objects (if the exported thing is a container). |
---|
| 32 | """ |
---|
| 33 | grok.context(IWAeUPObject) |
---|
| 34 | grok.provides(IWAeUPExporter) |
---|
| 35 | |
---|
| 36 | def __init__(self, context): |
---|
| 37 | self.context = context |
---|
| 38 | |
---|
| 39 | def export(self, filepath=None): |
---|
| 40 | if filepath is None: |
---|
| 41 | filelike_obj = StringIO() |
---|
| 42 | else: |
---|
| 43 | filelike_obj = open(filepath, 'wb') |
---|
| 44 | exported_obj = cPickle.dump(self.context, filelike_obj) |
---|
| 45 | filelike_obj.close() |
---|
| 46 | return filelike_obj |
---|
| 47 | |
---|
| 48 | class XMLExporter(grok.Adapter): |
---|
| 49 | """Export a WAeUP object as XML. |
---|
| 50 | |
---|
| 51 | This all-purpose exporter exports XML representations of pickable |
---|
| 52 | objects. |
---|
| 53 | """ |
---|
| 54 | grok.context(Interface) |
---|
| 55 | grok.provides(IWAeUPXMLExporter) |
---|
| 56 | |
---|
| 57 | def __init__(self, context): |
---|
| 58 | self.context = context |
---|
| 59 | |
---|
| 60 | def export(self, filepath=None): |
---|
| 61 | pickled_obj = cPickle.dumps(self.context) |
---|
| 62 | result = zope.xmlpickle.toxml(pickled_obj) |
---|
| 63 | if filepath is None: |
---|
| 64 | filelike_obj = StringIO() |
---|
| 65 | else: |
---|
| 66 | filelike_obj = open(filepath, 'wb') |
---|
| 67 | filelike_obj.write(result) |
---|
| 68 | filelike_obj.seek(0) |
---|
| 69 | return filelike_obj |
---|
| 70 | |
---|
| 71 | class XMLImporter(grok.Adapter): |
---|
| 72 | """Import a WAeUP object from XML. |
---|
| 73 | """ |
---|
| 74 | grok.context(Interface) |
---|
| 75 | grok.provides(IWAeUPXMLImporter) |
---|
| 76 | |
---|
| 77 | def __init__(self, context): |
---|
| 78 | self.context = context |
---|
| 79 | |
---|
| 80 | def doImport(self, filepath): |
---|
| 81 | xml = None |
---|
| 82 | if isinstance(filepath, basestring): |
---|
| 83 | xml = open(filepath, 'rb').read() |
---|
| 84 | else: |
---|
| 85 | xml = filepath.read() |
---|
| 86 | obj = zope.xmlpickle.loads(xml) |
---|
| 87 | return obj |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | class CSVImporter(grok.MultiAdapter): |
---|
| 91 | grok.adapts(ICSVFile, IWAeUPObject) |
---|
| 92 | grok.provides(IWAeUPCSVImporter) |
---|
| 93 | grok.baseclass() # We don't want this base to be registered really. |
---|
| 94 | |
---|
| 95 | def __init__(self, csvfile, receiver): |
---|
| 96 | self.csvfile = csvfile |
---|
| 97 | self.receiver = receiver |
---|
| 98 | |
---|
| 99 | def doImport(self): |
---|
| 100 | pass |
---|