#NEWLINE# def parse_presence( filename = None ) : #NEWLINE# #INDENT# "Parse the presence reports text file.\n       filename is the reports file to parse. defaults to 'presence.txt'\n       Will throw an IOError if the file is not found, or can't be read\n       Returns a tuple (member_totals, not_enough_data)\n       member_totals is a dict with member ids as keys, and a list of (week_timestamp, weekly hours) for this member as values\n       enough_data is a list of week timestamps in which we had enough data to compute weekly hours\n       a timestamp is a tuple (year, iso week number)\n    " #NEWLINE# if ( filename == None ) : #NEWLINE# #INDENT# filename = 'presence.txt' #NEWLINE# #UNINDENT# member_totals = dict ( ) #NEWLINE# totals = dict ( ) #NEWLINE# total_time = 0.0 #NEWLINE# f = gzip . open ( filename , 'r' ) #NEWLINE# workdays = [ 0 , 1 , 2 ] #NEWLINE# last_timestamp = None #NEWLINE# todays_timestamp = date . today ( ) . isocalendar ( ) [ : 2 ] #NEWLINE# reports = [ ] #NEWLINE# enough_data = [ ] #NEWLINE# line = f . readline ( ) #NEWLINE# data = line . split ( ',' ) #NEWLINE# time = datetime . strptime ( data [ 0 ] , '%Y-%m-%d %H:%M:%S' ) #NEWLINE# for line in f : #NEWLINE# #INDENT# data = line . split ( ',' ) #NEWLINE# last_time = time #NEWLINE# time = datetime . strptime ( data [ 0 ] , '%Y-%m-%d %H:%M:%S' ) #NEWLINE# time_in_day = ( time . hour + ( time . minute / 60.0 ) ) #NEWLINE# current_timestamp = time . isocalendar ( ) [ : 2 ] #NEWLINE# if ( ( time . weekday ( ) not in workdays ) or ( time_in_day < 6.0 ) or ( time_in_day > 22.0 ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( current_timestamp == todays_timestamp ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# if ( current_timestamp != last_timestamp ) : #NEWLINE# #INDENT# if ( len ( reports ) > 200 ) : #NEWLINE# #INDENT# enough_data . append ( last_timestamp ) #NEWLINE# subtotals = dict ( ) #NEWLINE# subtotal_time = 0 #NEWLINE# for m in reports : #NEWLINE# #INDENT# minutes = min ( m [ 0 ] , 15 ) #NEWLINE# subtotal_time += minutes #NEWLINE# for i in m [ 1 ] : #NEWLINE# #INDENT# if ( i in subtotals ) : #NEWLINE# #INDENT# subtotals [ i ] += minutes #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# subtotals [ i ] = minutes #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# for m in subtotals : #NEWLINE# #INDENT# if ( m in totals ) : #NEWLINE# #INDENT# totals [ m ] += float ( subtotals [ m ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# totals [ m ] = float ( subtotals [ m ] ) #NEWLINE# #UNINDENT# #UNINDENT# total_time += subtotal_time #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# reports = [ ] #NEWLINE# for m in totals : #NEWLINE# #INDENT# d = last_timestamp #NEWLINE# if ( m in member_totals ) : #NEWLINE# #INDENT# member_totals [ m ] . append ( ( d , round ( ( ( float ( totals [ m ] ) / total_time ) * WORKING_HOURS_PER_WEEK ) ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# member_totals [ m ] = [ ( d , round ( ( ( float ( totals [ m ] ) / total_time ) * WORKING_HOURS_PER_WEEK ) ) ) ] #NEWLINE# #UNINDENT# #UNINDENT# totals = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# total_time = 0.0 #NEWLINE# last_timestamp = time . isocalendar ( ) [ : 2 ] #NEWLINE# #UNINDENT# reports . append ( ( ( ( time - last_time ) . seconds / 60 ) , [ int ( x ) for x in data [ 1 : ] if ( len ( x . strip ( ) ) > 0 ) ] ) ) #NEWLINE# #UNINDENT# return ( member_totals , enough_data )