def async_reducer(item, rules): field = rules[0]['field'] word = item.get(field, **kwargs) grouped = group_by(rules, 'flags') group_rules = [g[1] for g in grouped] if multi else rules reducer = multi_substitute if multi else substitute replacement = yield ait.coop_reduce(reducer, group_rules, word) combined = merge([item, {field: replacement}]) return_value(DotDict(combined))
def async_parser(word, rules, skip, **kwargs): """ Asynchronously parses the pipe content Args: word (str): The string to transform rules (List[obj]): the parsed rules (Objectify instances). skip (bool): Don't parse the content kwargs (dict): Keyword arguments Kwargs: assign (str): Attribute to assign parsed content (default: exchangerate) stream (dict): The original item Returns: Deferred: twisted.internet.defer.Deferred Tuple of (item, skip) Examples: >>> from riko.bado import react >>> from riko.bado.mock import FakeReactor >>> from riko.lib.utils import Objectify >>> >>> def run(reactor): ... callback = lambda x: print(x[0]) ... item = {'content': 'hello world'} ... conf = {'rule': {'find': 'hello', 'replace': 'bye'}} ... rule = Objectify(conf['rule']) ... kwargs = {'stream': item, 'conf': conf} ... d = async_parser(item['content'], [rule], False, **kwargs) ... return d.addCallbacks(callback, logger.error) >>> >>> try: ... react(run, _reactor=FakeReactor()) ... except SystemExit: ... pass ... bye world """ if skip: value = kwargs['stream'] else: value = yield ait.coop_reduce(reducer, rules, word) result = (value, skip) return_value(result)
def async_parser(word, rules, skip, **kwargs): """ Asynchronously parses the pipe content Args: word (str): The string to transform rules (List[obj]): the parsed rules (Objectify instances). skip (bool): Don't parse the content kwargs (dict): Keyword arguments Kwargs: assign (str): Attribute to assign parsed content (default: strtransform) stream (dict): The original item Returns: Deferred: twisted.internet.defer.Deferred Tuple of (item, skip) Examples: >>> from riko.bado import react >>> from riko.bado.mock import FakeReactor >>> from riko.lib.utils import Objectify >>> >>> def run(reactor): ... callback = lambda x: print(x[0]) ... item = {'content': 'hello world'} ... conf = {'rule': {'transform': 'title'}} ... rule = Objectify(conf['rule']) ... kwargs = {'stream': item, 'conf': conf} ... d = async_parser(item['content'], [rule], False, **kwargs) ... return d.addCallbacks(callback, logger.error) >>> >>> try: ... react(run, _reactor=FakeReactor()) ... except SystemExit: ... pass ... Hello World """ if skip: value = kwargs['stream'] else: value = yield ait.coop_reduce(reducer, rules, word) result = (value, skip) return_value(result)
def async_parser(item, rules, skip, **kwargs): """ Asynchronously parses the pipe content Args: item (obj): The entry to process (a DotDict instance) rules (List[obj]): the parsed rules (Objectify instances). skip (bool): Don't parse the content kwargs (dict): Keyword arguments Kwargs: stream (dict): The original item Returns: Deferred: twisted.internet.defer.Deferred Tuple of (item, skip) Examples: >>> from riko.bado import react >>> from riko.lib.dotdict import DotDict >>> from riko.bado.mock import FakeReactor >>> from riko.lib.utils import Objectify >>> >>> def run(reactor): ... callback = lambda x: print(x[0] == {'greeting': 'hello world'}) ... item = DotDict({'content': 'hello world'}) ... rule = {'field': 'content', 'newval': 'greeting'} ... kwargs = {'stream': item} ... d = async_parser(item, [Objectify(rule)], False, **kwargs) ... return d.addCallbacks(callback, logger.error) >>> >>> try: ... react(run, _reactor=FakeReactor()) ... except SystemExit: ... pass ... True """ if skip: item = kwargs['stream'] else: item = yield ait.coop_reduce(reducer, rules, item) result = (item, skip) return_value(result)
def async_parser(item, rules, skip=False, **kwargs): """ Asynchronously parses the pipe content Args: item (obj): The entry to process (a DotDict instance) rules (List[obj]): the parsed rules (Objectify instances). skip (bool): Don't parse the content kwargs (dict): Keyword arguments Kwargs: stream (dict): The original item Returns: Deferred: twisted.internet.defer.Deferred item Examples: >>> from riko.bado import react >>> from riko.dotdict import DotDict >>> from riko.bado.mock import FakeReactor >>> from meza.fntools import Objectify >>> >>> def run(reactor): ... callback = lambda x: print(x == {'greeting': 'hello world'}) ... item = DotDict({'content': 'hello world'}) ... rule = {'field': 'content', 'newval': 'greeting'} ... kwargs = {'stream': item} ... d = async_parser(item, [Objectify(rule)], **kwargs) ... return d.addCallbacks(callback, logger.error) >>> >>> try: ... react(run, _reactor=FakeReactor()) ... except SystemExit: ... pass ... True """ if skip: item = kwargs['stream'] else: item = yield ait.coop_reduce(reducer, rules, item) return_value(item)