import cmq

'''
webhook, they work out the box on couchdb
so just read the db and if document dont have status done, emit it in a predefined function.

'''

_agentProcDir = {
    
}

if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(description='Couch Message Queue')
    parser.add_argument('--name',action='store',help='agent name')
    parser.add_argument('--db',action='store',help='database name')
    parser.add_argument('--url',action='store',default='http://localhost:5984/',help='database url')
    parser.add_argument('--process',action='store_true',help='query database and execute instruction')
    parser.add_argument('--view',action='store',default='_all_docs',help='couchdb view to use')
    parser.add_argument('--limit',action='store',default='0',help='limit quantity of instruction to run')
    parser.add_argument('--test', dest='test', action='store_true',default=False,help='execute test suite')
    args = parser.parse_args()

    if args.test:
        import doctest
        doctest.testmod(optionflags=doctest.ELLIPSIS)#,verbose=True)   #275 tests!!!
    if args.process:
        assert args.name
        assert args.db
        assert args.url
        assert args.view
        assert args.limit
        print('processing %s%s' % (args.url, args.db))
	import cmq
        c = cmq.connection(args.url,args.db,args.name)
        c.process(_agentProcDir,view=args.view, qty=args.limit)

    print('done')
