-
Notifications
You must be signed in to change notification settings - Fork 3
/
export_lookups
executable file
·50 lines (38 loc) · 1.25 KB
/
export_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
#!/usr/bin/env python
import csv
import sys
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
def main():
parse_config_file("sourcy.conf")
parser = OptionParser()
(opts, args) = parser.parse_args()
if len(args) != 1:
parser.error("missing arg: <kind>")
eng_url = "mysql+mysqldb://%(user)s:%(password)s@%(host)s/%(db)s?charset=utf8" % {
'user': options.mysql_user,
'password': options.mysql_password,
'host': options.mysql_host,
'db': options.mysql_database
}
engine = create_engine(eng_url, echo=False)
Session = sessionmaker(bind=engine)
session = Session()
(kind,) = args
if kind not in ('institution','journal'):
parser.error("unknown <kind>")
f = sys.stdout
out = csv.writer(f)
enc = 'utf-8'
q = session.query(Lookup).filter_by(kind=kind).order_by(Lookup.name)
for lookup in q:
row = [lookup.name.encode(enc),lookup.url.encode(enc)]
out.writerow(row)
if __name__ == "__main__":
main()