4 from .db
import connect
, metadata
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':
21 def csvimport(engine_uri
, dir='.'):
24 from sqlalchemy
.orm
.attributes
import instrumentation_registry
26 session
= connect(engine_uri
)
30 # This is a secret attribute on a secret singleton of a secret class that
31 # appears to hopefully contain all registered classes as keys.
32 # There is no other way to accomplish this, as far as I can tell.
34 for table
in sorted(instrumentation_registry
.manager_finders
.keys(),
35 key
=lambda self
: self
.__table__
.name
):
36 table_name
= table
.__table__
.name
39 reader
= csv
.reader(open("%s/%s.csv" %
(dir, table_name
), 'rb'), lineterminator
='\n')
40 columns
= [unicode(column
) for column
in reader
.next()]
45 for column
, value
in zip(columns
, csvs
):
46 value
= value
.decode('utf-8')
47 setattr(row
, column
, value
)
54 def csvexport(engine_uri
, dir='.'):
56 session
= connect(engine_uri
)
58 for table_name
in sorted(metadata
.tables
.keys()):
60 table
= metadata
.tables
[table_name
]
62 writer
= csv
.writer(open("%s/%s.csv" %
(dir, table_name
), 'wb'), lineterminator
='\n')
63 columns
= [col
.name
for col
in table
.columns
]
64 writer
.writerow(columns
)
66 for row
in session
.query(table
).all():
69 # Convert Pythony values to something more universal
70 val
= getattr(row
, col
)
78 val
= unicode(val
).encode('utf-8')
86 print u
"""pokedex -- a command-line Pokédex interface
88 help Displays this message.
90 These commands are only useful for developers:
91 csvimport {uri} [dir] Import data from a set of CSVs to the database
93 csvexport {uri} [dir] Export data from the database given by the URI
95 Directory defaults to cwd.