source: main/waeup.sirp/branches/henrik-bootstrap/src/waeup/sirp/utils/importexport.py @ 9819

Last change on this file since 9819 was 7321, checked in by Henrik Bettermann, 13 years ago

Replace the term 'WAeUP' by SIRP which is a WAeUP product.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1## $Id: importexport.py 7321 2011-12-10 06:15:17Z 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##
18import csv
19import grok
20import cPickle
21import zope.xmlpickle
22from cStringIO import StringIO
23from zope.interface import Interface
24from waeup.sirp.interfaces import (ISIRPObject, ISIRPExporter,
25                                   ISIRPXMLExporter, ISIRPXMLImporter)
26
27def 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
37def writeFile(data):
38    writer = csv.writer(open("export.csv", "wb"))
39    writer.writerows(data)
40
41class Exporter(grok.Adapter):
42    """Export a SIRP 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(ISIRPObject)
48    grok.provides(ISIRPExporter)
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
62class XMLExporter(grok.Adapter):
63    """Export a SIRP object as XML.
64
65    This all-purpose exporter exports XML representations of pickable
66    objects.
67    """
68    grok.context(Interface)
69    grok.provides(ISIRPXMLExporter)
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
85class XMLImporter(grok.Adapter):
86    """Import a SIRP object from XML.
87    """
88    grok.context(Interface)
89    grok.provides(ISIRPXMLImporter)
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
Note: See TracBrowser for help on using the repository browser.