Added lookup support for foreign language names. #15
[zzz-pokedex.git] / pokedex / __init__.py
1 # encoding: utf8
2 from optparse import OptionParser
3 import sys
4
5 from .db import connect, metadata
6 import pokedex.db.load
7 import pokedex.lookup
8
9 def main():
10 if len(sys.argv) <= 1:
11 command_help()
12
13 command = sys.argv[1]
14 args = sys.argv[2:]
15
16 # XXX there must be a better way to get Unicode argv
17 # XXX this doesn't work on Windows durp
18 enc = sys.stdin.encoding
19 args = [_.decode(enc) for _ in args]
20
21 # Find the command as a function in this file
22 func = globals().get("command_%s" % command, None)
23 if func:
24 func(*args)
25 else:
26 command_help()
27
28
29 def command_dump(*args):
30 parser = OptionParser()
31 parser.add_option('-e', '--engine', dest='engine_uri', default=None)
32 parser.add_option('-d', '--directory', dest='directory', default=None)
33 parser.add_option('-q', '--quiet', dest='verbose', default=True, action='store_false')
34 options, _ = parser.parse_args(list(args))
35
36 session = connect(options.engine_uri)
37 pokedex.db.load.dump(session, directory=options.directory,
38 verbose=options.verbose)
39
40 def command_load(*args):
41 parser = OptionParser()
42 parser.add_option('-e', '--engine', dest='engine_uri', default=None)
43 parser.add_option('-d', '--directory', dest='directory', default=None)
44 parser.add_option('-D', '--drop-tables', dest='drop_tables', default=False, action='store_true')
45 parser.add_option('-q', '--quiet', dest='verbose', default=True, action='store_false')
46 options, _ = parser.parse_args(list(args))
47
48 session = connect(options.engine_uri)
49
50 pokedex.db.load.load(session, directory=options.directory,
51 drop_tables=options.drop_tables,
52 verbose=options.verbose)
53
54 def command_setup(*args):
55 session = connect()
56 pokedex.db.load.load(session, verbose=False, drop_tables=True)
57 pokedex.lookup.open_index(session=session, recreate=True)
58
59
60 def command_lookup(name):
61 results = pokedex.lookup.lookup(name)
62 if not results:
63 print "No matches."
64 elif results[0].exact:
65 print "Matched:"
66 else:
67 print "Fuzzy-matched:"
68
69 for object, language, exact in results:
70 print object.__tablename__, object.name, language
71
72
73 def command_help():
74 print u"""pokedex -- a command-line Pokédex interface
75 usage: pokedex {command} [options...]
76 Run `pokedex setup` first, or nothing will work!
77
78 Commands:
79 help Displays this message.
80 lookup [thing] Look up something in the Pokédex.
81
82 System commands:
83 load Load Pokédex data into a database from CSV files.
84 dump Dump Pokédex data from a database into CSV files.
85 setup Loads Pokédex data into the right place and creates a
86 lookup index in the right place. No options or output.
87 This will blow away the default database and index!
88
89 Options:
90 -d|--directory By default, load and dump will use the CSV files in the
91 pokedex install directory. Use this option to specify
92 a different directory.
93 -D|--drop-tables With load, drop all tables before loading data.
94 -e|--engine=URI By default, all commands try to use a SQLite database
95 in the pokedex install directory. Use this option to
96 specify an alternate database.
97 -q|--quiet Turn off any unnecessary status output from dump/load.
98 """.encode(sys.getdefaultencoding(), 'replace')
99
100 sys.exit(0)