Add Gen III wild held items.
[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 or 'utf8'
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 result in results:
70 if hasattr(result.object, 'full_name'):
71 name = result.object.full_name
72 else:
73 name = result.object.name
74
75 print "%s: %s" % (result.object.__tablename__, name),
76 if result.language:
77 print "(%s in %s)" % (result.name, result.language)
78 else:
79 print
80
81
82 def command_help():
83 print u"""pokedex -- a command-line Pokédex interface
84 usage: pokedex {command} [options...]
85 Run `pokedex setup` first, or nothing will work!
86
87 Commands:
88 help Displays this message.
89 lookup [thing] Look up something in the Pokédex.
90
91 System commands:
92 load Load Pokédex data into a database from CSV files.
93 dump Dump Pokédex data from a database into CSV files.
94 setup Loads Pokédex data into the right place and creates a
95 lookup index in the right place. No options or output.
96 This will blow away the default database and index!
97
98 Options:
99 -d|--directory By default, load and dump will use the CSV files in the
100 pokedex install directory. Use this option to specify
101 a different directory.
102 -D|--drop-tables With load, drop all tables before loading data.
103 -e|--engine=URI By default, all commands try to use a SQLite database
104 in the pokedex install directory. Use this option to
105 specify an alternate database.
106 -q|--quiet Turn off any unnecessary status output from dump/load.
107 """.encode(sys.getdefaultencoding(), 'replace')
108
109 sys.exit(0)