X-Git-Url: http://git.veekun.com/zzz-pokedex.git/blobdiff_plain/6af0c42ed6234d8db295b8bf3811fbacac75dbc6..09495b1a01a9f5a0b4f74efc9234f2afbc83277a:/pokedex/__init__.py diff --git a/pokedex/__init__.py b/pokedex/__init__.py index df995ab..499d8d2 100644 --- a/pokedex/__init__.py +++ b/pokedex/__init__.py @@ -4,7 +4,7 @@ import sys from .db import connect, metadata import pokedex.db.load -from pokedex.lookup import lookup as pokedex_lookup +import pokedex.lookup def main(): if len(sys.argv) <= 1: @@ -13,6 +13,11 @@ def main(): command = sys.argv[1] args = sys.argv[2:] + # XXX there must be a better way to get Unicode argv + # XXX this doesn't work on Windows durp + enc = sys.stdin.encoding or 'utf8' + args = [_.decode(enc) for _ in args] + # Find the command as a function in this file func = globals().get("command_%s" % command, None) if func: @@ -25,36 +30,53 @@ def command_dump(*args): parser = OptionParser() parser.add_option('-e', '--engine', dest='engine_uri', default=None) parser.add_option('-d', '--directory', dest='directory', default=None) + parser.add_option('-q', '--quiet', dest='verbose', default=True, action='store_false') options, _ = parser.parse_args(list(args)) session = connect(options.engine_uri) - pokedex.db.load.dump(session, directory=options.directory) + pokedex.db.load.dump(session, directory=options.directory, + verbose=options.verbose) def command_load(*args): parser = OptionParser() parser.add_option('-e', '--engine', dest='engine_uri', default=None) parser.add_option('-d', '--directory', dest='directory', default=None) parser.add_option('-D', '--drop-tables', dest='drop_tables', default=False, action='store_true') + parser.add_option('-q', '--quiet', dest='verbose', default=True, action='store_false') options, _ = parser.parse_args(list(args)) session = connect(options.engine_uri) pokedex.db.load.load(session, directory=options.directory, - drop_tables=options.drop_tables) + drop_tables=options.drop_tables, + verbose=options.verbose) +def command_setup(*args): + session = connect() + pokedex.db.load.load(session, verbose=False, drop_tables=True) + pokedex.lookup.PokedexLookup(session=session, recreate=True) -def command_lookup(engine_uri, name): - # XXX don't require uri! somehow - session = connect(engine_uri) - results, exact = pokedex_lookup(session, name) - if exact: +def command_lookup(name): + results = pokedex.lookup.PokedexLookup().lookup(name) + if not results: + print "No matches." + elif results[0].exact: print "Matched:" else: print "Fuzzy-matched:" - for object in results: - print object.__tablename__, object.name + for result in results: + if hasattr(result.object, 'full_name'): + name = result.object.full_name + else: + name = result.object.name + + print "%s: %s" % (result.object.__tablename__, name), + if result.language: + print "(%s in %s)" % (result.name, result.language) + else: + print def command_help(): @@ -69,6 +91,9 @@ Commands: System commands: load Load Pokédex data into a database from CSV files. dump Dump Pokédex data from a database into CSV files. + setup Loads Pokédex data into the right place and creates a + lookup index in the right place. No options or output. + This will blow away the default database and index! Options: -d|--directory By default, load and dump will use the CSV files in the @@ -78,6 +103,7 @@ Options: -e|--engine=URI By default, all commands try to use a SQLite database in the pokedex install directory. Use this option to specify an alternate database. + -q|--quiet Turn off any unnecessary status output from dump/load. """.encode(sys.getdefaultencoding(), 'replace') sys.exit(0)