4af3fa0e6b58ab36df5c47b8a6f4fa31996524dd
2 from optparse
import OptionParser
5 from .db
import connect
, metadata
10 if len(sys
.argv
) <= 1:
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
]
21 # Find the command as a function in this file
22 func
= globals().get("command_%s" % command
, None)
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
))
36 session
= connect(options
.engine_uri
)
37 pokedex
.db
.load
.dump(session
, directory
=options
.directory
,
38 verbose
=options
.verbose
)
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
))
48 session
= connect(options
.engine_uri
)
50 pokedex
.db
.load
.load(session
, directory
=options
.directory
,
51 drop_tables
=options
.drop_tables
,
52 verbose
=options
.verbose
)
54 def command_setup(*args
):
56 pokedex
.db
.load
.load(session
, verbose
=False, drop_tables
=True)
57 pokedex
.lookup
.open_index(session
=session
, recreate
=True)
60 def command_lookup(name
):
61 results
= pokedex
.lookup
.lookup(name
)
64 elif results
[0].exact
:
67 print "Fuzzy-matched:"
69 for object, language
, exact
in results
:
70 print object.__tablename__
, object.name
, language
74 print u
"""pokedex -- a command-line Pokédex interface
75 usage: pokedex {command} [options...]
76 Run `pokedex setup` first, or nothing will work!
79 help Displays this message.
80 lookup [thing] Look up something in the Pokédex.
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!
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')