CSV import now respects NULLability of columns.
[zzz-pokedex.git] / pokedex / __init__.py
1 # encoding: utf8
2 import sys
3
4 from .db import connect, metadata
5
6 def main():
7 if len(sys.argv) <= 1:
8 help()
9
10 command = sys.argv[1]
11 args = sys.argv[2:]
12
13 # Find the command as a function in this file
14 func = globals().get(command, None)
15 if func and callable(func) and command != 'main':
16 func(*args)
17 else:
18 help()
19
20
21 def csvimport(engine_uri, dir='.'):
22 import csv
23
24 from sqlalchemy.orm.attributes import instrumentation_registry
25
26 session = connect(engine_uri)
27
28 metadata.create_all()
29
30 # Oh, mysql-chan.
31 # TODO try to insert data in preorder so we don't need this hack and won't
32 # break similarly on other engines
33 if 'mysql' in engine_uri:
34 session.execute('SET FOREIGN_KEY_CHECKS = 0')
35
36 # This is a secret attribute on a secret singleton of a secret class that
37 # appears to hopefully contain all registered classes as keys.
38 # There is no other way to accomplish this, as far as I can tell.
39 # Fuck.
40 for table in sorted(instrumentation_registry.manager_finders.keys(),
41 key=lambda self: self.__table__.name):
42 table_name = table.__table__.name
43 print table_name
44
45 reader = csv.reader(open("%s/%s.csv" % (dir, table_name), 'rb'), lineterminator='\n')
46 column_names = [unicode(column) for column in reader.next()]
47
48 for csvs in reader:
49 row = table()
50
51 for column_name, value in zip(column_names, csvs):
52 if table.__table__.c[column_name].nullable and value == '':
53 # Empty string in a nullable column really means NULL
54 value = None
55 else:
56 # Otherwise, unflatten from bytes
57 value = value.decode('utf-8')
58
59 setattr(row, column_name, value)
60
61 session.add(row)
62
63 session.commit()
64
65 # Shouldn't matter since this is usually the end of the program and thus
66 # the connection too, but let's change this back just in case
67 if 'mysql' in engine_uri:
68 session.execute('SET FOREIGN_KEY_CHECKS = 1')
69
70
71 def csvexport(engine_uri, dir='.'):
72 import csv
73 session = connect(engine_uri)
74
75 for table_name in sorted(metadata.tables.keys()):
76 print table_name
77 table = metadata.tables[table_name]
78
79 writer = csv.writer(open("%s/%s.csv" % (dir, table_name), 'wb'), lineterminator='\n')
80 columns = [col.name for col in table.columns]
81 writer.writerow(columns)
82
83 for row in session.query(table).all():
84 csvs = []
85 for col in columns:
86 # Convert Pythony values to something more universal
87 val = getattr(row, col)
88 if val == None:
89 val = ''
90 elif val == True:
91 val = '1'
92 elif val == False:
93 val = '0'
94 else:
95 val = unicode(val).encode('utf-8')
96
97 csvs.append(val)
98
99 writer.writerow(csvs)
100
101
102 def help():
103 print u"""pokedex -- a command-line Pokédex interface
104
105 help Displays this message.
106
107 These commands are only useful for developers:
108 csvimport {uri} [dir] Import data from a set of CSVs to the database
109 given by the URI.
110 csvexport {uri} [dir] Export data from the database given by the URI
111 to a set of CSVs.
112 Directory defaults to cwd.
113 """
114
115 sys.exit(0)