1 | :mod:`waeup.sirp.utils.csvimport` -- CSV file importers |
---|
2 | ******************************************************* |
---|
3 | |
---|
4 | .. module:: waeup.sirp.utils.csvimport |
---|
5 | |
---|
6 | Components for importing CSV files. |
---|
7 | |
---|
8 | :Test-Layer: unit |
---|
9 | |
---|
10 | Because CSV importers make use of components registered with the Zope |
---|
11 | Component Architecture (ZCA), we first have to grok the `waeup` |
---|
12 | package. This happens automatically in real-world use: |
---|
13 | |
---|
14 | >>> import grok |
---|
15 | >>> grok.testing.grok('waeup') |
---|
16 | |
---|
17 | Importing from CSV files |
---|
18 | ------------------------ |
---|
19 | |
---|
20 | We create a facultycontainer, which we want to populate with some CSV |
---|
21 | data afterwards: |
---|
22 | |
---|
23 | >>> from waeup.sirp.university.facultycontainer import FacultyContainer |
---|
24 | >>> mycontainer = FacultyContainer() |
---|
25 | |
---|
26 | Next, we create a CSV file we want to import: |
---|
27 | |
---|
28 | >>> open('myfaculties.csv', 'wb').write( |
---|
29 | ... """code,review_state,title,title_prefix |
---|
30 | ... AGR,static,Agriculture,faculty |
---|
31 | ... ART,static,Arts,faculty |
---|
32 | ... DEN,static,Dentistry,faculty |
---|
33 | ... """) |
---|
34 | |
---|
35 | We get a suitable importer by asking for a multi-adapter to |
---|
36 | our file and IWAeUPCSVExporter (see section above, the importexport |
---|
37 | module and the `waeup.sirp.csvfile` README for details): |
---|
38 | |
---|
39 | >>> from waeup.sirp.csvfile import getCSVFile |
---|
40 | >>> from waeup.sirp.interfaces import IWAeUPCSVImporter |
---|
41 | >>> from zope.component import getMultiAdapter |
---|
42 | >>> importer = getMultiAdapter((getCSVFile('myfaculties.csv'), |
---|
43 | ... mycontainer), |
---|
44 | ... IWAeUPCSVImporter) |
---|
45 | >>> importer |
---|
46 | <waeup.sirp.utils.csvimport.FacultyCSVImporter object at 0x...> |
---|
47 | |
---|
48 | This importer complies with the IWAeUPCSVImporter interface: |
---|
49 | |
---|
50 | >>> from waeup.sirp.utils.csvimport import FacultyCSVImporter |
---|
51 | >>> IWAeUPCSVImporter.implementedBy(FacultyCSVImporter) |
---|
52 | True |
---|
53 | |
---|
54 | >>> from zope.interface.verify import verifyClass |
---|
55 | >>> verifyClass(IWAeUPCSVImporter, FacultyCSVImporter) |
---|
56 | True |
---|
57 | |
---|
58 | The container is still empty: |
---|
59 | |
---|
60 | >>> len(mycontainer) |
---|
61 | 0 |
---|
62 | |
---|
63 | Now we can easily import the data: |
---|
64 | |
---|
65 | >>> importer.doImport() |
---|
66 | |
---|
67 | The container is now filled: |
---|
68 | |
---|
69 | >>> len(mycontainer) |
---|
70 | 3 |
---|
71 | |
---|
72 | >>> print [(x.code, x.title) for x in list(mycontainer.values())] |
---|
73 | [('AGR', 'Agriculture'), ('ART', 'Arts'), ('DEN', 'Dentistry')] |
---|
74 | |
---|
75 | |
---|
76 | Clean up: |
---|
77 | |
---|
78 | >>> import os |
---|
79 | >>> os.unlink('myfaculties.csv') |
---|