<s> import keyring <EOL> import getpass <EOL> import urllib2 <EOL> import reap . api . admin <EOL> import datetime <EOL> from reap . commands . support import * <EOL> REPORT_HEADER = '''<STR_LIT>''' <EOL> HOURS_REPORT_FORMAT = '''<STR_LIT>''' <EOL> PROJECTS_REPORT_HEADING_FORMAT = '''<STR_LIT>''' <EOL> PROJECTS_REPORT_BODY_FORMAT = '''<STR_LIT>''' <EOL> TASKS_HEADER_FORMAT = '''<STR_LIT>''' <EOL> PROJECT_BY_TASKS_HEADER_FORMAT = '''<STR_LIT>''' <EOL> TASKS_BODY_FORMAT = '''<STR_LIT>''' <EOL> def get_harvest ( ) : <EOL> info = load_info ( ) <EOL> if info : <EOL> base_uri = info [ <NUM_LIT:0> ] <EOL> username = info [ <NUM_LIT:1> ] <EOL> passwd = keyring . get_password ( base_uri , username ) <EOL> return reap . api . admin . Harvest ( base_uri , username , passwd ) <EOL> def get_people ( hv , ids ) : <EOL> people = [ ] <EOL> all_people = hv . people ( ) <EOL> for pid in set ( ids ) : <EOL> id = int ( pid ) <EOL> for p in all_people : <EOL> if p . id == id : <EOL> people += [ p ] <EOL> break <EOL> return people <EOL> def get_projects ( hv , ids ) : <EOL> projects = [ ] <EOL> all_projects = hv . projects ( ) <EOL> for pid in set ( ids ) : <EOL> id = int ( pid ) <EOL> for p in all_projects : <EOL> if p . id == id : <EOL> projects += [ p ] <EOL> break <EOL> return projects <EOL> def parse_time_inputs ( startstr , endstr ) : <EOL> if startstr : <EOL> start = datetime . datetime . strptime ( startstr , '<STR_LIT>' ) <EOL> else : <EOL> start = datetime . datetime . today ( ) <EOL> if endstr : <EOL> end = datetime . datetime . strptime ( endstr , '<STR_LIT>' ) <EOL> else : <EOL> end = datetime . datetime . today ( ) <EOL> return ( start , end ) <EOL> def hours ( args ) : <EOL> hv = get_harvest ( ) <EOL> if hv : <EOL> people = get_people ( hv , args . personids ) <EOL> if len ( people ) > <NUM_LIT:0> : <EOL> times = parse_time_inputs ( args . start , args . end ) <EOL> start = times [ <NUM_LIT:0> ] <EOL> end = times [ <NUM_LIT:1> ] <EOL> entries_collection = [ <EOL> ( person , person . entries ( start = start , end = end ) ) <EOL> for person in people <EOL> ] <EOL> if len ( entries_collection ) > <NUM_LIT:0> : <EOL> print str . format ( <EOL> REPORT_HEADER , <EOL> '<STR_LIT>' , <EOL> start . strftime ( '<STR_LIT>' ) , <EOL> end . strftime ( '<STR_LIT>' ) , <EOL> ) <EOL> overall_hours = <NUM_LIT:0.0> <EOL> overall_billable = <NUM_LIT:0.0> <EOL> projects = hv . projects ( ) <EOL> for person , entries in entries_collection : <EOL> map = { <EOL> p : { <EOL> t : [ <EOL> e for e in entries <EOL> if e . project_id == p . id and e . task_id == t . task_id <EOL> ] <EOL> for t in p . task_assignments ( ) <EOL> } <EOL> for p in projects <EOL> } <EOL> total = <NUM_LIT:0.0> <EOL> billable = <NUM_LIT:0.0> <EOL> unbillable = <NUM_LIT:0.0> <EOL> for proj in map : <EOL> for task in map [ proj ] : <EOL> for entry in map [ proj ] [ task ] : <EOL> if task . billable : <EOL> billable += entry . hours <EOL> overall_billable += entry . hours <EOL> else : <EOL> unbillable += entry . hours <EOL> total += entry . hours <EOL> overall_hours += entry . hours <EOL> ratio = billable / unbillable if unbillable > <NUM_LIT:0.0> else <NUM_LIT:0.0> <EOL> percent = billable / total if total > <NUM_LIT:0.0> else <NUM_LIT:0.0> <EOL> print str . format ( <EOL> HOURS_REPORT_FORMAT , <EOL> total = total , <EOL> billable = billable , <EOL> unbillable = unbillable , <EOL> ratio = ratio , <EOL> person = person , <EOL> percent = percent , <EOL> ) <EOL> print str . format ( <EOL> '<STR_LIT>' , <EOL> overall_billable / overall_hours if overall_hours != <NUM_LIT:0.0> else <NUM_LIT:0> <EOL> ) <EOL> else : <EOL> print '<STR_LIT>' <EOL> else : <EOL> print '<STR_LIT>' <EOL> def projects ( args ) : <EOL> hv = get_harvest ( ) <EOL> if hv : <EOL> people = get_people ( hv , args . personids ) <EOL> if len ( people ) > <NUM_LIT:0> : <EOL> times = parse_time_inputs ( args . start , args . end ) <EOL> start = times [ <NUM_LIT:0> ] <EOL> end = times [ <NUM_LIT:1> ] <EOL> projects = hv . projects ( ) <EOL> projects_by_id = { project . id : project for project in projects } <EOL> print str . format ( <EOL> REPORT_HEADER , <EOL> '<STR_LIT>' , <EOL> start . strftime ( '<STR_LIT>' ) , <EOL> end . strftime ( '<STR_LIT>' ) , <EOL> ) <EOL> for person in people : <EOL> print str . format ( PROJECTS_REPORT_HEADING_FORMAT , person = person ) <EOL> person_projects = { } <EOL> for entry in person . entries ( start = start , end = end ) : <EOL> project = projects_by_id [ entry . project_id ] <EOL> if person_projects . has_key ( project ) : <EOL> person_projects [ project ] += entry . hours <EOL> else : <EOL> person_projects [ project ] = entry . hours <EOL> for project in person_projects . keys ( ) : <EOL> print str . format ( <EOL> PROJECTS_REPORT_BODY_FORMAT , <EOL> name = project . name , <EOL> hours = person_projects [ project ] <EOL> ) <EOL> else : <EOL> print '<STR_LIT>' <EOL> def tasks ( args ) : <EOL> hv = get_harvest ( ) <EOL> if hv : <EOL> people = get_people ( hv , args . personids ) <EOL> if len ( people ) > <NUM_LIT:0> : <EOL> times = parse_time_inputs ( args . start , args . end ) <EOL> start = times [ <NUM_LIT:0> ] <EOL> end = times [ <NUM_LIT:1> ] <EOL> tasks = hv . tasks ( ) <EOL> tasks_by_id = { task . id : task for task in tasks } <EOL> print str . format ( <EOL> REPORT_HEADER , <EOL> '<STR_LIT>' , <EOL> start . strftime ( '<STR_LIT>' ) , <EOL> end . strftime ( '<STR_LIT>' ) , <EOL> ) <EOL> for person in people : <EOL> print str . format ( TASKS_HEADER_FORMAT , person = person ) <EOL> person_tasks = { } <EOL> for entry in person . entries ( start = start , end = end ) : <EOL> task = tasks_by_id [ entry . task_id ] <EOL> if person_tasks . has_key ( task ) : <EOL> person_tasks [ task ] += entry . hours <EOL> else : <EOL> person_tasks [ task ] = entry . hours <EOL> for task in person_tasks . keys ( ) : <EOL> print str . format ( <EOL> TASKS_BODY_FORMAT , <EOL> name = task . name , <EOL> hours = person_tasks [ task ] <EOL> ) <EOL> else : <EOL> print '<STR_LIT>' <EOL> def tasks_by_proj ( args ) : <EOL> hv = get_harvest ( ) <EOL> if hv : <EOL> projects = get_projects ( hv , args . projectids ) <EOL> if len ( projects ) > <NUM_LIT:0> : <EOL> times = parse_time_inputs ( args . start , args . end ) <EOL> start = times [ <NUM_LIT:0> ] <EOL> end = times [ <NUM_LIT:1> ] <EOL> tasks_by_id = { task . id : task for task in hv . tasks ( ) } <EOL> print str . format ( <EOL> REPORT_HEADER , <EOL> '<STR_LIT>' , <EOL> start . strftime ( '<STR_LIT>' ) , <EOL> end . strftime ( '<STR_LIT>' ) , <EOL> ) <EOL> for project in projects : <EOL> print str . format ( <EOL> PROJECT_BY_TASKS_HEADER_FORMAT , <EOL> project = project <EOL> ) <EOL> entries = project . entries ( start = start , end = end ) <EOL> project_tasks = { } <EOL> for entry in entries : <EOL> task = tasks_by_id [ entry . task_id ] <EOL> if project_tasks . has_key ( task ) : <EOL> project_tasks [ task ] += entry . hours <EOL> else : <EOL> project_tasks [ task ] = entry . hours <EOL> for task in project_tasks : <EOL> print str . format ( <EOL> TASKS_BODY_FORMAT , <EOL> name = task . name , <EOL> hours = project_tasks [ task ] <EOL> ) <EOL> else : <EOL> print '<STR_LIT>' </s>
