2009-02-05 08:05:42 +00:00
|
|
|
# encoding: utf8
|
2009-08-19 01:02:53 +00:00
|
|
|
from optparse import OptionParser
|
2009-02-05 08:05:42 +00:00
|
|
|
import sys
|
|
|
|
|
2009-08-19 01:02:53 +00:00
|
|
|
from .db import connect, metadata
|
|
|
|
import pokedex.db.load
|
2009-08-19 06:42:59 +00:00
|
|
|
import pokedex.lookup
|
2009-02-05 08:05:42 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
if len(sys.argv) <= 1:
|
2009-08-19 01:02:53 +00:00
|
|
|
command_help()
|
2009-02-05 08:05:42 +00:00
|
|
|
|
|
|
|
command = sys.argv[1]
|
|
|
|
args = sys.argv[2:]
|
|
|
|
|
2009-08-21 07:30:01 +00:00
|
|
|
# XXX there must be a better way to get Unicode argv
|
|
|
|
# XXX this doesn't work on Windows durp
|
2009-08-25 15:07:54 +00:00
|
|
|
enc = sys.stdin.encoding or 'utf8'
|
2009-08-21 07:30:01 +00:00
|
|
|
args = [_.decode(enc) for _ in args]
|
|
|
|
|
2009-02-05 08:05:42 +00:00
|
|
|
# Find the command as a function in this file
|
2009-07-25 09:43:30 +00:00
|
|
|
func = globals().get("command_%s" % command, None)
|
|
|
|
if func:
|
2009-02-05 08:05:42 +00:00
|
|
|
func(*args)
|
|
|
|
else:
|
2009-07-25 09:43:30 +00:00
|
|
|
command_help()
|
2009-02-05 08:05:42 +00:00
|
|
|
|
|
|
|
|
2009-08-19 01:02:53 +00:00
|
|
|
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)
|
2009-08-19 01:36:45 +00:00
|
|
|
parser.add_option('-q', '--quiet', dest='verbose', default=True, action='store_false')
|
2009-08-19 01:02:53 +00:00
|
|
|
options, _ = parser.parse_args(list(args))
|
2009-02-05 08:05:42 +00:00
|
|
|
|
2009-08-19 01:02:53 +00:00
|
|
|
session = connect(options.engine_uri)
|
2009-08-19 01:36:45 +00:00
|
|
|
pokedex.db.load.dump(session, directory=options.directory,
|
|
|
|
verbose=options.verbose)
|
2009-02-05 08:05:42 +00:00
|
|
|
|
2009-08-19 01:02:53 +00:00
|
|
|
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')
|
2009-08-19 01:36:45 +00:00
|
|
|
parser.add_option('-q', '--quiet', dest='verbose', default=True, action='store_false')
|
2009-08-19 01:02:53 +00:00
|
|
|
options, _ = parser.parse_args(list(args))
|
2009-02-05 08:05:42 +00:00
|
|
|
|
2009-08-19 01:02:53 +00:00
|
|
|
session = connect(options.engine_uri)
|
|
|
|
|
|
|
|
pokedex.db.load.load(session, directory=options.directory,
|
2009-08-19 01:36:45 +00:00
|
|
|
drop_tables=options.drop_tables,
|
|
|
|
verbose=options.verbose)
|
2009-02-05 08:05:42 +00:00
|
|
|
|
2009-08-19 06:42:59 +00:00
|
|
|
def command_setup(*args):
|
|
|
|
session = connect()
|
|
|
|
pokedex.db.load.load(session, verbose=False, drop_tables=True)
|
|
|
|
pokedex.lookup.open_index(session=session, recreate=True)
|
2009-02-05 08:05:42 +00:00
|
|
|
|
2009-07-23 06:44:06 +00:00
|
|
|
|
2009-08-19 06:42:59 +00:00
|
|
|
def command_lookup(name):
|
2009-08-21 07:30:01 +00:00
|
|
|
results = pokedex.lookup.lookup(name)
|
|
|
|
if not results:
|
|
|
|
print "No matches."
|
|
|
|
elif results[0].exact:
|
2009-07-25 08:28:33 +00:00
|
|
|
print "Matched:"
|
|
|
|
else:
|
|
|
|
print "Fuzzy-matched:"
|
|
|
|
|
2009-08-22 08:13:34 +00:00
|
|
|
for result in results:
|
2009-08-23 23:27:13 +00:00
|
|
|
if hasattr(result.object, 'full_name'):
|
|
|
|
name = result.object.full_name
|
|
|
|
else:
|
|
|
|
name = result.object.name
|
|
|
|
|
|
|
|
print "%s: %s" % (result.object.__tablename__, name),
|
2009-08-22 08:13:34 +00:00
|
|
|
if result.language:
|
|
|
|
print "(%s in %s)" % (result.name, result.language)
|
|
|
|
else:
|
|
|
|
print
|
2009-07-23 06:44:06 +00:00
|
|
|
|
2009-02-05 08:05:42 +00:00
|
|
|
|
2009-07-25 09:43:30 +00:00
|
|
|
def command_help():
|
2009-02-05 08:05:42 +00:00
|
|
|
print u"""pokedex -- a command-line Pokédex interface
|
2009-08-19 01:02:53 +00:00
|
|
|
usage: pokedex {command} [options...]
|
|
|
|
Run `pokedex setup` first, or nothing will work!
|
|
|
|
|
|
|
|
Commands:
|
|
|
|
help Displays this message.
|
|
|
|
lookup [thing] Look up something in the Pokédex.
|
|
|
|
|
|
|
|
System commands:
|
|
|
|
load Load Pokédex data into a database from CSV files.
|
|
|
|
dump Dump Pokédex data from a database into CSV files.
|
2009-08-19 06:42:59 +00:00
|
|
|
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!
|
2009-08-19 01:02:53 +00:00
|
|
|
|
|
|
|
Options:
|
|
|
|
-d|--directory By default, load and dump will use the CSV files in the
|
|
|
|
pokedex install directory. Use this option to specify
|
|
|
|
a different directory.
|
|
|
|
-D|--drop-tables With load, drop all tables before loading data.
|
|
|
|
-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.
|
2009-08-19 01:36:45 +00:00
|
|
|
-q|--quiet Turn off any unnecessary status output from dump/load.
|
2009-07-29 01:31:06 +00:00
|
|
|
""".encode(sys.getdefaultencoding(), 'replace')
|
2009-02-05 08:05:42 +00:00
|
|
|
|
|
|
|
sys.exit(0)
|