4 import sqlalchemy
.types
6 from .db
import connect
, metadata
15 # Find the command as a function in this file
16 func
= globals().get(command
, None)
17 if func
and callable(func
) and command
!= 'main':
23 def csvimport(engine_uri
, dir='.'):
26 from sqlalchemy
.orm
.attributes
import instrumentation_registry
28 session
= connect(engine_uri
)
33 # TODO try to insert data in preorder so we don't need this hack and won't
34 # break similarly on other engines
35 if 'mysql' in engine_uri
:
36 session
.execute('SET FOREIGN_KEY_CHECKS = 0')
38 # This is a secret attribute on a secret singleton of a secret class that
39 # appears to hopefully contain all registered classes as keys.
40 # There is no other way to accomplish this, as far as I can tell.
42 for table
in sorted(instrumentation_registry
.manager_finders
.keys(),
43 key
=lambda self
: self
.__table__
.name
):
44 table_name
= table
.__table__
.name
45 # Print the table name but leave the cursor in a fixed column
46 print table_name
+ '...', ' ' * (40 - len(table_name
)),
49 csvfile
= open("%s/%s.csv" %
(dir, table_name
), 'rb')
51 # File doesn't exist; don't load anything!
55 reader
= csv
.reader(csvfile
, lineterminator
='\n')
56 column_names
= [unicode(column
) for column
in reader
.next()]
61 for column_name
, value
in zip(column_names
, csvs
):
62 column
= table
.__table__
.c
[column_name
]
63 if column
.nullable
and value
== '':
64 # Empty string in a nullable column really means NULL
66 elif isinstance(column
.type, sqlalchemy
.types
.Boolean
):
67 # Boolean values are stored as string values 0/1, but both
68 # of those evaluate as true; SQLA wants True/False
74 # Otherwise, unflatten from bytes
75 value
= value
.decode('utf-8')
77 setattr(row
, column_name
, value
)
84 # Shouldn't matter since this is usually the end of the program and thus
85 # the connection too, but let's change this back just in case
86 if 'mysql' in engine_uri
:
87 session
.execute('SET FOREIGN_KEY_CHECKS = 1')
90 def csvexport(engine_uri
, dir='.'):
92 session
= connect(engine_uri
)
94 for table_name
in sorted(metadata
.tables
.keys()):
96 table
= metadata
.tables
[table_name
]
98 writer
= csv
.writer(open("%s/%s.csv" %
(dir, table_name
), 'wb'), lineterminator
='\n')
99 columns
= [col
.name
for col
in table
.columns
]
100 writer
.writerow(columns
)
102 for row
in session
.query(table
).all():
105 # Convert Pythony values to something more universal
106 val
= getattr(row
, col
)
114 val
= unicode(val
).encode('utf-8')
118 writer
.writerow(csvs
)
122 print u
"""pokedex -- a command-line Pokédex interface
124 help Displays this message.
126 These commands are only useful for developers:
127 csvimport {uri} [dir] Import data from a set of CSVs to the database
129 csvexport {uri} [dir] Export data from the database given by the URI
131 Directory defaults to cwd.