#!/usr/bin/env python

from optparse import OptionParser
#from tornado.options import define, options
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from tornado.options import define, options

from pprint import pprint
from sourcy.models import Lookup
from sourcy.util import parse_config_file
from sourcy import db

import csv


def main():
    parse_config_file("sourcy.conf")

    parser = OptionParser()
    (opts, args) = parser.parse_args()
    if len(args) != 2:
        parser.error("requires 2 args: <kind> <infile>")

    eng_url = db.engine_url()
    engine = create_engine(eng_url, echo=False)
    Session = sessionmaker(bind=engine)
    session = Session()

    (kind,infilename) = args
    if kind not in ('institution','journal'):
        parser.error("unknown <kind>")

    f = open(infilename)
    rdr = csv.reader(f)

    MAX_URL_LEN=200
    MAX_NAME_LEN=200
    enc = 'utf-8'

    data = ((unicode(row[0],enc),unicode(row[1],enc)) for row in rdr)
    data = (row for row in data if len(row[0])<MAX_NAME_LEN and len(row[1])<MAX_URL_LEN)

    for row in data:
        name = row[0]
        url = row[1]
        lookup = Lookup(kind,name,url)
        session.add(lookup)
    session.commit()


if __name__ == "__main__":
    main()

