@@ -0,0 +1,2001 @@
+#!/usr/bin/env python%0A'''%0AA CLI tool for formulating an Abba url using data from PostgreSQL%0A'''%0A%0Afrom __future__ import print_function%0A%0Aimport argparse%0Aimport psycopg2%0Aimport sys%0A%0A%0ATOOL_DESCRIPTION = '''%0AFormulates an Abba url using data from PostgreSQL%0A%0AThe query passed to this tool should return three columns, which will%0Abecome the label, success count, and trial count in Abba.  If the query%0Aflag is not specified, the query will be taken from standard input.%0A%0ANote that the db parameters are optional, and if not provided psycopg2%0Awill attempt to connect to the default locally-hosted database.%0A'''%0A%0A%0Adef parse_arguments():%0A    '''%0A    Parse the arguments from the command line for this program%0A    '''%0A    parser = argparse.ArgumentParser(%0A        description=TOOL_DESCRIPTION,%0A        formatter_class=argparse.RawDescriptionHelpFormatter%0A    )%0A    parser.add_argument(%0A        '-d', '--db_params', metavar='PARAMS',%0A        help='A libpq connection string with params for the target database'%0A    )%0A    parser.add_argument(%0A        '-q', '--query',%0A        help='The query which will provide the data for Abba',%0A    )%0A    return parser.parse_args()%0A%0A%0Adef build_url_from_database_query(dsn, query):%0A    '''%0A    Build an Abba URL using data from a PostgreSQL connection and query%0A    '''%0A    url_template = 'http://thumbtack.com/labs/abba#%7B%7D'%0A%0A    cursor = psycopg2.connect(dsn).cursor()%0A    cursor.execute(query)%0A%0A    if not cursor.rowcount:%0A        return url_template.format('')%0A%0A    rows = cursor.fetchall()%0A    if len(rows%5B0%5D) != 3:%0A        raise ValueError('Query does not return 3 columns of data')%0A%0A    groups_querystr = '&'.join('%7B%7D=%7B%7D%252C%7B%7D'.format(*row) for row in rows)%0A%0A    return url_template.format(groups_querystr)%0A%0A%0Adef main():%0A    args = parse_arguments()%0A    query = args.query if args.query is not None else sys.stdin.read()%0A    params = args.db_params if args.db_params else ''%0A%0A    print(build_url_from_database_query(params, query))%0A%0A%0Aif __name__ == '__main__':%0A    main()%0A

Add tool for pulling data from PostgreSQL to Abba