예제 #1
0
파일: cli.py 프로젝트: robdrimmie/centaur
def main():
    argparser = argparse.ArgumentParser(description='')
    argparser.add_argument('config', nargs='+',
        help='full path to config file')
    argparser.add_argument('-f', '--feed', action='append', 
            help='a feed to include')
    args = argparser.parse_args()
    cnf = {}
    try:
        for c in args.config:
            config_dict = config.load(c)
            for key in config_dict:
                if key not in cnf:
                    cnf[key] = config_dict[key]
                elif key !='feeds' and cnf[key] != config_dict[key]:
                    sys.exit('You specified multiple actions for {}! '
                        'Please specify only one.'.format(key))
                elif key == 'feeds':
                    cnf[key].extend(config_dict[key])
    except Exception as e:
        sys.exit('Something went wrong:\n {}'.format(e))
    if args.feed:
        feedlist = list(set(cnf['feeds'] + args.feed))
        cnf['feeds'] = feedlist
    if 'aggregators' not in cnf:
        sys.exit('You didn\'t specify any aggregators!')
    if 'filters' in cnf:
        try:
            filters = [
                util.inflate_filter(k, v) for k, v in cnf['filters'].items()
            ]
        except Exception as e:
            sys.exit('Could not inflate filters!\n{}'.format(e))
    try:
        aggregators = [
            util.inflate_aggregator(k, v) for k, v in cnf['aggregators'].items()
        ]
    except Exception as e:
        sys.exit('Could not inflate aggregators!\n{}'.format(e))
    try:
        for a in aggregators:
            # Pump to prime
            next(a) 
        for f in cnf['feeds']:
            parser.parse_feed(f, aggregators=aggregators, filters=filters)
        for a in aggregators:
            a.close()
    except Exception as e:
        sys.exit('Something went wrong!\n{}'.format(e))
예제 #2
0
def test_inflation():
    passable = {"title": "fooby"}
    failable = {"title": "baz zap"}
    # Inflate with just a function name from the centaur.filters module
    util._filter_cache = {}
    f1 = util.inflate_filter("title_matches", {"strings": ["foo", "bar"]})
    assert isinstance(f1, types.FunctionType), "function-name style import does not return a function"
    assert f1(passable) == passable, "fully-qualified import returns bad function"
    assert not f1(failable), "fully-qualified import returns bad function"
    # Inflate with a fully-qualified module.function-style string
    f2 = util.inflate_filter("centaur.filters.title_matches", {"strings": ["foo", "bar"]})
    assert isinstance(f2, types.FunctionType), "fully-qualified import does not return a function"
    assert f2(passable) == passable, "fully-qualified import returns bad function"
    assert not f2(failable), "fully-qualified import returns bad function"

    def dummy(entry):
        return "xyzzy"

    def dummy_factory(**kwargs):
        return dummy

    util._filter_cache["title_matches"] = dummy_factory
    f3 = util.inflate_filter("title_matches", {"strings": ["foo", "bar"]})
    assert f3(passable) == "xyzzy", "inflate_filter does not preferentially use cached values"