<s> import os <EOL> import re <EOL> from copy import copy <EOL> from collections import deque <EOL> from time import time <EOL> import sublime <EOL> from robot . api import TestCaseFile , ResourceFile <EOL> from robot . errors import DataError <EOL> from scanner_cache import ScannerCache <EOL> from string_populator import populate_from_lines <EOL> scanner_cache = ScannerCache ( ) <EOL> SCAN_TIMEOUT = <NUM_LIT:5> <EOL> detect_robot_regex = '<STR_LIT>' <EOL> class WrappedKeyword : <EOL> def __init__ ( self , data_file , keyword , file_path ) : <EOL> self . keyword = keyword <EOL> self . name = data_file . name + '<STR_LIT:.>' + keyword . name <EOL> self . file_path = file_path <EOL> self . description = [ ] <EOL> args = '<STR_LIT:U+002CU+0020>' . join ( keyword . args . value ) <EOL> if args : <EOL> self . description . append ( args ) <EOL> if keyword . doc . value : <EOL> self . description . append ( keyword . doc . value ) <EOL> self . description . append ( file_path ) <EOL> def show_definition ( self , view , views_to_center ) : <EOL> source_path = self . keyword . source <EOL> new_view = view . window ( ) . open_file ( "<STR_LIT>" % ( source_path , self . keyword . linenumber ) , sublime . ENCODED_POSITION ) <EOL> new_view . show_at_center ( new_view . text_point ( self . keyword . linenumber , <NUM_LIT:0> ) ) <EOL> if new_view . is_loading ( ) : <EOL> views_to_center [ new_view . id ( ) ] = self . keyword . linenumber <EOL> def __eq__ ( self , other ) : <EOL> return isinstance ( other , WrappedKeyword ) and self . file_path == other . file_path <EOL> def allow_unprompted_go_to ( self ) : <EOL> return True <EOL> class Scanner ( object ) : <EOL> def __init__ ( self , view ) : <EOL> self . view = view <EOL> def scan_file ( self , data_file ) : <EOL> self . start_time = time ( ) <EOL> self . start_path = data_file . directory <EOL> self . scanned_files = set ( ) <EOL> keywords = { } <EOL> self . __scan_file ( keywords , data_file , deque ( ) ) <EOL> return keywords <EOL> def __scan_file ( self , keywords , data_file , import_history ) : <EOL> if time ( ) - self . start_time > SCAN_TIMEOUT : <EOL> sublime . set_timeout ( lambda : self . view . set_status ( '<STR_LIT>' , '<STR_LIT>' ) , <NUM_LIT:0> ) <EOL> sublime . set_timeout ( lambda : self . view . erase_status ( '<STR_LIT>' ) , <NUM_LIT> ) <EOL> return <EOL> self . scanned_files . add ( data_file . source ) <EOL> if data_file . source in import_history : <EOL> return <EOL> import_history = copy ( import_history ) <EOL> import_history . append ( data_file . source ) <EOL> for setting in data_file . imports : <EOL> if hasattr ( setting , '<STR_LIT:type>' ) : <EOL> if setting . type == '<STR_LIT>' : <EOL> resource_path = os . path . normpath ( os . path . join ( setting . directory , setting . name ) ) <EOL> cached , stored_hash = scanner_cache . get_cached_data ( resource_path ) <EOL> if cached : <EOL> self . __scan_file ( keywords , cached , import_history ) <EOL> else : <EOL> try : <EOL> resource_data = ResourceFile ( source = resource_path ) . populate ( ) <EOL> scanner_cache . put_data ( resource_path , resource_data , stored_hash ) <EOL> self . __scan_file ( keywords , resource_data , import_history ) <EOL> except DataError as de : <EOL> print '<STR_LIT>' , resource_path <EOL> self . scan_keywords ( data_file , keywords ) <EOL> def scan_keywords ( self , data_file , keywords ) : <EOL> for keyword in data_file . keyword_table : <EOL> lower_name = keyword . name . lower ( ) <EOL> if not keywords . has_key ( lower_name ) : <EOL> keywords [ lower_name ] = [ ] <EOL> wrapped = WrappedKeyword ( data_file , keyword , os . path . relpath ( keyword . source , self . start_path ) ) <EOL> if wrapped in keywords [ lower_name ] : <EOL> continue <EOL> keywords [ lower_name ] . append ( wrapped ) <EOL> def scan_without_resources ( self , file_path , keywords ) : <EOL> if file_path in self . scanned_files : <EOL> return <EOL> try : <EOL> with open ( file_path , '<STR_LIT:rb>' ) as f : <EOL> lines = f . readlines ( ) <EOL> except IOError as e : <EOL> return <EOL> cached , stored_hash = scanner_cache . get_cached_data ( file_path , lines ) <EOL> if cached : <EOL> self . scan_keywords ( cached , keywords ) <EOL> else : <EOL> try : <EOL> for line in lines : <EOL> if re . search ( detect_robot_regex , line , re . IGNORECASE ) != None : <EOL> data_file = populate_from_lines ( lines , file_path ) <EOL> scanner_cache . put_data ( file_path , data_file , stored_hash ) <EOL> self . scan_keywords ( data_file , keywords ) <EOL> break <EOL> except DataError as de : <EOL> pass </s>
