<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> from datetime import datetime <EOL> from operator import itemgetter <EOL> from os . path import basename , splitext <EOL> from collections import defaultdict <EOL> from gate import ui <EOL> from gate import project <EOL> from gate import session <EOL> from gate . utils import v <EOL> from gate . utils import settings <EOL> from gate . utils . match import match <EOL> from gate . exceptions import GateUnknownProfile <EOL> class Gate : <EOL> def __init__ ( self ) : <EOL> self . project = project . Project ( ) <EOL> self . session = session . Session ( ) <EOL> self . ui = ui . UserInterface ( self ) <EOL> self . history = defaultdict ( datetime . now ) <EOL> self . frequency = defaultdict ( int ) <EOL> self . banned = set ( ) <EOL> def update_history ( self ) : <EOL> """<STR_LIT>""" <EOL> buf = v . bufname ( ) <EOL> self . history [ buf ] = datetime . now ( ) <EOL> self . frequency [ buf ] += <NUM_LIT:1> <EOL> def open ( self ) : <EOL> """<STR_LIT>""" <EOL> self . ui . open ( ) <EOL> def find ( self , query , max_results = - <NUM_LIT:1> , curr_buf = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> profile , query = self . _split_query ( query . strip ( ) ) <EOL> if query or ( not profile and not query ) : <EOL> if profile : <EOL> if profile in settings . get ( "<STR_LIT>" ) : <EOL> files = self . project . get_files ( profile , curr_buf ) <EOL> else : <EOL> raise GateUnknownProfile ( "<STR_LIT>" ) <EOL> else : <EOL> files = self . session . get_files ( curr_buf ) <EOL> return self . _find ( query , files , max_results ) <EOL> return [ ] <EOL> def _split_query ( self , query ) : <EOL> """<STR_LIT>""" <EOL> if len ( query . split ( "<STR_LIT::>" ) ) > <NUM_LIT:1> : <EOL> return query . split ( "<STR_LIT::>" , <NUM_LIT:1> ) <EOL> return u"<STR_LIT>" , query <EOL> def _find ( self , query , files , max_results ) : <EOL> """<STR_LIT>""" <EOL> smart_case = settings . get ( "<STR_LIT>" , int ) <EOL> match_ext = settings . get ( "<STR_LIT>" , int ) <EOL> def fname ( f ) : <EOL> if match_ext : <EOL> return basename ( f ) <EOL> else : <EOL> return splitext ( basename ( f ) ) [ <NUM_LIT:0> ] <EOL> matches = [ ] <EOL> for f in ( f for f in files if f not in self . banned ) : <EOL> if not query : <EOL> similarity , positions = self . history [ f ] , ( v . opt ( "<STR_LIT>" , fmt = int ) , ) <EOL> else : <EOL> similarity , positions = match ( query , fname ( f ) , smart_case ) <EOL> similarity *= ( <NUM_LIT:1> + self . frequency [ f ] / <NUM_LIT:100> ) <EOL> if positions : <EOL> matches . append ( { <EOL> "<STR_LIT>" : positions , <EOL> "<STR_LIT>" : similarity , <EOL> "<STR_LIT:path>" : f , <EOL> } ) <EOL> if max_results < <NUM_LIT:0> or max_results > len ( matches ) : <EOL> max_results = len ( matches ) <EOL> keyfn = itemgetter ( "<STR_LIT>" ) <EOL> return sorted ( matches , key = keyfn , reverse = True ) [ : max_results ] </s>
