Changeset 4965 for main/waeup.sirp/trunk
- Timestamp:
- 1 Feb 2010, 16:29:02 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/utils/importexport.txt
r4920 r4965 141 141 >>> import os 142 142 >>> os.unlink('myexport.xml') 143 144 145 Importing CSV data146 ==================147 148 The WAeUP portal supports import of CSV data for several different149 data types like faculties, students, and the like.150 151 For our purposes we define CSV imports as tuples, containing a CSV152 source and a CSV receiver:153 154 ``CSV Import := <CSVSource, CSVReceiver>``155 156 CSV sources157 -----------158 159 CSV sources build a plugin framework inside the WAeUP portal (that160 might be factored out one day). See `waeup.sirp.csvfile` to learn more161 about that.162 163 A `waeup.sirp.csvfile.CSVFile` plugin cares for a special kind of CSV file.164 165 To make clear what that means, we start by creating a simple CSV166 file.167 168 CSVFile objects are abstractions of simple .csv-files. But as not169 every CSV file contains the same kind of data, we can create different170 flavours of it, for instance to import departments or faculties. As171 each kind of import needs different kind of data, we have different172 kinds of CSV sources.173 174 CSV sources can check their associated files for consistency,175 importability, etc.176 177 We create a simple CSV file with cave data:178 179 >>> open('mycavedata.csv', 'wb').write(180 ... """size,owner181 ... 42,Manfred182 ... """)183 184 We have a very simple CSVFile available, which is registered as an185 adapter for strings. To make this work, the string should be a path to186 a file:187 188 >>> from waeup.sirp.csvfile import getCSVFile189 >>> mysource = getCSVFile('mycavedata.csv')190 >>> mysource191 <waeup.sirp.csvfile.csvfile.CSVFile object at 0x...>192 193 We define caves like this:194 195 >>> class Cave(object):196 ... def __init__(self, size, owner):197 ... self.size = size198 ... self.owner = owner199 ... def __repr__(self):200 ... return '<Cave object [size: %s, owner: %s]>' % (201 ... self.size, self.owner)202 203 Let's assume, we have a container for caves:204 205 >>> from zope.interface import Interface206 >>> class ICaveContainer(Interface):207 ... """A container for caves."""208 209 >>> class CaveContainer(object):210 ... grok.implements(ICaveContainer)211 ... caves = []212 ... def addCave(self, cave):213 ... self.caves.append(cave)214 215 >>> mycaves = CaveContainer()216 217 Now, if we want to import caves from CSV files, we also need an218 importer, that imports data to the container:219 220 >>> from waeup.sirp.csvfile.interfaces import ICSVFile221 >>> from waeup.sirp.utils.importexport import CSVImporter222 >>> from waeup.sirp.interfaces import IWAeUPCSVImporter223 >>> class CaveImporter(CSVImporter):224 ... # Tell, what kinds of objects we connect...225 ... grok.adapts(ICSVFile, ICaveContainer)226 ... # Tell the world, that we are an importer...227 ... grok.provides(IWAeUPCSVImporter)228 ... # The datatype can be used by UI components and is required229 ... # by the interface...230 ... datatype = u'Cave Importer'231 ... def doImport(self):232 ... # CSVImporter instances have a `csvfile` and a `receiver`233 ... # object defined which refer to the CSV file and the container.234 ... for row in self.csvfile.getData():235 ... cave = Cave(size = row['size'],236 ... owner = row['owner'])237 ... self.receiver.addCave(cave)238 239 240 An CSV importer must be grokked before we can use it. In real life,241 this is normally done on startup automatically:242 243 >>> grok.testing.grok_component('CaveImporter', CaveImporter)244 True245 246 Importers are multi-adapters, so we need to ask for one:247 248 >>> from zope.component import getMultiAdapter249 >>> myimporter = getMultiAdapter((mysource, mycaves), IWAeUPCSVImporter)250 251 Now we can finally do the import:252 253 >>> myimporter.doImport()254 255 The import was really done:256 257 >>> mycaves.caves258 [<Cave object [size: 42, owner: Manfred]>]259 260 **Important note**:261 262 This importer adapts ICSVFile and ICaveContainer, which means that263 the type of data receiver is specified correctly. For the CSV264 source, however, the importer cannot be sure to find a column265 ``size`` or ``owner`` in the file.266 267 This can be prevented, by defining a more special CSVFile type and268 adapting this instead of ICSVData.269 270 In the `waeup.sirp.csvfile` subpackage's README it is shown, how this can271 be accomplished.272 273 Summing up we would define an ICaveCSVFile type, provide an274 appropriate CSV file wrapper and then let our importer adapt::275 276 (ICaveCSVFile, ICaveContainer)277 278 **Another note**:279 280 The importer we defined above does not support the ``clear_old_data``281 and ``overwrite`` keywords as would be required by the282 IWAeUPImporter interface.283 284 In real life this keywords should be supported.285 286 Clean up:287 288 >>> import os289 >>> os.unlink('mycavedata.csv')
Note: See TracChangeset for help on using the changeset viewer.