Changeset 4159
- Timestamp:
- 24 May 2009, 10:10:40 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
waeup/branches/ulif-rewrite/src/waeup/utils/importexport.txt
r4059 r4159 141 141 >>> import os 142 142 >>> os.unlink('myexport.xml') 143 144 145 Importing CSV data 146 ================== 147 148 The WAeUP portal supports import of CSV data for several different 149 data types like faculties, students, and the like. 150 151 Obtaining importers 152 ------------------- 153 154 Before we actually do real imports, we have to provide so-called 155 importers, that will do the job for us. Let's see how that works. 156 157 We 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 166 We get a suitable importer for certain objects by asking for an 167 adapter to `IWAeUPCSVImporter`: 168 169 >>> from waeup.interfaces import IWAeUPCSVImporter 170 171 If, however, we try to create an importer for our container, we won't 172 find 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 181 The reason is, that there is simply no importer defined for 182 `MyContainer` objects. 183 184 As an importer must take care for the specific structure of objects 185 that receive the data, we must first define an importer for our 186 special 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 198 This importer derives from `CSVImporter`. The `CSVImporter` base class 199 makes our importer automatically an adapter. Therefore we have to give 200 the type of object we provide this importer for by using 201 `grok.context()`. 202 203 Instances of our importer will have an instance variable 204 `self.context` available, which will hold the specific object into 205 which data should be imported. 206 207 The `datatype` string gives a description of the kind of importer we 208 define. It might be used by UI components to offer services of this 209 importer. 210 211 The `column_terms` class variable holds the column headings we expect 212 in a received CSV file. 213 214 Our importer should also comply with the IWAeUPCSVImporter interface, 215 therefore we defined a `doImport` method. 216 217 To register our importer with the framework, we have to ``grok`` it 218 first. Note, that in normal use this is done automatically on startup 219 by the Grok framework: 220 221 >>> grok.testing.grok_component('MyImporter', MyImporter) 222 True 223 224 Now we can get an importer: 225 226 >>> importer = IWAeUPCSVImporter(mycontainer) 227 >>> importer 228 <MyImporter object at 0x...> 229 230 This importer is rather useless as it does not do much: 231 232 >>> importer.doImport('nonsense') 233 Data imported! 234 235 This is a shameless lie. You might, however, get an idea of how to 236 build your own, real importer. 237 238 From the baseclass, however, we get some functionality out-of-the-box: 239 We 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 249 If 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 261 Apparently our input file lacks a ``col2`` column. 262 263 The WAeUP portal already comes with a set of better suited 264 importers. As they rely pretty much on the data structures they are 265 built for, they are normally defined in the approriate code pieces 266 where also the data structure itself is handled. So you can find an 267 importer for faculty CSV data in the `facultycontainer` module. 268 269 Clean 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.