dde6b5fcd927ed5769e62325ed36a7419b8fdf65
2 from optparse
import OptionParser
12 if len(sys
.argv
) <= 1:
18 # XXX there must be a better way to get Unicode argv
19 # XXX this doesn't work on Windows durp
20 enc
= sys
.stdin
.encoding
or 'utf8'
21 args
= [_
.decode(enc
) for _
in args
]
23 # Find the command as a function in this file
24 func
= globals().get("command_%s" % command
, None)
31 def get_parser(verbose
=True):
32 """Returns an OptionParser prepopulated with the global options.
34 `verbose` is whether or not the options should be verbose by default.
36 parser
= OptionParser()
37 parser
.add_option('-e', '--engine', dest
='engine_uri', default
=os
.environ
.get('POKEDEX_DB_ENGINE', None))
38 parser
.add_option('-i', '--index', dest
='index_dir', default
=os
.environ
.get('POKEDEX_INDEX_DIR', None))
39 parser
.add_option('-q', '--quiet', dest
='verbose', default
=verbose
, action
='store_false')
40 parser
.add_option('-v', '--verbose', dest
='verbose', default
=verbose
, action
='store_true')
43 def get_session(options
):
44 """Given a parsed options object, connects to the database and returns a
48 engine_uri
= options
.engine_uri
51 got_from
= 'command line'
53 engine_uri
= os
.environ
.get('POKEDEX_DB_ENGINE', None)
55 got_from
= 'environment'
57 got_from
= 'default setting'
59 session
= pokedex
.db
.connect(engine_uri
)
62 print "Connected to database {engine} (from {got_from})" \
63 .format(engine
=session
.bind
.url
, got_from
=got_from
)
67 def get_lookup(options
, session
=None, recreate
=False):
68 """Given a parsed options object, opens the whoosh index and returns a
71 Unlike `get_session`, this function can actually do population as a side
72 effect! This is fallout from how PokedexLookup works.
76 if recreate
and not session
:
77 raise ValueError("get_lookup() needs an explicit session to regen the index")
79 index_dir
= options
.index_dir
82 got_from
= 'command line'
84 index_dir
= os
.environ
.get('POKEDEX_INDEX_DIR', None)
86 got_from
= 'environment'
88 index_dir
= pkg_resources
.resource_filename('pokedex',
90 got_from
= 'default setting'
93 print "Opened lookup index {index_dir} (from {got_from})" \
94 .format(index_dir
=index_dir
, got_from
=got_from
)
96 lookup
= pokedex
.lookup
.PokedexLookup(index_dir
, session
=session
)
99 lookup
.rebuild_index()
103 def print_csv_directory(options
):
104 """Just prints the csv directory we're about to use."""
106 if not options
.verbose
:
109 if options
.directory
:
110 csvdir
= options
.directory
111 got_from
= 'command line'
113 # This is the same as the db.load default
114 csvdir
= pkg_resources
.resource_filename('pokedex', 'data/csv')
115 got_from
= 'default setting'
117 print "Using CSV directory {csvdir} (from {got_from})" \
118 .format(csvdir
=csvdir
, got_from
=got_from
)
121 ### Plumbing commands
123 def command_dump(*args
):
124 parser
= get_parser(verbose
=True)
125 parser
.add_option('-d', '--directory', dest
='directory', default
=None)
126 options
, tables
= parser
.parse_args(list(args
))
128 session
= get_session(options
)
129 print_csv_directory(options
)
131 pokedex
.db
.load
.dump(session
, directory
=options
.directory
,
133 verbose
=options
.verbose
)
135 def command_load(*args
):
136 parser
= get_parser(verbose
=True)
137 parser
.add_option('-d', '--directory', dest
='directory', default
=None)
138 parser
.add_option('-D', '--drop-tables', dest
='drop_tables', default
=False, action
='store_true')
139 options
, tables
= parser
.parse_args(list(args
))
141 if not options
.engine_uri
:
142 print "WARNING: You're reloading the default database, but not the lookup index. They"
143 print " might get out of sync, and pokedex commands may not work correctly!"
144 print "To fix this, run `pokedex reindex` when this command finishes. Or, just use"
145 print "`pokedex setup` to do both at once."
148 session
= get_session(options
)
149 print_csv_directory(options
)
151 pokedex
.db
.load
.load(session
, directory
=options
.directory
,
152 drop_tables
=options
.drop_tables
,
154 verbose
=options
.verbose
)
156 def command_reindex(*args
):
157 parser
= get_parser(verbose
=True)
158 options
, _
= parser
.parse_args(list(args
))
160 session
= get_session(options
)
161 lookup
= get_lookup(options
, session
=session
, recreate
=True)
163 print "Recreated lookup index."
166 def command_setup(*args
):
167 parser
= get_parser(verbose
=False)
168 options
, _
= parser
.parse_args(list(args
))
170 options
.directory
= None
172 session
= get_session(options
)
173 print_csv_directory(options
)
174 pokedex
.db
.load
.load(session
, directory
=None, drop_tables
=True,
175 verbose
=options
.verbose
)
177 lookup
= get_lookup(options
, session
=session
, recreate
=True)
179 print "Recreated lookup index."
182 def command_status(*args
):
183 parser
= get_parser(verbose
=True)
184 options
, _
= parser
.parse_args(list(args
))
185 options
.verbose
= True
186 options
.directory
= None
188 session
= get_session(options
)
189 print_csv_directory(options
)
190 lookup
= get_lookup(options
, recreate
=False)
193 ### User-facing commands
195 def command_lookup(*args
):
196 parser
= get_parser(verbose
=False)
197 options
, words
= parser
.parse_args(list(args
))
199 name
= u
' '.join(words
)
201 session
= get_session(options
)
202 lookup
= get_lookup(options
, session
=session
, recreate
=False)
204 results
= lookup
.lookup(name
)
207 elif results
[0].exact
:
210 print "Fuzzy-matched:"
212 for result
in results
:
213 if hasattr(result
.object, 'full_name'):
214 name
= result
.object.full_name
216 name
= result
.object.name
218 print "%s: %s" %
(result
.object.__tablename__
, name
),
220 print "(%s in %s)" %
(result
.name
, result
.language
)
226 print u
"""pokedex -- a command-line Pokédex interface
227 usage: pokedex {command} [options...]
228 Run `pokedex setup` first, or nothing will work!
229 See http://bugs.veekun.com/projects/pokedex/wiki/CLI for more documentation.
232 help Displays this message.
233 lookup [thing] Look up something in the Pokédex.
236 load Load Pokédex data into a database from CSV files.
237 dump Dump Pokédex data from a database into CSV files.
238 reindex Rebuilds the lookup index from the database.
239 setup Combines load and reindex.
240 status No effect, but prints which engine, index, and csv
241 directory would be used for other commands.
244 -e|--engine=URI By default, all commands try to use a SQLite database
245 in the pokedex install directory. Use this option (or
246 a POKEDEX_DB_ENGINE environment variable) to specify an
248 -i|--index=DIR By default, all commands try to put the lookup index in
249 the pokedex install directory. Use this option (or a
250 POKEDEX_INDEX_DIR environment variable) to specify an
252 -q|--quiet Don't print system output. This is the default for
253 non-system commands and setup.
254 -v|--verbose Print system output. This is the default for system
255 commands, except setup.
258 -d|--directory=DIR By default, load and dump will use the CSV files in the
259 pokedex install directory. Use this option to specify
260 a different directory.
261 -D|--drop-tables With load, drop all tables before loading data.
263 Additionally, load and dump accept a list of table names (possibly with
264 wildcards) and/or csv fileames as an argument list.
265 """.encode(sys
.getdefaultencoding(), 'replace')