Example #1
0
    def __iter__(self):
        import_modules(get_words(self.options.get('modules')))
        expressions = get_expressions(
            self, get_words(self.options.get('expressions')),
            ['blueprint', 'modules', 'condition', 'expressions'])
        assert expressions, 'No expressions defined'

        for item in self.previous:
            extras = is_mapping(item) and item or {}
            if self.condition(item, **extras) and is_mapping(item):
                for name, expression in expressions:
                    item[name] = expression(item, **extras)
            yield item
Example #2
0
    def __iter__(self):
        import_modules(get_words(self.options.get('modules')))
        expressions = get_expressions(
            self, get_words(self.options.get('expressions')),
            ['blueprint', 'modules', 'condition', 'expressions']
        )
        assert expressions, 'No expressions defined'

        for item in self.previous:
            extras = is_mapping(item) and item or {}
            if self.condition(item, **extras) and is_mapping(item):
                for name, expression in expressions:
                    item[name] = expression(item, **extras)
            yield item
Example #3
0
 def __iter__(self):
     for item in self.previous:
         extras = is_mapping(item) and item or {}
         if self.condition(item, **extras):
             # noinspection PyProtectedMember
             self.pdb.set_trace(sys._getframe())  # Break!
         yield item
Example #4
0
 def __iter__(self):
     key = self.options.get('key')
     for item in self.previous:
         if self.condition(item) and is_mapping(item.get(key)):
             inverted = item.pop(key)
             for key_, value in item.items():
                 inverted[key_] = value
             yield inverted
         else:
             yield item
Example #5
0
 def __iter__(self):
     key = self.options.get('key')
     for item in self.previous:
         if self.condition(item) and is_mapping(item.get(key)):
             inverted = item.pop(key)
             for key_, value in item.items():
                 inverted[key_] = value
             yield inverted
         else:
             yield item
Example #6
0
    def __iter__(self):
        import_modules(get_words(self.options.get('modules')))
        expressions = get_expressions(
            self, get_words(self.options.get('expressions')),
            ['blueprint', 'modules', 'expressions'])

        for item in self.previous:
            extras = is_mapping(item) and item or {}
            for name, expression in expressions:
                if bool(expression(item, **extras)):
                    yield item
                    break
Example #7
0
    def __iter__(self):
        import_modules(get_words(self.options.get('modules')))
        expressions = get_expressions(
            self, get_words(self.options.get('expressions')),
            ['blueprint', 'modules', 'expressions']
        )

        for item in self.previous:
            extras = is_mapping(item) and item or {}
            for name, expression in expressions:
                if bool(expression(item, **extras)):
                    yield item
                    break
Example #8
0
    def __iter__(self):
        import_modules(get_words(self.options.get('modules')))
        expressions = get_expressions(
            self, get_words(self.options.get('expressions')),
            ['blueprint', 'modules', 'expressions'])

        for item in self.previous:
            extras = is_mapping(item) and item or {}
            try:
                for name, expression in expressions:
                    assert bool(expression(item, **extras)), 'Condition failed'
                yield item
            except AssertionError:
                pass
Example #9
0
    def __iter__(self):
        import_modules(get_words(self.options.get('modules')))
        expressions = get_expressions(
            self, get_words(self.options.get('expressions')),
            ['blueprint', 'modules', 'expressions']
        )

        for item in self.previous:
            extras = is_mapping(item) and item or {}
            try:
                for name, expression in expressions:
                    assert bool(expression(item, **extras)), 'Condition failed'
                yield item
            except AssertionError:
                pass
Example #10
0
    def __iter__(self):  # flake8: noqa
        path = self.options.get('filename', 'output.csv').strip()
        fieldnames = get_words(self.options.get('fieldnames'))

        if path != '-' and not os.path.isabs(path):
            path = os.path.join(os.getcwd(), path)

        if sys.version_info[0] < 3:
            fp = BytesIO()
        else:
            fp = StringIO()
        writer = DictWriter(fp, list(fieldnames))

        counter = 0
        for item in self.previous:
            if self.condition(item) and is_mapping(item):
                if not writer.fieldnames:
                    writer.fieldnames = [key for key in item.keys()
                                         if not key.startswith('_')]
                if counter == 0:
                    header = dict(zip(writer.fieldnames, writer.fieldnames))
                    writer.writerow(header)

                clone = dict(item)
                for fieldname in writer.fieldnames:
                    clone.setdefault(fieldname, None)
                writer.writerow(dict([
                    (key, value) for key, value in clone.items()
                    if key in writer.fieldnames
                ]))
                counter += 1

            yield item
        fp.seek(0)

        if path == '-':
            sys.stdout.write(fp.read())
        else:
            with open(path, 'w') as fp2:
                fp2.write(fp.read())

        logger.info('{0:s}:{1:s} wrote {2:d} items to {3:s}'.format(
            self.__class__.__name__, self.name, counter, path,
            self.options.get('filename', 'output.csv')
        ))
Example #11
0
    def __iter__(self):  # flake8: noqa
        path = self.options.get('filename', 'output.csv').strip()
        fieldnames = get_words(self.options.get('fieldnames'))

        if path != '-' and not os.path.isabs(path):
            path = os.path.join(os.getcwd(), path)

        if sys.version_info[0] < 3:
            fp = BytesIO()
        else:
            fp = StringIO()
        writer = DictWriter(fp, list(fieldnames))

        counter = 0
        for item in self.previous:
            if self.condition(item) and is_mapping(item):
                if not writer.fieldnames:
                    writer.fieldnames = [
                        key for key in item.keys() if not key.startswith('_')
                    ]
                if counter == 0:
                    header = dict(zip(writer.fieldnames, writer.fieldnames))
                    writer.writerow(header)

                clone = dict(item)
                for fieldname in writer.fieldnames:
                    clone.setdefault(fieldname, None)
                writer.writerow(
                    dict([(key, value) for key, value in clone.items()
                          if key in writer.fieldnames]))
                counter += 1

            yield item
        fp.seek(0)

        if path == '-':
            sys.stdout.write(fp.read())
        else:
            with open(path, 'w') as fp2:
                fp2.write(fp.read())

        logger.info('{0:s}:{1:s} wrote {2:d} items to {3:s}'.format(
            self.__class__.__name__, self.name, counter, path,
            self.options.get('filename', 'output.csv')))
Example #12
0
    def __iter__(self):
        for item in self.previous:
            yield item

        import_modules(get_words(self.options.get('modules')))
        expressions = get_expressions(
            self, get_words(self.options.get('expressions')),
            ['blueprint', 'modules', 'condition', 'expressions'])
        assert expressions, 'No expressions defined'

        for name, expression in expressions:
            for item in (expression(None) or []):
                try:
                    if is_mapping(unwrap(item)):
                        if self.condition(item):
                            yield item
                except BrokenImplementation:
                    if self.condition({name: item}):
                        yield {name: item}
            break
Example #13
0
    def __iter__(self):
        for item in self.previous:
            yield item

        import_modules(get_words(self.options.get('modules')))
        expressions = get_expressions(
            self, get_words(self.options.get('expressions')),
            ['blueprint', 'modules', 'condition', 'expressions']
        )
        assert expressions, 'No expressions defined'

        for name, expression in expressions:
            for item in (expression(None) or []):
                try:
                    if is_mapping(unwrap(item)):
                        if self.condition(item):
                            yield item
                except BrokenImplementation:
                    if self.condition({name: item}):
                        yield {name: item}
            break