source: main/waeup.kofa/trunk/src/waeup/kofa/utils/batching.txt @ 9701

Last change on this file since 9701 was 8330, checked in by Henrik Bettermann, 13 years ago

When using catalogs existing objects must not necessarily be in the same container.

File size: 16.7 KB
Line 
1:mod:`waeup.kofa.utils.batching` -- Batch processing
2****************************************************
3
4Batch processing is much more than pure data import.
5
6Overview
7========
8
9Basically, it means processing CSV files in order to mass-create,
10mass-remove, or mass-update data.
11
12So you can feed CSV files to processors, that are part of
13the batch-processing mechanism.
14
15Processors
16----------
17
18Each CSV file processor
19
20* accepts a single data type identified by an interface.
21
22* knows about the places inside a site (University) where to store,
23  remove or update the data.
24
25* can check headers before processing data.
26
27* supports the mode 'create', 'update', 'remove'.
28
29* creates log entries (optional)
30
31* creates csv files containing successful and not-successful processed
32  data respectively.
33
34Output
35------
36
37The results of processing are written to loggers, if a logger was
38given. Beside this new CSV files are created during processing:
39
40* a pending CSV file, containing datasets that could not be processed
41
42* a finished CSV file, containing datasets successfully processed.
43
44The pending file is not created if everything works fine. The
45respective path returned in that case is ``None``.
46
47The pending file (if created) is a CSV file that contains the failed
48rows appended by a column ``--ERRROR--`` in which the reasons for
49processing failures are listed.
50
51The complete paths of these files are returned. They will be in a
52temporary directory created only for this purpose. It is the caller's
53responsibility to remove the temporay directories afterwards (the
54datacenters distProcessedFiles() method takes care for that).
55
56It looks like this::
57 
58     -----+      +---------+
59    /     |      |         |              +------+
60   | .csv +----->|Batch-   |              |      |
61   |      |      |processor+----changes-->| ZODB |
62   |  +------+   |         |              |      |
63   +--|      |   |         +              +------+
64      | Mode +-->|         |                 -------+
65      |      |   |         +----outputs-+-> /       |
66      |  +----+->+---------+            |  |.pending|
67      +--|Log |  ^                      |  |        |
68         +----+  |                      |  +--------+
69           +-----++                     v
70           |Inter-|                  ----------+
71           |face  |                 /          |
72           +------+                | .finished |
73                                   |           |
74                                   +-----------+
75
76
77Creating a batch processor
78==========================
79
80We create an own batch processor for an own datatype. This datatype
81must be based on an interface that the batcher can use for converting
82data.
83
84Founding Stoneville
85-------------------
86
87We start with the interface:
88
89    >>> from zope.interface import Interface
90    >>> from zope import schema
91    >>> class ICave(Interface):
92    ...   """A cave."""
93    ...   name = schema.TextLine(
94    ...     title = u'Cave name',
95    ...     default = u'Unnamed',
96    ...     required = True)
97    ...   dinoports = schema.Int(
98    ...     title = u'Number of DinoPorts (tm)',
99    ...     required = False,
100    ...     default = 1)
101    ...   owner = schema.TextLine(
102    ...     title = u'Owner name',
103    ...     required = True,
104    ...     missing_value = 'Fred Estates Inc.')
105    ...   taxpayer = schema.Bool(
106    ...     title = u'Payes taxes',
107    ...     required = True,
108    ...     default = False)
109
110Now a class that implements this interface:
111
112    >>> import grok
113    >>> class Cave(object):
114    ...   grok.implements(ICave)
115    ...   def __init__(self, name=u'Unnamed', dinoports=2,
116    ...                owner='Fred Estates Inc.', taxpayer=False):
117    ...     self.name = name
118    ...     self.dinoports = 2
119    ...     self.owner = owner
120    ...     self.taxpayer = taxpayer
121
122We also provide a factory for caves. Strictly speaking, this not
123necessary but makes the batch processor we create afterwards, better
124understandable.
125
126    >>> from zope.component import getGlobalSiteManager
127    >>> from zope.component.factory import Factory
128    >>> from zope.component.interfaces import IFactory
129    >>> gsm = getGlobalSiteManager()
130    >>> cave_maker = Factory(Cave, 'A cave', 'Buy caves here!')
131    >>> gsm.registerUtility(cave_maker, IFactory, 'Lovely Cave')
132
133Now we can create caves using a factory:
134
135    >>> from zope.component import createObject
136    >>> createObject('Lovely Cave')
137    <Cave object at 0x...>
138
139This is nice, but we still lack a place, where we can place all the
140lovely caves we want to sell.
141
142Furthermore, as a replacement for a real site, we define a place where
143all caves can be stored: Stoneville! This is a lovely place for
144upperclass cavemen (which are the only ones that can afford more than
145one dinoport).
146
147We found Stoneville:
148
149    >>> stoneville = dict()
150
151Everything in place.
152
153Now, to improve local health conditions, imagine we want to populate
154Stoneville with lots of new happy dino-hunting natives that slept on
155the bare ground in former times and had no idea of
156bathrooms. Disgusting, isn't it?
157
158Lots of cavemen need lots of caves.
159
160Of course we can do something like:
161
162    >>> cave1 = createObject('Lovely Cave')
163    >>> cave1.name = "Fred's home"
164    >>> cave1.owner = "Fred"
165    >>> stoneville[cave1.name] = cave1
166
167and Stoneville has exactly
168
169    >>> len(stoneville)
170    1
171
172inhabitant. But we don't want to do this for hundreds or thousands of
173citizens-to-be, do we?
174
175It is much easier to create a simple CSV list, where we put in all the
176data and let a batch processor do the job.
177
178The list is already here:
179
180    >>> open('newcomers.csv', 'wb').write(
181    ... """name,dinoports,owner,taxpayer
182    ... Barneys Home,2,Barney,1
183    ... Wilmas Asylum,1,Wilma,1
184    ... Freds Dinoburgers,10,Fred,0
185    ... Joeys Drive-in,110,Joey,0
186    ... """)
187
188All we need, is a batch processor now.
189
190    >>> from waeup.kofa.utils.batching import BatchProcessor
191    >>> from waeup.kofa.interfaces import IGNORE_MARKER
192    >>> class CaveProcessor(BatchProcessor):
193    ...   util_name = 'caveprocessor'
194    ...   grok.name(util_name)
195    ...   name = 'Cave Processor'
196    ...   iface = ICave
197    ...   location_fields = ['name']
198    ...   factory_name = 'Lovely Cave'
199    ...
200    ...   def parentsExist(self, row, site):
201    ...     return True
202    ...
203    ...   def getParent(self, row, site):
204    ...     return stoneville
205    ...
206    ...   def entryExists(self, row, site):
207    ...     return row['name'] in stoneville.keys()
208    ...
209    ...   def getEntry(self, row, site):
210    ...     if not self.entryExists(row, site):
211    ...       return None
212    ...     return stoneville[row['name']]
213    ...
214    ...   def delEntry(self, row, site):
215    ...     del stoneville[row['name']]
216    ...
217    ...   def addEntry(self, obj, row, site):
218    ...     stoneville[row['name']] = obj
219    ...
220    ...   def updateEntry(self, obj, row, site):
221    ...     # This is not strictly necessary, as the default
222    ...     # updateEntry method does exactly the same
223    ...     for key, value in row.items():
224    ...       if value != IGNORE_MARKER:
225    ...         setattr(obj, key, value)
226
227If we also want the results being logged, we must provide a logger
228(this is optional):
229
230    >>> import logging
231    >>> logger = logging.getLogger('stoneville')
232    >>> logger.setLevel(logging.DEBUG)
233    >>> logger.propagate = False
234    >>> handler = logging.FileHandler('stoneville.log', 'w')
235    >>> logger.addHandler(handler)
236
237Create the fellows:
238
239    >>> processor = CaveProcessor()
240    >>> result = processor.doImport('newcomers.csv',
241    ...                   ['name', 'dinoports', 'owner', 'taxpayer'],
242    ...                    mode='create', user='Bob', logger=logger)
243    >>> result
244    (4, 0, '/.../newcomers.finished.csv', None)
245
246The result means: four entries were processed and no warnings
247occured. Furthermore we get filepath to a CSV file with successfully
248processed entries and a filepath to a CSV file with erraneous entries.
249As everything went well, the latter is ``None``. Let's check:
250
251    >>> sorted(stoneville.keys())
252    [u'Barneys Home', ..., u'Wilmas Asylum']
253
254The values of the Cave instances have correct type:
255
256    >>> barney = stoneville['Barneys Home']
257    >>> barney.dinoports
258    2
259
260which is a number, not a string.
261
262Apparently, when calling the processor, we gave some more info than
263only the CSV filepath. What does it all mean?
264
265While the first argument is the path to the CSV file, we also have to
266give an ordered list of headernames. These replace the header field
267names that are actually in the file. This way we can override faulty
268headers.
269
270The ``mode`` paramter tells what kind of operation we want to perform:
271``create``, ``update``, or ``remove`` data.
272
273The ``user`` parameter finally is optional and only used for logging.
274
275We can, by the way, see the results of our run in a logfile if we
276provided a logger during the call:
277
278    >>> print open('stoneville.log').read()
279    --------------------
280    Bob: Batch processing finished: OK
281    Bob: Source: newcomers.csv
282    Bob: Mode: create
283    Bob: User: Bob
284    Bob: Processing time: ... s (... s/item)
285    Bob: Processed: 4 lines (4 successful/ 0 failed)
286    --------------------
287
288We cleanup the temporay dir created by doImport():
289
290    >>> import shutil
291    >>> import os
292    >>> shutil.rmtree(os.path.dirname(result[2]))
293
294As we can see, the processing was successful. Otherwise, all problems
295could be read here as we can see, if we do the same operation again:
296
297    >>> result = processor.doImport('newcomers.csv',
298    ...                   ['name', 'dinoports', 'owner', 'taxpayer'],
299    ...                    mode='create', user='Bob', logger=logger)
300    >>> result
301    (4, 4, '/.../newcomers.finished.csv', '/.../newcomers.pending.csv')
302
303This time we also get a path to a .pending file.
304
305The log file will tell us this in more detail:
306
307    >>> print open('stoneville.log').read()
308    --------------------
309    ...
310    --------------------
311    Bob: Batch processing finished: FAILED
312    Bob: Source: newcomers.csv
313    Bob: Mode: create
314    Bob: User: Bob
315    Bob: Failed datasets: newcomers.pending.csv
316    Bob: Processing time: ... s (... s/item)
317    Bob: Processed: 4 lines (0 successful/ 4 failed)
318    --------------------
319
320This time a new file was created, which keeps all the rows we could not
321process and an additional column with error messages:
322
323    >>> print open(result[3]).read()
324    owner,name,taxpayer,dinoports,--ERRORS--
325    Barney,Barneys Home,1,2,This object already exists. Skipping.
326    Wilma,Wilmas Asylum,1,1,This object already exists. Skipping.
327    Fred,Freds Dinoburgers,0,10,This object already exists. Skipping.
328    Joey,Joeys Drive-in,0,110,This object already exists. Skipping.
329
330This way we can correct the faulty entries and afterwards retry without
331having the already processed rows in the way.
332
333We also notice, that the values of the taxpayer column are returned as
334in the input file. There we wrote '1' for ``True`` and '0' for
335``False`` (which is accepted by the converters).
336
337Clean up:
338
339    >>> shutil.rmtree(os.path.dirname(result[2]))
340
341
342We can also tell to ignore some cols from input by passing
343``--IGNORE--`` as col name:
344
345    >>> result = processor.doImport('newcomers.csv', ['name',
346    ...                             '--IGNORE--', '--IGNORE--'],
347    ...                    mode='update', user='Bob')
348    >>> result
349    (4, 0, '...', None)
350
351Clean up:
352
353    >>> shutil.rmtree(os.path.dirname(result[2]))
354
355If something goes wrong during processing, the respective --IGNORE--
356cols won't be populated  in the resulting pending file:
357
358    >>> result = processor.doImport('newcomers.csv', ['name', 'dinoports',
359    ...                             '--IGNORE--', '--IGNORE--'],
360    ...                    mode='create', user='Bob')
361    >>> result
362    (4, 4, '...', '...')
363
364    >>> print open(result[3], 'rb').read()
365    name,dinoports,--ERRORS--
366    Barneys Home,2,This object already exists. Skipping.
367    Wilmas Asylum,1,This object already exists. Skipping.
368    Freds Dinoburgers,10,This object already exists. Skipping.
369    Joeys Drive-in,110,This object already exists. Skipping.
370
371
372Clean up:
373
374    >>> shutil.rmtree(os.path.dirname(result[2]))
375
376
377
378
379Updating entries
380----------------
381
382To update entries, we just call the batchprocessor in a different
383mode:
384
385    >>> result = processor.doImport('newcomers.csv', ['name',
386    ...                             'dinoports', 'owner'],
387    ...                    mode='update', user='Bob')
388    >>> result
389    (4, 0, '...', None)
390
391Now we want to tell, that Wilma got an extra port for her second dino:
392
393    >>> open('newcomers.csv', 'wb').write(
394    ... """name,dinoports,owner
395    ... Wilmas Asylum,2,Wilma
396    ... """)
397
398    >>> wilma = stoneville['Wilmas Asylum']
399    >>> wilma.dinoports
400    1
401
402Clean up:
403
404    >>> shutil.rmtree(os.path.dirname(result[2]))
405
406
407We start the processor:
408
409    >>> result = processor.doImport('newcomers.csv', ['name',
410    ...                    'dinoports', 'owner'], mode='update', user='Bob')
411    >>> result
412    (1, 0, '...', None)
413
414    >>> wilma = stoneville['Wilmas Asylum']
415    >>> wilma.dinoports
416    2
417
418Wilma's number of dinoports raised.
419
420Clean up:
421
422    >>> shutil.rmtree(os.path.dirname(result[2]))
423
424
425If we try to update an unexisting entry, an error occurs:
426
427    >>> open('newcomers.csv', 'wb').write(
428    ... """name,dinoports,owner
429    ... NOT-WILMAS-ASYLUM,2,Wilma
430    ... """)
431
432    >>> result = processor.doImport('newcomers.csv', ['name',
433    ...                             'dinoports', 'owner'],
434    ...                    mode='update', user='Bob')
435    >>> result
436    (1, 1, '/.../newcomers.finished.csv', '/.../newcomers.pending.csv')
437
438Clean up:
439
440    >>> shutil.rmtree(os.path.dirname(result[2]))
441
442   
443Also invalid values will be spotted:
444
445    >>> open('newcomers.csv', 'wb').write(
446    ... """name,dinoports,owner
447    ... Wilmas Asylum,NOT-A-NUMBER,Wilma
448    ... """)
449
450    >>> result = processor.doImport('newcomers.csv', ['name',
451    ...                             'dinoports', 'owner'],
452    ...                    mode='update', user='Bob')
453    >>> result
454    (1, 1, '...', '...')
455
456Clean up:
457
458    >>> shutil.rmtree(os.path.dirname(result[2]))
459
460
461We can also update only some cols, leaving some out. We skip the
462'dinoports' column in the next run:
463
464    >>> open('newcomers.csv', 'wb').write(
465    ... """name,owner
466    ... Wilmas Asylum,Barney
467    ... """)
468
469    >>> result = processor.doImport('newcomers.csv', ['name', 'owner'],
470    ...                             mode='update', user='Bob')
471    >>> result
472    (1, 0, '...', None)
473
474    >>> wilma.owner
475    u'Barney'
476
477Clean up:
478
479    >>> shutil.rmtree(os.path.dirname(result[2]))
480
481
482We can however, not leave out the 'location field' ('name' in our
483case), as this one tells us which entry to update:
484
485    >>> open('newcomers.csv', 'wb').write(
486    ... """name,dinoports,owner
487    ... 2,Wilma
488    ... """)
489
490    >>> processor.doImport('newcomers.csv', ['dinoports', 'owner'],
491    ...                    mode='update', user='Bob')
492    Traceback (most recent call last):
493    ...
494    FatalCSVError: Need at least columns 'name' for import!
495
496This time we get even an exception!
497
498Generally, empty strings are considered as ``None``:
499
500    >>> open('newcomers.csv', 'wb').write(
501    ... """name,dinoports,owner
502    ... "Wilmas Asylum","","Wilma"
503    ... """)
504
505    >>> result = processor.doImport('newcomers.csv', ['name',
506    ...                             'dinoports', 'owner'],
507    ...                    mode='update', user='Bob')
508    >>> result
509    (1, 0, '...', None)
510
511    >>> wilma.dinoports
512    2
513
514Clean up:
515
516    >>> shutil.rmtree(os.path.dirname(result[2]))
517
518We can tell to set dinoports to ``None`` although this is not a
519number, as we declared the field not required in the interface:
520
521    >>> open('newcomers.csv', 'wb').write(
522    ... """name,dinoports,owner
523    ... "Wilmas Asylum","XXX","Wilma"
524    ... """)
525
526    >>> result = processor.doImport('newcomers.csv', ['name',
527    ...                             'dinoports', 'owner'],
528    ...                    mode='update', user='Bob', ignore_empty=False)
529    >>> result
530    (1, 0, '...', None)
531
532    >>> wilma.dinoports is None
533    True
534
535Clean up:
536
537    >>> shutil.rmtree(os.path.dirname(result[2]))
538
539Removing entries
540----------------
541
542In 'remove' mode we can delete entries. Here validity of values in
543non-location fields doesn't matter because those fields are ignored.
544
545    >>> open('newcomers.csv', 'wb').write(
546    ... """name,dinoports,owner
547    ... "Wilmas Asylum","ILLEGAL-NUMBER",""
548    ... """)
549
550    >>> result = processor.doImport('newcomers.csv', ['name',
551    ...                             'dinoports', 'owner'],
552    ...                    mode='remove', user='Bob')
553    >>> result
554    (1, 0, '...', None)
555
556    >>> sorted(stoneville.keys())
557    [u'Barneys Home', "Fred's home", u'Freds Dinoburgers', u'Joeys Drive-in']
558
559Oops! Wilma is gone.
560
561Clean up:
562
563    >>> shutil.rmtree(os.path.dirname(result[2]))
564
565
566Clean up:
567
568    >>> import os
569    >>> os.unlink('newcomers.csv')
570    >>> os.unlink('stoneville.log')
Note: See TracBrowser for help on using the repository browser.