#back to logger intro page
"""
log.py
This application logs time spent on projects, and work done during that time. It can be useful to freelancers, contractors, etc.
Ideas for improvement:
    Graphic presentation and statistics of hours spend (on each project) in day/week/month views
    Pickling the data so that logs could be grouped by project or other categories
    At this moment, data can be grouped in excel after importing it in tab delimited format (there is a dummy first column for indexing projects)
"""

from time import strftime, localtime, time

def start():
    print """\nApplication logs time spent on different projects specified by the user"""
    print '-'*72,"\n"
    try:
        f = open("log.txt","r")         #check if log file already exists
    except IOError:
        print "Log file does not exist! New one is created: 'log.txt'."
        f = open("log.txt","w")
        f.close()
        f = open("log.txt","r")
    projectString = f.readline()        #storing projects array into string
    restOfFile = f.readlines()          #storing the rest of the file into the string
    f.close()
    
    #reading and adding projects and hour rates to the project string (first line in the file)
    tempProjectString = projectString.split("|")
    projectName=""
    print "Enter:"
    for i in range(0,len(tempProjectString),2):
        print i,"\tfor\t",str(tempProjectString[i]).rstrip()
    print "or any words to define a new project name:"
    while projectName.isdigit() or projectName=="":
        projectName = raw_input()
        if projectName.isdigit():
            if int(projectName) in range(0,len(tempProjectString),2):
                hourlyRate = tempProjectString[(int(projectName)+1)]
                index = int(projectName)
                projectName = tempProjectString[int(projectName)]
        elif  projectName<>"":
            if projectString=="":
                ttprojectString = projectName
                index=0
            else:
                tp=projectString.rstrip()
                ttprojectString = tp[:]+"|"+projectName
                index = len(tempProjectString)
            print "Enter hourly rate for project '",projectName,"':"
            while True:
                try:
                    hourlyRate = raw_input()
                    tr = int(hourlyRate)
                    th=ttprojectString.rstrip()
                    tprojectString = th[:]+"|"+hourlyRate+"\n"
                    projectString=tprojectString
                    break
                except ValueError:
                    print "That was no valid number.  Try again... Enter only integers."

    #write the old content of the file before storing new logs
    f = open("log.txt","w")
    f.writelines(projectString)
    f.writelines(restOfFile)
    
    #adding current log info
    print "You are working on the project",projectName
    s = "\n",str(index),"\tProject:\t",projectName,"\n",str(index),"\tHourly rate:\t",hourlyRate,"\n"
    f.writelines(s)
    begin=time()
    temp=begin
    value = strftime("%a, %d %b %Y %H:%M:%S +0000", localtime())
    s = str(index),"\tBegin: ",str(value),"\n\n"
    f.writelines(s)
    s = raw_input("\nEnter description of the work done; when finished type 'exit' to exit:\n")
    while s.lower() <> "exit":
        value = round((time()-temp)/(60*60),2)
        temp=time()
        s=str(index),"\t",s,"\t",str(value),"\n"
        f.writelines(s)
        s = raw_input()
    value = round((time()-begin)/(60*60),2)
    s = "\n", str(index),"\tTotal time:\t\t",str(value),"\n", str(index),"\tMoney earned:\t\t\t",str(int(hourlyRate)*value),"\n"
    f.writelines(s)
    value = strftime("%a, %d %b %Y %H:%M:%S +0000", localtime())
    s = "\n", str(index),"\tEnd :",str(value),"\n\n",'-'*31,"\n"
    f.writelines(s)
    f.close()
    s = raw_input("If you want to start a new work press 'y', any other key for no: ")
    if s=="y":
        start()
        
start()
print "File 'log.txt' has been updated. You can import it in excel with tab delimited format."
print "Log data can be grouped by projects by sorting in Excel by the first column."
raw_input()
#