Removed whoosh hacks; 0.3 obviates them. #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 # Find the command as a function in this file
17 func = globals().get("command_%s" % command, None)
18 if func:
19 func(*args)
20 else:
21 command_help()
22
23
24 def command_dump(*args):
25 parser = OptionParser()
26 parser.add_option('-e', '--engine', dest='engine_uri', default=None)
27 parser.add_option('-d', '--directory', dest='directory', default=None)
28 parser.add_option('-q', '--quiet', dest='verbose', default=True, action='store_false')
29 options, _ = parser.parse_args(list(args))
30
31 session = connect(options.engine_uri)
32 pokedex.db.load.dump(session, directory=options.directory,
33 verbose=options.verbose)
34
35 def command_load(*args):
36 parser = OptionParser()
37 parser.add_option('-e', '--engine', dest='engine_uri', default=None)
38 parser.add_option('-d', '--directory', dest='directory', default=None)
39 parser.add_option('-D', '--drop-tables', dest='drop_tables', default=False, action='store_true')
40 parser.add_option('-q', '--quiet', dest='verbose', default=True, action='store_false')
41 options, _ = parser.parse_args(list(args))
42
43 session = connect(options.engine_uri)
44
45 pokedex.db.load.load(session, directory=options.directory,
46 drop_tables=options.drop_tables,
47 verbose=options.verbose)
48
49 def command_setup(*args):
50 session = connect()
51 pokedex.db.load.load(session, verbose=False, drop_tables=True)
52 pokedex.lookup.open_index(session=session, recreate=True)
53
54
55 def command_lookup(name):
56 results, exact = pokedex.lookup.lookup(name)
57 if exact:
58 print "Matched:"
59 else:
60 print "Fuzzy-matched:"
61
62 for object in results:
63 print object.__tablename__, object.name
64
65
66 def command_help():
67 print u"""pokedex -- a command-line Pokédex interface
68 usage: pokedex {command} [options...]
69 Run `pokedex setup` first, or nothing will work!
70
71 Commands:
72 help Displays this message.
73 lookup [thing] Look up something in the Pokédex.
74
75 System commands:
76 load Load Pokédex data into a database from CSV files.
77 dump Dump Pokédex data from a database into CSV files.
78 setup Loads Pokédex data into the right place and creates a
79 lookup index in the right place. No options or output.
80 This will blow away the default database and index!
81
82 Options:
83 -d|--directory By default, load and dump will use the CSV files in the
84 pokedex install directory. Use this option to specify
85 a different directory.
86 -D|--drop-tables With load, drop all tables before loading data.
87 -e|--engine=URI By default, all commands try to use a SQLite database
88 in the pokedex install directory. Use this option to
89 specify an alternate database.
90 -q|--quiet Turn off any unnecessary status output from dump/load.
91 """.encode(sys.getdefaultencoding(), 'replace')
92
93 sys.exit(0)