-
Notifications
You must be signed in to change notification settings - Fork 3
/
import_lookups
executable file
·55 lines (39 loc) · 1.25 KB
/
import_lookups
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/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()