2 from optparse
import OptionParser
5 from .db
import connect
, metadata
10 if len(sys
.argv
) <= 1:
16 # Find the command as a function in this file
17 func
= globals().get("command_%s" % command
, None)
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
))
31 session
= connect(options
.engine_uri
)
32 pokedex
.db
.load
.dump(session
, directory
=options
.directory
,
33 verbose
=options
.verbose
)
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
))
43 session
= connect(options
.engine_uri
)
45 pokedex
.db
.load
.load(session
, directory
=options
.directory
,
46 drop_tables
=options
.drop_tables
,
47 verbose
=options
.verbose
)
49 def command_setup(*args
):
51 pokedex
.db
.load
.load(session
, verbose
=False, drop_tables
=True)
52 pokedex
.lookup
.open_index(session
=session
, recreate
=True)
55 def command_lookup(name
):
56 results
, exact
= pokedex
.lookup
.lookup(name
)
60 print "Fuzzy-matched:"
62 for object in results
:
63 print object.__tablename__
, object.name
67 print u
"""pokedex -- a command-line Pokédex interface
68 usage: pokedex {command} [options...]
69 Run `pokedex setup` first, or nothing will work!
72 help Displays this message.
73 lookup [thing] Look up something in the Pokédex.
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!
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')