Last active
July 21, 2017 21:46
-
-
Save claytantor/4ad616d0fd0f4fe8508bf227fe86130c to your computer and use it in GitHub Desktop.
for use with android app Timesheet https://play.google.com/store/apps/details?id=com.rauscha.apps.timesheet&hl=en
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#coding: utf-8 | |
#python 2.7 | |
# for use with android app Timesheet https://play.google.com/store/apps/details?id=com.rauscha.apps.timesheet&hl=en | |
import json | |
import logging | |
import sys | |
import argparse | |
import csv | |
import time | |
from datetime import datetime | |
LOG_FORMAT = ('%(levelname) -10s %(asctime)s %(funcName) ' | |
'-35s %(lineno) -5d: %(message)s') | |
LOGGER = logging.getLogger(__name__) | |
def calc_amount(duration_native): | |
amount_parts = duration_native.split(':') | |
amount_calc = float(amount_parts[0])+(float(amount_parts[1])/60.0) | |
return amount_calc | |
def make_tweetyfour_time(common_time): | |
m2 = datetime.strptime(common_time, '%I:%M %p') | |
return str(m2).split(' ')[1] | |
def make_row_item(row): | |
# 7/10/2017 |*| 7:51 AM |*| 12:41 PM |*| 4:50:00 |*| Cambia |*| Events controller unit tests , documentation. |*| Yes | |
# 7/10/2017 |*| 12:48 PM |*| 4:35 PM |*| 3:47:00 |*| Cambia |*| Refactor event insert |*| Yes | |
return { | |
'start_time': make_tweetyfour_time(row[1]), | |
'end_time': make_tweetyfour_time(row[2]), | |
'duration': calc_amount(row[3]) | |
} | |
def print_outcsv(model, outfile): | |
outfile.write('date,time_started,lunch_out,lunch_in,time_finished,day_duration\n') | |
for key in model.keys(): | |
line={} | |
if len(model[key])==2: | |
line['date'] = key | |
line['time_started'] = model[key][0]['start_time'] | |
line['lunch_out'] = model[key][0]['end_time'] | |
line['lunch_in'] = model[key][1]['start_time'] | |
line['time_finished'] = model[key][1]['end_time'] | |
line['day_duration'] = model[key][0]['duration']+model[key][1]['duration'] | |
elif len(model[key])==1: | |
line['date'] = key | |
line['time_started'] = '' | |
line['lunch_out'] = '' | |
line['lunch_in'] = model[key][0]['start_time'] | |
line['time_finished'] = model[key][0]['end_time'] | |
line['day_duration'] = model[key][0]['duration'] | |
outfile.write('{date},{time_started},{lunch_out},{lunch_in},{time_finished},{day_duration:.2f}\n'.format(**line)) | |
def main(): | |
print "starting." | |
parser = argparse.ArgumentParser() | |
parser.add_argument("timefile", help="the native csv timefile to read.") | |
parser.add_argument("outfile", help="the csv timefile to write.") | |
args = parser.parse_args() | |
timefile = args.timefile | |
outmodel = {} | |
print 'loading timefile:{0}'.format(timefile) | |
with open(timefile) as csvfile: | |
ireader = csv.reader(csvfile, delimiter=',') | |
ireader.next() | |
for row in ireader: | |
#print(row) | |
if row[0] != None and row[0] != '': | |
if row[0] in outmodel: | |
outmodel[row[0]].append(make_row_item(row)) | |
else: | |
outmodel[row[0]] = [make_row_item(row)] | |
f = open(args.outfile, 'w') | |
print_outcsv(outmodel, f) | |
f.close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment