source: main/waeup.sirp/trunk/src/waeup/sirp/utils/importexport.txt @ 4997

Last change on this file since 4997 was 4965, checked in by uli, 15 years ago

Remove tests of quick-import functionality.

File size: 3.5 KB
Line 
1Importing and Exporting Things
2******************************
3
4The waeup package tries to support all special object types to be
5exportable and importable.
6
7:Test-Layer: unit
8
9As imports and exports of data are a cruical point when doing updates
10or to enable data backups, this support must be considered a serious
11topic for the base functionality of the whole software.
12
13Exports and imports are implemented using the Zope component framework
14capabilities. This means we need some adapters and utilities, which we
15register, by grokking the whole package::
16
17    >>> import grok
18    >>> grok.testing.grok('waeup')
19
20
21Exporting XML
22=============
23
24One of the major formats we support for data export is XML.
25
26When we want to export an object, we can choose the destination
27file. If we do not give a file to write to, we get a StringIO object,
28i.e. an in-memory file.
29
30To show this basic functionality provided for all XML exportable
31objects, we choose faculties.
32
33We can export faculties. First we create a faculty::
34
35    >>> from waeup.sirp.university.faculty import Faculty
36    >>> myfaculty = Faculty()
37    >>> myfaculty.name = 'My very faculty.'
38
39Exporting plain XML
40-------------------
41
42Now let's export it to XML. To do this we first ask the framework for
43an exporter::
44
45    >>> from waeup.sirp.interfaces import IWAeUPXMLExporter
46    >>> exporter = IWAeUPXMLExporter(myfaculty)
47    >>> exporter
48    <waeup.sirp.utils.importexport.XMLExporter object at 0x...>
49
50All exporters provide an ``export(<obj>)`` method::
51
52    >>> result = exporter.export()
53
54The result is an object of type StringIO. We therefore use its
55`read()` method to get the actual XML data::
56
57    >>> print result.read()
58    <?xml version="1.0" encoding="utf-8" ?>...
59
60
61Generating XML files
62--------------------
63
64We can also let the results be written to a file::
65
66    >>> result = exporter.export('myexport.xml')
67    >>> print open('myexport.xml', 'rb').read()
68    <?xml version="1.0" encoding="utf-8" ?>
69    <pickle>
70      <initialized_object>
71    ...
72    </pickle>
73
74Clean up::
75
76    >>> import os
77    >>> os.unlink('myexport.xml')
78
79Importing XML
80=============
81
82We can generate objects from XML.
83
84Let's create a faculty instance, that we want to be restored
85afterwards::
86
87    >>> from waeup.sirp.university.faculty import Faculty
88    >>> myfaculty = Faculty()
89    >>> myfaculty.name = 'My very faculty.'
90
91We create an XML dump of this object::
92
93    >>> from waeup.sirp.interfaces import IWAeUPXMLExporter
94    >>> exporter = IWAeUPXMLExporter(myfaculty)
95    >>> result = exporter.export('myexport.xml')
96
97We change the name of the faculty::
98
99    >>> myfaculty.name = 'Another name'
100
101Now we create an importer for that file::
102
103    >>> from waeup.sirp.interfaces import IWAeUPXMLImporter
104    >>> importer = IWAeUPXMLImporter(myfaculty)
105
106Importing from filenames
107------------------------
108
109We can use a filepath to import from the denoted file::
110
111    >>> new_obj = importer.doImport('myexport.xml')
112
113The object created is indeed a faculty::
114
115    >>> new_obj
116    <waeup.sirp.university.faculty.Faculty object at 0x...>
117
118    >>> new_obj.name
119    'My very faculty.'
120
121The new object is really a new object::
122
123    >>> new_obj is myfaculty
124    False
125
126Importing from filelike objects
127-------------------------------
128
129We can also pass a file-like object instead of a filepath::
130
131    >>> filelike_obj = open('myexport.xml', 'rb')
132    >>> new_obj = importer.doImport(filelike_obj)
133
134The object created is indeed a faculty::
135
136    >>> new_obj
137    <waeup.sirp.university.faculty.Faculty object at 0x...>
138
139Clean up::
140
141    >>> import os
142    >>> os.unlink('myexport.xml')
Note: See TracBrowser for help on using the repository browser.