source: main/waeup.sirp/trunk/src/waeup/sirp/utils/csvimport.txt @ 4925

Last change on this file since 4925 was 4920, checked in by uli, 15 years ago

Make unit tests run again with the new package layout.

File size: 2.2 KB
Line 
1:mod:`waeup.sirp.utils.csvimport` -- CSV file importers
2*******************************************************
3
4.. module:: waeup.sirp.utils.csvimport
5
6Components for importing CSV files.
7
8:Test-Layer: unit
9
10Because CSV importers make use of components registered with the Zope
11Component Architecture (ZCA), we first have to grok the `waeup`
12package. This happens automatically in real-world use:
13
14    >>> import grok
15    >>> grok.testing.grok('waeup')
16
17Importing from CSV files
18------------------------
19
20We create a facultycontainer, which we want to populate with some CSV
21data afterwards:
22
23    >>> from waeup.sirp.university.facultycontainer import FacultyContainer
24    >>> mycontainer = FacultyContainer()
25
26Next, 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
35We get a suitable importer by asking for a multi-adapter to
36our file and IWAeUPCSVExporter (see section above, the importexport
37module 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
48This 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
58The container is still empty:
59
60    >>> len(mycontainer)
61    0
62
63Now we can easily import the data:
64
65    >>> importer.doImport()
66
67The 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
76Clean up:
77
78    >>> import os
79    >>> os.unlink('myfaculties.csv')
Note: See TracBrowser for help on using the repository browser.