+ # Defaults
+ if not directory:
+ directory = pkg_resources.resource_filename('pokedex',
+ 'data/whoosh_index')
+
+ if not session:
+ session = connect()
+
+ # Attempt to open or create the index
+ directory_exists = os.path.exists(directory)
+ if directory_exists and not recreate:
+ # Already exists; should be an index!
+ try:
+ index = whoosh.index.open_dir(directory, indexname='pokedex')
+ speller = whoosh.index.open_dir(directory, indexname='spelling')
+ return index, speller
+ except whoosh.index.EmptyIndexError as e:
+ # Apparently not a real index. Fall out of the if and create it
+ pass