<s> from __future__ import with_statement <EOL> '''<STR_LIT>''' <EOL> from cPickle import UnpicklingError <EOL> from cPickle import dump as pickle_dump <EOL> from cPickle import load as pickle_load <EOL> from logging import info as log_info <EOL> from os import listdir <EOL> from os . path import isfile , getmtime <EOL> from os . path import join as path_join <EOL> from annotation import Annotations , open_textfile <EOL> from config import DATA_DIR , BASE_DIR <EOL> from message import Messager <EOL> from projectconfig import get_config_path , options_get_validation <EOL> STATS_CACHE_FILE_NAME = '<STR_LIT>' <EOL> def get_stat_cache_by_dir ( directory ) : <EOL> return path_join ( directory , STATS_CACHE_FILE_NAME ) <EOL> def get_config_py_path ( ) : <EOL> return path_join ( BASE_DIR , '<STR_LIT>' ) <EOL> def get_statistics ( directory , base_names , use_cache = True ) : <EOL> cache_file_path = get_stat_cache_by_dir ( directory ) <EOL> try : <EOL> cache_mtime = getmtime ( cache_file_path ) ; <EOL> except OSError , e : <EOL> if e . errno == <NUM_LIT:2> : <EOL> cache_mtime = - <NUM_LIT:1> ; <EOL> else : <EOL> raise <EOL> try : <EOL> if ( not isfile ( cache_file_path ) <EOL> or getmtime ( get_config_py_path ( ) ) > cache_mtime <EOL> or any ( True for f in listdir ( directory ) <EOL> if ( getmtime ( path_join ( directory , f ) ) > cache_mtime <EOL> and not f . startswith ( '<STR_LIT:.>' ) ) ) <EOL> or getmtime ( get_config_path ( directory ) ) > cache_mtime ) : <EOL> generate = True <EOL> docstats = [ ] <EOL> else : <EOL> generate = False <EOL> try : <EOL> with open ( cache_file_path , '<STR_LIT:rb>' ) as cache_file : <EOL> docstats = pickle_load ( cache_file ) <EOL> if len ( docstats ) != len ( base_names ) : <EOL> Messager . warning ( '<STR_LIT>' % cache_file_path ) <EOL> generate = True <EOL> docstats = [ ] <EOL> except UnpicklingError : <EOL> Messager . warning ( '<STR_LIT>' % cache_file_path , - <NUM_LIT:1> ) <EOL> generate = True <EOL> except EOFError : <EOL> generate = True <EOL> except OSError , e : <EOL> Messager . warning ( '<STR_LIT>' ) <EOL> generate = True <EOL> if not use_cache : <EOL> generate = True <EOL> stat_types = [ ( "<STR_LIT>" , "<STR_LIT:int>" ) , ( "<STR_LIT>" , "<STR_LIT:int>" ) , ( "<STR_LIT>" , "<STR_LIT:int>" ) ] <EOL> if options_get_validation ( directory ) != '<STR_LIT:none>' : <EOL> stat_types . append ( ( "<STR_LIT>" , "<STR_LIT:int>" ) ) <EOL> if generate : <EOL> from annotation import JOINED_ANN_FILE_SUFF <EOL> log_info ( '<STR_LIT>' % directory ) <EOL> docstats = [ ] <EOL> for docname in base_names : <EOL> try : <EOL> with Annotations ( path_join ( directory , docname ) , <EOL> read_only = True ) as ann_obj : <EOL> tb_count = len ( [ a for a in ann_obj . get_entities ( ) ] ) <EOL> rel_count = ( len ( [ a for a in ann_obj . get_relations ( ) ] ) + <EOL> len ( [ a for a in ann_obj . get_equivs ( ) ] ) ) <EOL> event_count = len ( [ a for a in ann_obj . get_events ( ) ] ) <EOL> if options_get_validation ( directory ) == '<STR_LIT:none>' : <EOL> docstats . append ( [ tb_count , rel_count , event_count ] ) <EOL> else : <EOL> try : <EOL> from projectconfig import ProjectConfiguration <EOL> projectconf = ProjectConfiguration ( directory ) <EOL> from verify_annotations import verify_annotation <EOL> issues = verify_annotation ( ann_obj , projectconf ) <EOL> issue_count = len ( issues ) <EOL> except : <EOL> issue_count = - <NUM_LIT:1> <EOL> docstats . append ( [ tb_count , rel_count , event_count , issue_count ] ) <EOL> except Exception , e : <EOL> log_info ( '<STR_LIT>' % e ) <EOL> docstats . append ( [ - <NUM_LIT:1> ] * len ( stat_types ) ) <EOL> try : <EOL> with open ( cache_file_path , '<STR_LIT:wb>' ) as cache_file : <EOL> pickle_dump ( docstats , cache_file ) <EOL> except IOError , e : <EOL> Messager . warning ( "<STR_LIT>" % ( directory , e ) ) <EOL> return stat_types , docstats </s>
