<s> import os . path , shutil , sys , random <EOL> from functools import wraps <EOL> from whoosh . filedb . filestore import FileStorage <EOL> from whoosh . util import now <EOL> class TempDir ( object ) : <EOL> def __init__ ( self , parentdir = "<STR_LIT>" , basename = None , ext = "<STR_LIT>" , <EOL> suppress = frozenset ( ) , keepdir = False ) : <EOL> self . basename = basename or hex ( random . randint ( <NUM_LIT:0> , <NUM_LIT> ) ) [ <NUM_LIT:2> : ] <EOL> dirname = os . path . join ( parentdir , self . basename + ext ) <EOL> self . dir = os . path . abspath ( dirname ) <EOL> self . suppress = suppress <EOL> self . keepdir = keepdir <EOL> def __enter__ ( self ) : <EOL> if not os . path . exists ( self . dir ) : <EOL> os . makedirs ( self . dir ) <EOL> return self . dir <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> if not self . keepdir : <EOL> try : <EOL> shutil . rmtree ( self . dir ) <EOL> except OSError : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> print ( "<STR_LIT>" + str ( e ) ) <EOL> if exc_type is not None : <EOL> if self . keepdir : <EOL> print ( "<STR_LIT>" , self . dir ) <EOL> if exc_type not in self . suppress : <EOL> return False <EOL> class TempStorage ( TempDir ) : <EOL> def __enter__ ( self ) : <EOL> dirpath = TempDir . __enter__ ( self ) <EOL> return FileStorage ( dirpath ) <EOL> class TempIndex ( TempStorage ) : <EOL> def __init__ ( self , schema , ixname = '<STR_LIT>' , ** kwargs ) : <EOL> TempStorage . __init__ ( self , basename = ixname , ** kwargs ) <EOL> self . schema = schema <EOL> def __enter__ ( self ) : <EOL> fstore = TempStorage . __enter__ ( self ) <EOL> return fstore . create_index ( self . schema , indexname = self . basename ) <EOL> def skip_if ( cond ) : <EOL> """<STR_LIT>""" <EOL> def decorating_function ( testfn ) : <EOL> @ wraps ( testfn ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> if cond ( ) : <EOL> from nose . plugins . skip import SkipTest <EOL> raise SkipTest <EOL> else : <EOL> return testfn ( * args , ** kwargs ) <EOL> return wrapper <EOL> return decorating_function <EOL> def skip_if_unavailable ( modulename ) : <EOL> """<STR_LIT>""" <EOL> def cantimport ( ) : <EOL> try : <EOL> __import__ ( modulename ) <EOL> except ImportError : <EOL> return True <EOL> else : <EOL> return False <EOL> return skip_if ( cantimport ) <EOL> def is_abstract_method ( attr ) : <EOL> """<STR_LIT>""" <EOL> return ( hasattr ( attr , "<STR_LIT>" ) <EOL> and getattr ( attr , "<STR_LIT>" ) ) <EOL> def check_abstract_methods ( base , subclass ) : <EOL> """<STR_LIT>""" <EOL> for attrname in dir ( base ) : <EOL> if attrname . startswith ( "<STR_LIT:_>" ) : <EOL> continue <EOL> attr = getattr ( base , attrname ) <EOL> if is_abstract_method ( attr ) : <EOL> oattr = getattr ( subclass , attrname ) <EOL> if is_abstract_method ( oattr ) : <EOL> raise Exception ( "<STR_LIT>" <EOL> % ( subclass . __name__ , attrname ) ) <EOL> class Timing ( object ) : <EOL> def __init__ ( self ) : <EOL> pass <EOL> def __enter__ ( self ) : <EOL> self . t = now ( ) <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> if not exc_type : <EOL> print "<STR_LIT>" % ( now ( ) - self . t ) </s>
