<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import os . path <EOL> from whoosh import fields , store <EOL> _DEF_INDEX_NAME = "<STR_LIT>" <EOL> class IndexError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class IndexVersionError ( IndexError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , msg , version , release = None ) : <EOL> Exception . __init__ ( self , msg ) <EOL> self . version = version <EOL> self . release = release <EOL> class OutOfDateError ( IndexError ) : <EOL> """<STR_LIT>""" <EOL> class EmptyIndexError ( IndexError ) : <EOL> """<STR_LIT>""" <EOL> def create_in ( dirname , schema , indexname = None ) : <EOL> """<STR_LIT>""" <EOL> if not indexname : <EOL> indexname = _DEF_INDEX_NAME <EOL> from whoosh . filedb . filestore import FileStorage <EOL> storage = FileStorage ( dirname ) <EOL> return storage . create_index ( schema , indexname ) <EOL> def open_dir ( dirname , indexname = None , mapped = True ) : <EOL> """<STR_LIT>""" <EOL> if indexname is None : <EOL> indexname = _DEF_INDEX_NAME <EOL> from whoosh . filedb . filestore import FileStorage <EOL> storage = FileStorage ( dirname , mapped = mapped ) <EOL> return storage . open_index ( indexname ) <EOL> def exists_in ( dirname , indexname = None ) : <EOL> """<STR_LIT>""" <EOL> if os . path . exists ( dirname ) : <EOL> try : <EOL> ix = open_dir ( dirname , indexname = indexname ) <EOL> return ix . latest_generation ( ) > - <NUM_LIT:1> <EOL> except EmptyIndexError : <EOL> pass <EOL> return False <EOL> def exists ( storage , indexname = None ) : <EOL> """<STR_LIT>""" <EOL> if indexname is None : <EOL> indexname = _DEF_INDEX_NAME <EOL> try : <EOL> ix = storage . open_index ( indexname ) <EOL> return ix . latest_generation ( ) > - <NUM_LIT:1> <EOL> except EmptyIndexError : <EOL> pass <EOL> return False <EOL> def version_in ( dirname , indexname = None ) : <EOL> """<STR_LIT>""" <EOL> from whoosh . filedb . filestore import FileStorage <EOL> storage = FileStorage ( dirname ) <EOL> return version ( storage , indexname = indexname ) <EOL> def version ( storage , indexname = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if indexname is None : <EOL> indexname = _DEF_INDEX_NAME <EOL> ix = storage . open_index ( indexname ) <EOL> return ( ix . release , ix . version ) <EOL> except IndexVersionError , e : <EOL> return ( None , e . version ) <EOL> class DeletionMixin ( object ) : <EOL> def delete_by_term ( self , fieldname , text ) : <EOL> """<STR_LIT>""" <EOL> from whoosh . query import Term <EOL> q = Term ( fieldname , text ) <EOL> return self . delete_by_query ( q ) <EOL> def delete_by_query ( self , q ) : <EOL> """<STR_LIT>""" <EOL> count = <NUM_LIT:0> <EOL> for docnum in q . docs ( self . searcher ( ) ) : <EOL> self . delete_document ( docnum ) <EOL> count += <NUM_LIT:1> <EOL> return count <EOL> class Index ( DeletionMixin ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , storage , schema = None , indexname = _DEF_INDEX_NAME ) : <EOL> """<STR_LIT>""" <EOL> self . storage = storage <EOL> self . indexname = indexname <EOL> if schema is not None and not isinstance ( schema , fields . Schema ) : <EOL> raise ValueError ( "<STR_LIT>" % schema ) <EOL> self . schema = schema <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def delete_document ( self , docnum , delete = True ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def latest_generation ( self ) : <EOL> """<STR_LIT>""" <EOL> return - <NUM_LIT:1> <EOL> def refresh ( self ) : <EOL> """<STR_LIT>""" <EOL> return self <EOL> def up_to_date ( self ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> def last_modified ( self ) : <EOL> """<STR_LIT>""" <EOL> return - <NUM_LIT:1> <EOL> def is_empty ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def optimize ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def commit ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def doc_count_all ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def doc_count ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def field_length ( self , fieldid ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def searcher ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> from whoosh . searching import Searcher <EOL> return Searcher ( self . reader ( ) , ** kwargs ) <EOL> def reader ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def writer ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> pass </s>
