1 | import logging |
---|
2 | from time import time |
---|
3 | |
---|
4 | from zope.interface import classProvides, implements |
---|
5 | from zope.annotation.interfaces import IAnnotations |
---|
6 | |
---|
7 | from collective.transmogrifier.interfaces import ISectionBlueprint |
---|
8 | from collective.transmogrifier.interfaces import ISection |
---|
9 | from collective.transmogrifier.utils import Matcher |
---|
10 | |
---|
11 | VALIDATIONKEY = 'quintagroup.transmogrifier.logger' |
---|
12 | |
---|
13 | class LoggerSection(object): |
---|
14 | classProvides(ISectionBlueprint) |
---|
15 | implements(ISection) |
---|
16 | |
---|
17 | def __init__(self, transmogrifier, name, options, previous): |
---|
18 | self.transmogrifier = transmogrifier |
---|
19 | keys = options.get('keys') or '' |
---|
20 | self.pathkey = options.get('path-key', '_path').strip() |
---|
21 | self.keys = Matcher(*keys.splitlines()) |
---|
22 | self.previous = previous |
---|
23 | self.logger = name |
---|
24 | self.storage = IAnnotations(transmogrifier).setdefault(VALIDATIONKEY, []) |
---|
25 | |
---|
26 | def __iter__(self): |
---|
27 | start_time = time() |
---|
28 | count = 0 |
---|
29 | problematic = 0 |
---|
30 | for item in self.previous: |
---|
31 | # source sections add store path of current generated item in annotation |
---|
32 | # it gives posibility to monitor what items go through all pipeline |
---|
33 | # sections between source section and this section and what don't |
---|
34 | if self.pathkey in item and item[self.pathkey] in self.storage: |
---|
35 | self.storage.remove(item[self.pathkey]) |
---|
36 | count += 1 |
---|
37 | # print item data stored on keys given as option |
---|
38 | items = [] |
---|
39 | for key in item.keys(): |
---|
40 | if self.keys(key)[0] is not None: |
---|
41 | items.append("%s=%s" % (key, item[key])) |
---|
42 | if items: |
---|
43 | msg = ", ".join(items) |
---|
44 | logging.getLogger(self.logger).info(msg) |
---|
45 | yield item |
---|
46 | |
---|
47 | working_time = int(round(time() - start_time)) |
---|
48 | |
---|
49 | # log items that maybe have some problems |
---|
50 | if self.storage: |
---|
51 | problematic = len(self.storage) |
---|
52 | logging.getLogger(self.logger).warning('\nNext objects didn\'t go through full pipeline:\n%s' % \ |
---|
53 | '\n'.join(['\t'+i for i in self.storage])) |
---|
54 | # delete validation data from annotations |
---|
55 | anno = IAnnotations(self.transmogrifier) |
---|
56 | if VALIDATIONKEY in anno: |
---|
57 | del anno[VALIDATIONKEY] |
---|
58 | |
---|
59 | seconds = working_time % 60 |
---|
60 | minutes = working_time / 60 % 60 |
---|
61 | hours = working_time / 3600 |
---|
62 | stats = "\nPipeline processing time: %02d:%02d:%02d\n" % (hours, minutes, seconds) |
---|
63 | stats += "\t%4d items were generated in source sections\n" % (count + problematic) |
---|
64 | stats += "\t%4d went through full pipeline\n" % count |
---|
65 | stats += "\t%4d were discarded in some section" % problematic |
---|
66 | logging.getLogger(self.logger).info(stats) |
---|