df995ab59a61dc14730d7d7395d5515d6c1cd636
[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 from pokedex.lookup import lookup as 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 options, _ = parser.parse_args(list(args))
29
30 session = connect(options.engine_uri)
31 pokedex.db.load.dump(session, directory=options.directory)
32
33 def command_load(*args):
34 parser = OptionParser()
35 parser.add_option('-e', '--engine', dest='engine_uri', default=None)
36 parser.add_option('-d', '--directory', dest='directory', default=None)
37 parser.add_option('-D', '--drop-tables', dest='drop_tables', default=False, action='store_true')
38 options, _ = parser.parse_args(list(args))
39
40 session = connect(options.engine_uri)
41
42 pokedex.db.load.load(session, directory=options.directory,
43 drop_tables=options.drop_tables)
44
45
46 def command_lookup(engine_uri, name):
47 # XXX don't require uri! somehow
48 session = connect(engine_uri)
49
50 results, exact = pokedex_lookup(session, name)
51 if exact:
52 print "Matched:"
53 else:
54 print "Fuzzy-matched:"
55
56 for object in results:
57 print object.__tablename__, object.name
58
59
60 def command_help():
61 print u"""pokedex -- a command-line Pokédex interface
62 usage: pokedex {command} [options...]
63 Run `pokedex setup` first, or nothing will work!
64
65 Commands:
66 help Displays this message.
67 lookup [thing] Look up something in the Pokédex.
68
69 System commands:
70 load Load Pokédex data into a database from CSV files.
71 dump Dump Pokédex data from a database into CSV files.
72
73 Options:
74 -d|--directory By default, load and dump will use the CSV files in the
75 pokedex install directory. Use this option to specify
76 a different directory.
77 -D|--drop-tables With load, drop all tables before loading data.
78 -e|--engine=URI By default, all commands try to use a SQLite database
79 in the pokedex install directory. Use this option to
80 specify an alternate database.
81 """.encode(sys.getdefaultencoding(), 'replace')
82
83 sys.exit(0)