1 | ## $Id: importexport.py 11949 2014-11-13 14:40:27Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | import unicodecsv as csv # XXX: csv ops should move to dedicated module |
---|
19 | import grok |
---|
20 | import cPickle |
---|
21 | import zope.xmlpickle |
---|
22 | from cStringIO import StringIO |
---|
23 | from zope.interface import Interface |
---|
24 | from waeup.ikoba.interfaces import (IIkobaObject, IIkobaExporter, |
---|
25 | IIkobaXMLExporter, IIkobaXMLImporter) |
---|
26 | |
---|
27 | def readFile(f): |
---|
28 | """read a CSV file""" |
---|
29 | headers = [] |
---|
30 | rows = [] |
---|
31 | f = csv.reader(f) |
---|
32 | headers = f.next() |
---|
33 | for line in f: |
---|
34 | rows.append(line) |
---|
35 | return (headers, rows) |
---|
36 | |
---|
37 | def writeFile(data): |
---|
38 | writer = csv.writer(open("export.csv", "wb")) |
---|
39 | writer.writerows(data) |
---|
40 | |
---|
41 | class Exporter(grok.Adapter): |
---|
42 | """Export a Ikoba object as pickle. |
---|
43 | |
---|
44 | This all-purpose exporter exports attributes defined in schemata |
---|
45 | and contained objects (if the exported thing is a container). |
---|
46 | """ |
---|
47 | grok.context(IIkobaObject) |
---|
48 | grok.provides(IIkobaExporter) |
---|
49 | |
---|
50 | def __init__(self, context): |
---|
51 | self.context = context |
---|
52 | |
---|
53 | def export(self, filepath=None): |
---|
54 | if filepath is None: |
---|
55 | filelike_obj = StringIO() |
---|
56 | else: |
---|
57 | filelike_obj = open(filepath, 'wb') |
---|
58 | exported_obj = cPickle.dump(self.context, filelike_obj) |
---|
59 | filelike_obj.close() |
---|
60 | return filelike_obj |
---|
61 | |
---|
62 | class XMLExporter(grok.Adapter): |
---|
63 | """Export a Ikoba object as XML. |
---|
64 | |
---|
65 | This all-purpose exporter exports XML representations of pickable |
---|
66 | objects. |
---|
67 | """ |
---|
68 | grok.context(Interface) |
---|
69 | grok.provides(IIkobaXMLExporter) |
---|
70 | |
---|
71 | def __init__(self, context): |
---|
72 | self.context = context |
---|
73 | |
---|
74 | def export(self, filepath=None): |
---|
75 | pickled_obj = cPickle.dumps(self.context) |
---|
76 | result = zope.xmlpickle.toxml(pickled_obj) |
---|
77 | if filepath is None: |
---|
78 | filelike_obj = StringIO() |
---|
79 | else: |
---|
80 | filelike_obj = open(filepath, 'wb') |
---|
81 | filelike_obj.write(result) |
---|
82 | filelike_obj.seek(0) |
---|
83 | return filelike_obj |
---|
84 | |
---|
85 | class XMLImporter(grok.Adapter): |
---|
86 | """Import a Ikoba object from XML. |
---|
87 | """ |
---|
88 | grok.context(Interface) |
---|
89 | grok.provides(IIkobaXMLImporter) |
---|
90 | |
---|
91 | def __init__(self, context): |
---|
92 | self.context = context |
---|
93 | |
---|
94 | def doImport(self, filepath): |
---|
95 | xml = None |
---|
96 | if isinstance(filepath, basestring): |
---|
97 | xml = open(filepath, 'rb').read() |
---|
98 | else: |
---|
99 | xml = filepath.read() |
---|
100 | obj = zope.xmlpickle.loads(xml) |
---|
101 | return obj |
---|