X-Git-Url: http://git.veekun.com/zzz-pokedex.git/blobdiff_plain/2c0980e4dcadaa31d7ea7ebab174a6fd81723090..06be8bc5add31a5937b252c678d60037cb103b5a:/pokedex/__init__.py diff --git a/pokedex/__init__.py b/pokedex/__init__.py index 02b8ce5..4a731d1 100644 --- a/pokedex/__init__.py +++ b/pokedex/__init__.py @@ -5,6 +5,7 @@ from sqlalchemy.exc import IntegrityError import sqlalchemy.types from .db import connect, metadata, tables as tables_module +from pokedex.lookup import lookup as pokedex_lookup def main(): if len(sys.argv) <= 1: @@ -14,14 +15,14 @@ def main(): args = sys.argv[2:] # Find the command as a function in this file - func = globals().get(command, None) - if func and callable(func) and command != 'main': + func = globals().get("command_%s" % command, None) + if func: func(*args) else: - help() + command_help() -def csvimport(engine_uri, directory='.'): +def command_csvimport(engine_uri, directory='.'): import csv from sqlalchemy.orm.attributes import instrumentation_registry @@ -103,7 +104,7 @@ def csvimport(engine_uri, directory='.'): try: session.add(row) session.flush() - except IntegrityError as e: + except IntegrityError, e: failed_rows.append(row) # Loop over the failed rows and keep trying to insert them. If a loop @@ -120,7 +121,7 @@ def csvimport(engine_uri, directory='.'): # Success! del failed_rows[i] do_another_loop = True - except IntegrityError as e: + except IntegrityError, e: pass if failed_rows: @@ -128,7 +129,7 @@ def csvimport(engine_uri, directory='.'): else: print 'loaded' -def csvexport(engine_uri, directory='.'): +def command_csvexport(engine_uri, directory='.'): import csv session = connect(engine_uri) @@ -141,7 +142,8 @@ def csvexport(engine_uri, directory='.'): columns = [col.name for col in table.columns] writer.writerow(columns) - for row in session.query(table).all(): + primary_key = table.primary_key + for row in session.query(table).order_by(*primary_key).all(): csvs = [] for col in columns: # Convert Pythony values to something more universal @@ -159,11 +161,25 @@ def csvexport(engine_uri, directory='.'): writer.writerow(csvs) +def command_lookup(engine_uri, name): + # XXX don't require uri! somehow + session = connect(engine_uri) + + results, exact = pokedex_lookup(session, name) + if exact: + print "Matched:" + else: + print "Fuzzy-matched:" + + for object in results: + print object.__tablename__, object.name + -def help(): +def command_help(): print u"""pokedex -- a command-line Pokédex interface help Displays this message. + lookup {uri} [name] Look up something in the Pokédex. These commands are only useful for developers: csvimport {uri} [dir] Import data from a set of CSVs to the database