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
or 'utf8'
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
.PokedexLookup(session
=session
, recreate
=True)
60 def command_lookup(name
):
61 results
= pokedex
.lookup
.PokedexLookup().lookup(name
)
64 elif results
[0].exact
:
67 print "Fuzzy-matched:"
69 for result
in results
:
70 if hasattr(result
.object, 'full_name'):
71 name
= result
.object.full_name
73 name
= result
.object.name
75 print "%s: %s" %
(result
.object.__tablename__
, name
),
77 print "(%s in %s)" %
(result
.name
, result
.language
)
83 print u
"""pokedex -- a command-line Pokédex interface
84 usage: pokedex {command} [options...]
85 Run `pokedex setup` first, or nothing will work!
88 help Displays this message.
89 lookup [thing] Look up something in the Pokédex.
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!
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')