Changeset 4159


Ignore:
Timestamp:
24 May 2009, 10:10:40 (15 years ago)
Author:
uli
Message:

Update tests.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • waeup/branches/ulif-rewrite/src/waeup/utils/importexport.txt

    r4059 r4159  
    141141    >>> import os
    142142    >>> os.unlink('myexport.xml')
     143
     144
     145Importing CSV data
     146==================
     147
     148The WAeUP portal supports import of CSV data for several different
     149data types like faculties, students, and the like.
     150
     151Obtaining importers
     152-------------------
     153
     154Before we actually do real imports, we have to provide so-called
     155importers, that will do the job for us. Let's see how that works.
     156
     157We first create an object into that we can import something:
     158
     159    >>> import grok
     160    >>> from waeup.interfaces import IWAeUPObject
     161    >>> class MyContainer(grok.Container):
     162    ...   #grok.implements(IWAeUPObject)
     163    ...   def foo(self):
     164    ...     print "Boo!"
     165
     166We get a suitable importer for certain objects by asking for an
     167adapter to `IWAeUPCSVImporter`:
     168
     169    >>> from waeup.interfaces import IWAeUPCSVImporter
     170
     171If, however, we try to create an importer for our container, we won't
     172find one right now:
     173
     174    >>> mycontainer = MyContainer()
     175    >>> importer = IWAeUPCSVImporter(mycontainer)
     176    Traceback (most recent call last):
     177    ...
     178    TypeError: ('Could not adapt', <MyContainer ...>,
     179                <InterfaceClass ...IWAeUPCSVImporter>)
     180
     181The reason is, that there is simply no importer defined for
     182`MyContainer` objects.
     183
     184As an importer must take care for the specific structure of objects
     185that receive the data, we must first define an importer for our
     186special container class:
     187
     188    >>> import grok
     189    >>> from waeup.utils.importexport import CSVImporter
     190    >>> class MyImporter(CSVImporter):
     191    ...   grok.context(MyContainer)
     192    ...   datatype = u'My Stuff'
     193    ...   column_terms = ['col1', 'col2']
     194    ...   def doImport(self, filepath, clear_old_data=True,
     195    ...                                overwrite=True):
     196    ...     print "Data imported!"
     197
     198This importer derives from `CSVImporter`. The `CSVImporter` base class
     199makes our importer automatically an adapter. Therefore we have to give
     200the type of object we provide this importer for by using
     201`grok.context()`.
     202
     203Instances of our importer will have an instance variable
     204`self.context` available, which will hold the specific object into
     205which data should be imported.
     206
     207The `datatype` string gives a description of the kind of importer we
     208define. It might be used by UI components to offer services of this
     209importer.
     210
     211The `column_terms` class variable holds the column headings we expect
     212in a received CSV file.
     213
     214Our importer should also comply with the IWAeUPCSVImporter interface,
     215therefore we defined a `doImport` method.
     216
     217To register our importer with the framework, we have to ``grok`` it
     218first. Note, that in normal use this is done automatically on startup
     219by the Grok framework:
     220
     221    >>> grok.testing.grok_component('MyImporter', MyImporter)
     222    True
     223
     224Now we can get an importer:
     225
     226    >>> importer = IWAeUPCSVImporter(mycontainer)
     227    >>> importer
     228    <MyImporter object at 0x...>
     229
     230This importer is rather useless as it does not do much:
     231
     232    >>> importer.doImport('nonsense')
     233    Data imported!
     234
     235This is a shameless lie. You might, however, get an idea of how to
     236build your own, real importer.
     237
     238From the baseclass, however, we get some functionality out-of-the-box:
     239We can validate files (without importing them):
     240
     241    >>> open('mydata.csv', 'wb').write(
     242    ... """col1,col2
     243    ... item1,item2
     244    ... """)
     245
     246    >>> importer.validate('mydata.csv')
     247    True
     248
     249If we deliver a misformatted file, the validator will notice that:
     250
     251    >>> open('crapdata.csv', 'wb').write("""
     252    ... col1,blah
     253    ... item1,item2
     254    ... """)
     255
     256    >>> importer.validate('crapdata.csv')
     257    Traceback (most recent call last):
     258    ...
     259    KeyError: 'Validation Error: ... could not be found: col2'
     260
     261Apparently our input file lacks a ``col2`` column.
     262
     263The WAeUP portal already comes with a set of better suited
     264importers. As they rely pretty much on the data structures they are
     265built for, they are normally defined in the approriate code pieces
     266where also the data structure itself is handled. So you can find an
     267importer for faculty CSV data in the `facultycontainer` module.
     268
     269Clean up:
     270
     271    >>> import os
     272    >>> os.unlink('crapdata.csv')
     273    >>> os.unlink('mydata.csv')
     274
Note: See TracChangeset for help on using the changeset viewer.