1 | Importing and Exporting Things |
---|
2 | ****************************** |
---|
3 | |
---|
4 | The waeup package tries to support all special object types to be |
---|
5 | exportable and importable. |
---|
6 | |
---|
7 | :Test-Layer: unit |
---|
8 | |
---|
9 | As imports and exports of data are a cruical point when doing updates |
---|
10 | or to enable data backups, this support must be considered a serious |
---|
11 | topic for the base functionality of the whole software. |
---|
12 | |
---|
13 | Exports and imports are implemented using the Zope component framework |
---|
14 | capabilities. This means we need some adapters and utilities, which we |
---|
15 | register, by grokking the whole package:: |
---|
16 | |
---|
17 | >>> import grok |
---|
18 | >>> grok.testing.grok('waeup') |
---|
19 | |
---|
20 | |
---|
21 | Exporting XML |
---|
22 | ============= |
---|
23 | |
---|
24 | One of the major formats we support for data export is XML. |
---|
25 | |
---|
26 | When we want to export an object, we can choose the destination |
---|
27 | file. If we do not give a file to write to, we get a StringIO object, |
---|
28 | i.e. an in-memory file. |
---|
29 | |
---|
30 | To show this basic functionality provided for all XML exportable |
---|
31 | objects, we choose faculties. |
---|
32 | |
---|
33 | We 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 | |
---|
39 | Exporting plain XML |
---|
40 | ------------------- |
---|
41 | |
---|
42 | Now let's export it to XML. To do this we first ask the framework for |
---|
43 | an 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 | |
---|
50 | All exporters provide an ``export(<obj>)`` method:: |
---|
51 | |
---|
52 | >>> result = exporter.export() |
---|
53 | |
---|
54 | The 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 | |
---|
61 | Generating XML files |
---|
62 | -------------------- |
---|
63 | |
---|
64 | We 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 | |
---|
74 | Clean up:: |
---|
75 | |
---|
76 | >>> import os |
---|
77 | >>> os.unlink('myexport.xml') |
---|
78 | |
---|
79 | Importing XML |
---|
80 | ============= |
---|
81 | |
---|
82 | We can generate objects from XML. |
---|
83 | |
---|
84 | Let's create a faculty instance, that we want to be restored |
---|
85 | afterwards:: |
---|
86 | |
---|
87 | >>> from waeup.sirp.university.faculty import Faculty |
---|
88 | >>> myfaculty = Faculty() |
---|
89 | >>> myfaculty.name = 'My very faculty.' |
---|
90 | |
---|
91 | We 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 | |
---|
97 | We change the name of the faculty:: |
---|
98 | |
---|
99 | >>> myfaculty.name = 'Another name' |
---|
100 | |
---|
101 | Now we create an importer for that file:: |
---|
102 | |
---|
103 | >>> from waeup.sirp.interfaces import IWAeUPXMLImporter |
---|
104 | >>> importer = IWAeUPXMLImporter(myfaculty) |
---|
105 | |
---|
106 | Importing from filenames |
---|
107 | ------------------------ |
---|
108 | |
---|
109 | We can use a filepath to import from the denoted file:: |
---|
110 | |
---|
111 | >>> new_obj = importer.doImport('myexport.xml') |
---|
112 | |
---|
113 | The 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 | |
---|
121 | The new object is really a new object:: |
---|
122 | |
---|
123 | >>> new_obj is myfaculty |
---|
124 | False |
---|
125 | |
---|
126 | Importing from filelike objects |
---|
127 | ------------------------------- |
---|
128 | |
---|
129 | We 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 | |
---|
134 | The object created is indeed a faculty:: |
---|
135 | |
---|
136 | >>> new_obj |
---|
137 | <waeup.sirp.university.faculty.Faculty object at 0x...> |
---|
138 | |
---|
139 | Clean up:: |
---|
140 | |
---|
141 | >>> import os |
---|
142 | >>> os.unlink('myexport.xml') |
---|