<s> """<STR_LIT>""" <EOL> import heapq <EOL> import re <EOL> import os <EOL> from chunkedfile import ChunkedFile <EOL> from utils import Timer <EOL> import parsers <EOL> import search <EOL> class IMDbTitle ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , title , backend = None ) : <EOL> self . title = title <EOL> self . backend = backend <EOL> self . name , self . year , self . unique , self . cat = self . parse ( title ) [ <NUM_LIT:1> : ] <EOL> self . aka = None <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % repr ( self . title ) <EOL> def __unicode__ ( self ) : <EOL> return self . title <EOL> def __str__ ( self ) : <EOL> return self . __unicode__ ( ) . encode ( '<STR_LIT:utf-8>' ) <EOL> parse = staticmethod ( parsers . parse_title ) <EOL> class IMDb ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dbfile , debug = False ) : <EOL> self . dbfile = dbfile <EOL> self . debug = debug <EOL> def rebuild_index ( self , dbdir ) : <EOL> """<STR_LIT>""" <EOL> if os . path . exists ( self . dbfile ) : <EOL> raise Exception ( '<STR_LIT>' % self . dbfile ) <EOL> for parsername , parser in parsers . parsers ( ) : <EOL> obj = parser ( dbfile = self . dbfile , dbdir = dbdir , debug = self . debug ) <EOL> if self . debug : <EOL> print "<STR_LIT>" % parsername <EOL> with Timer ( indent = <NUM_LIT:2> , quiet = not self . debug ) : <EOL> obj . rebuild_index ( do_copy = True ) <EOL> if self . debug : <EOL> print "<STR_LIT>" <EOL> with Timer ( indent = <NUM_LIT:2> , quiet = not self . debug ) : <EOL> search . create_index ( self . dbfile , dbdir , debug = self . debug ) <EOL> def search ( self , query , year = None , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> scores , akascores = search . search ( self . dbfile , query , year , <EOL> debug = self . debug , timeout = timeout ) <EOL> numret = <NUM_LIT:30> <EOL> topscores = heapq . nlargest ( numret , scores , scores . get ) <EOL> titles = dict ( ( title , IMDbTitle ( title , backend = self ) ) for title in topscores ) <EOL> for title , obj in titles . items ( ) : <EOL> if title in akascores : <EOL> obj . aka = akascores [ title ] <EOL> return [ ( titles [ title ] , scores [ title ] ) for title in topscores ] <EOL> def imdbtitle_property ( name ) : <EOL> """<STR_LIT>""" <EOL> populater = '<STR_LIT>' + name <EOL> data_val = '<STR_LIT:_>' + name <EOL> def getter ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , data_val ) : <EOL> populate_func = getattr ( self . backend , populater ) <EOL> populate_func ( ( self , ) ) <EOL> return getattr ( self , data_val ) <EOL> def setter ( self , value ) : <EOL> """<STR_LIT>""" <EOL> setattr ( self , data_val , value ) <EOL> return ( getter , setter ) <EOL> def imdb_populator ( parserclass , prop , default ) : <EOL> """<STR_LIT>""" <EOL> def populate ( self , titles ) : <EOL> """<STR_LIT>""" <EOL> titles = tuple ( title for title in titles ) <EOL> parser = parserclass ( dbfile = self . dbfile , debug = self . debug ) <EOL> results = parser . search ( title . title for title in titles ) <EOL> for title in titles : <EOL> if title . title in results : <EOL> setattr ( title , prop , results [ title . title ] ) <EOL> else : <EOL> setattr ( title , prop , default ) <EOL> return populate <EOL> def _install_parsers ( ) : <EOL> """<STR_LIT>""" <EOL> property_name = re . compile ( r'<STR_LIT>' ) <EOL> for name , parser in parsers . parsers ( ) : <EOL> name = property_name . sub ( r'<STR_LIT>' , name ) . lower ( ) <EOL> populator = imdb_populator ( parser , name , default = parser . default ) <EOL> setattr ( IMDb , '<STR_LIT>' + name , populator ) <EOL> prop = property ( * imdbtitle_property ( name ) , <EOL> doc = """<STR_LIT>""" + name + """<STR_LIT>""" ) <EOL> setattr ( IMDbTitle , name , prop ) <EOL> _install_parsers ( ) </s>
