<s> import csv <EOL> import errno <EOL> import logging <EOL> from os import listdir , makedirs , remove , rename <EOL> from os . path import exists , isdir , join , splitext <EOL> from StringIO import StringIO <EOL> from shutil import rmtree <EOL> from time import time <EOL> import simplejson as json <EOL> from pylons import config <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class MetricsSession ( object ) : <EOL> keys = [ '<STR_LIT:file>' , '<STR_LIT>' , '<STR_LIT:size>' , '<STR_LIT:type>' , '<STR_LIT:time>' , '<STR_LIT:status>' ] <EOL> _slug_sessions = { } <EOL> _last_timestamp = <NUM_LIT:0> <EOL> def __init__ ( self , slug ) : <EOL> self . entries = [ ] <EOL> self . slug = slug <EOL> timestamp = '<STR_LIT>' % time ( ) <EOL> if MetricsSession . _last_timestamp == timestamp : <EOL> timestamp += '<STR_LIT:1>' <EOL> MetricsSession . _last_timestamp = timestamp <EOL> self . timestamp = timestamp <EOL> self . file_name = MetricsSession . get_file_name ( slug , timestamp ) <EOL> LOG . info ( '<STR_LIT>' , timestamp ) <EOL> def __del__ ( self ) : <EOL> self . finish ( ) <EOL> def append ( self , file_name , file_size , file_type , file_status ) : <EOL> _ , file_ext = splitext ( file_name ) <EOL> entry = { <EOL> "<STR_LIT:file>" : file_name , <EOL> "<STR_LIT>" : file_ext , <EOL> "<STR_LIT:size>" : int ( file_size ) , <EOL> "<STR_LIT:type>" : file_type , <EOL> "<STR_LIT:time>" : time ( ) , <EOL> "<STR_LIT:status>" : file_status <EOL> } <EOL> self . entries . append ( entry ) <EOL> def get_file_path ( self ) : <EOL> return self . file_name <EOL> def finish ( self ) : <EOL> folder_name = MetricsSession . get_folder_name ( self . slug ) <EOL> if not exists ( folder_name ) : <EOL> try : <EOL> makedirs ( folder_name ) <EOL> except OSError , e : <EOL> if e . errno != errno . EEXIST : <EOL> LOG . error ( str ( e ) ) <EOL> raise <EOL> LOG . info ( '<STR_LIT>' , self . file_name ) <EOL> session = { '<STR_LIT>' : self . entries } <EOL> try : <EOL> f = open ( self . file_name , mode = '<STR_LIT:w>' ) <EOL> json . dump ( session , f , indent = <NUM_LIT:4> ) <EOL> f . close ( ) <EOL> except IOError , e : <EOL> LOG . error ( str ( e ) ) <EOL> @ classmethod <EOL> def get_folder_name ( cls , slug ) : <EOL> return join ( config [ '<STR_LIT>' ] , slug ) <EOL> @ classmethod <EOL> def get_file_name ( cls , slug , timestamp ) : <EOL> return join ( cls . get_folder_name ( slug ) , '<STR_LIT>' % timestamp ) <EOL> @ classmethod <EOL> def get_csv_file_name ( cls , slug , timestamp ) : <EOL> return join ( cls . get_folder_name ( slug ) , '<STR_LIT>' % timestamp ) <EOL> @ classmethod <EOL> def get_data ( cls , slug , timestamp ) : <EOL> file_name = cls . get_file_name ( slug , timestamp ) <EOL> try : <EOL> f = open ( file_name , mode = '<STR_LIT:r>' ) <EOL> session_data = json . load ( f ) <EOL> f . close ( ) <EOL> except ( IOError , json . JSONDecodeError ) : <EOL> session_data = '<STR_LIT>' <EOL> return session_data <EOL> @ classmethod <EOL> def get_data_as_csv ( cls , slug , timestamp ) : <EOL> csv_buffer = StringIO ( ) <EOL> file_name = cls . get_file_name ( slug , timestamp ) <EOL> keys = MetricsSession . keys <EOL> try : <EOL> with open ( file_name , '<STR_LIT:r>' ) as f : <EOL> session_data = json . load ( f ) <EOL> rows = session_data [ '<STR_LIT>' ] <EOL> except IOError : <EOL> return None <EOL> else : <EOL> writer = csv . DictWriter ( csv_buffer , keys ) <EOL> writer . writerow ( dict ( zip ( keys , keys ) ) ) <EOL> for row in rows : <EOL> writer . writerow ( row ) <EOL> csv_data = csv_buffer . getvalue ( ) <EOL> csv_buffer . close ( ) <EOL> return csv_data <EOL> @ classmethod <EOL> def get_data_as_json ( cls , slug , timestamp ) : <EOL> file_name = cls . get_file_name ( slug , timestamp ) <EOL> try : <EOL> f = open ( file_name , '<STR_LIT:r>' ) <EOL> session_data = f . read ( ) <EOL> f . close ( ) <EOL> except IOError : <EOL> return None <EOL> else : <EOL> return session_data <EOL> @ classmethod <EOL> def delete ( cls , slug , timestamp ) : <EOL> if timestamp : <EOL> file_name = cls . get_file_name ( slug , timestamp ) <EOL> try : <EOL> remove ( file_name ) <EOL> except OSError : <EOL> return False <EOL> else : <EOL> rmtree ( cls . get_folder_name ( slug ) , True ) <EOL> return True <EOL> @ classmethod <EOL> def rename ( cls , old_slug , new_slug ) : <EOL> old_folder = cls . get_folder_name ( old_slug ) <EOL> new_folder = cls . get_folder_name ( new_slug ) <EOL> try : <EOL> rmtree ( new_folder ) <EOL> except OSError : <EOL> pass <EOL> try : <EOL> rename ( old_folder , new_folder ) <EOL> except OSError : <EOL> pass <EOL> @ classmethod <EOL> def stop_recording ( cls , slug ) : <EOL> try : <EOL> del cls . _slug_sessions [ slug ] <EOL> except KeyError : <EOL> return False <EOL> else : <EOL> return True <EOL> @ classmethod <EOL> def has_metrics ( cls , slug ) : <EOL> if slug in cls . _slug_sessions : <EOL> return True <EOL> folder_name = cls . get_folder_name ( slug ) <EOL> if isdir ( folder_name ) : <EOL> for f in listdir ( folder_name ) : <EOL> if f . endswith ( '<STR_LIT>' ) : <EOL> return True <EOL> return False <EOL> @ classmethod <EOL> def get_metrics ( cls , slug ) : <EOL> cls . stop_recording ( slug ) <EOL> folder_name = cls . get_folder_name ( slug ) <EOL> if isdir ( folder_name ) : <EOL> timestamps = [ f [ : - <NUM_LIT:5> ] for f in listdir ( folder_name ) if f . endswith ( '<STR_LIT>' ) ] <EOL> timestamps . sort ( ) <EOL> return [ { '<STR_LIT>' : t , '<STR_LIT>' : cls . get_data ( slug , t ) [ '<STR_LIT>' ] } for t in timestamps ] <EOL> else : <EOL> return [ ] <EOL> @ classmethod <EOL> def get_sessions ( cls , slug ) : <EOL> try : <EOL> slug_sessions = cls . _slug_sessions [ slug ] <EOL> except KeyError : <EOL> slug_sessions = { } <EOL> cls . _slug_sessions [ slug ] = slug_sessions <EOL> return slug_sessions </s>
